如何使用 PowerShell 或 VBScript 将数据插入 Postgres 表?

发布于 2024-07-25 21:44:02 字数 285 浏览 4 评论 0原文

我需要定期查询少数服务器上的事件日志并将特定事件插入 Postgres 表中。

我无法弄清楚如何使用 PowerShell 和/或 VBScript 通过 ODBC 将数据插入表中。 一般来说,我对 VBScript 和 PowerShell 相当熟悉,并且我可以编写一个有效的 SQL UPDATE 语句,我只是试图将两者结合在一起,这是我以前从未做过的。

我安装了 Postgres ODBC 驱动程序,配置了一个测试正常的数据源。

到目前为止,谷歌还没有帮助我,有人可以提供一些指导吗?

I need to periodically query the event logs on a handful of servers and insert specific events into a Postgres table.

I am having trouble figuring out how I can insert data into a table via ODBC using PowerShell and/or VBScript. I'm reasonably familiar with both VBScript and PowerShell generally, and I can craft a SQL UPDATE statement that works, I'm just trying to tie the two together, which I've never done before.

I have the Postgres ODBC driver installed, I've configured a data source which tests OK.

Google isn't helping me thus far, can someone provide some pointers?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

唠甜嗑 2024-08-01 21:44:02

感谢您的回复。

我使用了更多的谷歌搜索,最终摸索了足够多的 ADO.NET 来让它工作,尽管它可能不一定是“正确的”。

$DBConnectionString = "Driver={PostgreSQL UNICODE};Server=$DBIP;Port=$DBPort;Database=$DBName;Uid=$DBUser;Pwd=$DBPass;"

$DBConn = New-Object System.Data.Odbc.OdbcConnection
$DBConn.ConnectionString = $DBConnectionString

$DBCmd = $DBConn.CreateCommand()

[void]$DBCmd.Parameters.Add("@TimeStamp", [System.Data.Odbc.OdbcType]::varchar, 26)
[void]$DBCmd.Parameters.Add("@ErrorText", [System.Data.Odbc.OdbcType]::varchar, 4000)

$DBCmd.CommandText = "INSERT INTO errorinfo (errortime,xml) VALUES(?,?)"

$DBCmd.Connection.Open()

$DBCmd.Parameters["@TimeStamp"].Value = $TimeStamp.ToString("yyyy-MM-dd HH:mm:ss")
$DBCmd.Parameters["@ErrorText"].Value = $ErrorText

[void]$DBCmd.ExecuteNonQuery()

Thanks for the response.

I used more Googling and eventually grokked enough ADO.NET to get it working, although it may not necessarily be "correct".

$DBConnectionString = "Driver={PostgreSQL UNICODE};Server=$DBIP;Port=$DBPort;Database=$DBName;Uid=$DBUser;Pwd=$DBPass;"

$DBConn = New-Object System.Data.Odbc.OdbcConnection
$DBConn.ConnectionString = $DBConnectionString

$DBCmd = $DBConn.CreateCommand()

[void]$DBCmd.Parameters.Add("@TimeStamp", [System.Data.Odbc.OdbcType]::varchar, 26)
[void]$DBCmd.Parameters.Add("@ErrorText", [System.Data.Odbc.OdbcType]::varchar, 4000)

$DBCmd.CommandText = "INSERT INTO errorinfo (errortime,xml) VALUES(?,?)"

$DBCmd.Connection.Open()

$DBCmd.Parameters["@TimeStamp"].Value = $TimeStamp.ToString("yyyy-MM-dd HH:mm:ss")
$DBCmd.Parameters["@ErrorText"].Value = $ErrorText

[void]$DBCmd.ExecuteNonQuery()
绮烟 2024-08-01 21:44:02

您的哪一部分遇到了问题? 你已经走了多远? 您有打开的连接吗? 你知道连接字符串的语法吗?

准备连接:

Set conn = Server.CreateObject("ADODB.Connection")
conn.Open dsn, dbuser, dbpass

插入:

insert = "insert into table (col1, col2) values (12, 'Example Record')"
conn.Execute insert
If conn.errors.Count > 0 Then
    Dim counter
    WScript.echo "Error during insert"
    For counter = 0 To conn.errors.Count
        WScript.echo "Error #" & DataConn.errors(counter).Number
        WScript.echo "  Description(" & DataConn.errors(counter).Description & ")"
    Next
Else
    WScript.echo "insert: ok"
End If

为了完整性,查询:

query = "select * from table where col1 = 7"
Set recordSet = conn.execute(query)
' result is an object of type ADODB.RecordSet

如果您需要 powershell,请尝试 这篇文章
如果您需要知道连接字符串,请尝试 connectionstrings.com

What part are you having trouble with? How far have you got? Do you have a connection open? Do you know the syntax of the connection string?

Prepare a connection:

Set conn = Server.CreateObject("ADODB.Connection")
conn.Open dsn, dbuser, dbpass

insert:

insert = "insert into table (col1, col2) values (12, 'Example Record')"
conn.Execute insert
If conn.errors.Count > 0 Then
    Dim counter
    WScript.echo "Error during insert"
    For counter = 0 To conn.errors.Count
        WScript.echo "Error #" & DataConn.errors(counter).Number
        WScript.echo "  Description(" & DataConn.errors(counter).Description & ")"
    Next
Else
    WScript.echo "insert: ok"
End If

for completeness, query:

query = "select * from table where col1 = 7"
Set recordSet = conn.execute(query)
' result is an object of type ADODB.RecordSet

If you want powershell, try this post.
If you need to know the connection string, try connectionstrings.com.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文