PowerShell正在尝试编写SQL工厂代码。。。
我正在编写一个需要多次sql连接的脚本。我需要平行学习来加快速度。这就是为什么我希望有一种SQL工厂,它会返回一个连接句柄。但不知为什么我的代码不起作用。我做错了什么PowerShell正在尝试编写SQL工厂代码。。。,sql,powershell,module,Sql,Powershell,Module,我正在编写一个需要多次sql连接的脚本。我需要平行学习来加快速度。这就是为什么我希望有一种SQL工厂,它会返回一个连接句柄。但不知为什么我的代码不起作用。我做错了什么 $m = New-Module -Name sql_factory -AsCustomObject -ScriptBlock { Function new_session { $db_host = 'my.sqlhost.tld'; $db_user = 'user'; $d
$m = New-Module -Name sql_factory -AsCustomObject -ScriptBlock {
Function new_session {
$db_host = 'my.sqlhost.tld';
$db_user = 'user';
$db_pass = 'pass';
$db_name = 'db';
$table_name = $env:COMPUTERNAME;
$conn = New-Object system.data.sqlclient.sqlconnection;
$conn.ConnectionString = "Server=${db_host};Database=${db_name};User ID=${db_user};Password=${db_pass};";
$cmd = New-Object System.Data.SqlClient.SqlCommand;
$cmd.connection = $conn;
return $cmd, $conn;
}
}
$test = {
$cmd, $conn = $m.new_session();
$conn.Open();
$cmd.CommandText = "INSERT INTO tableXXX (date, time, action, protocol, src_ip, dst_ip, src_port, dst_port, size, tcp_flags, tcpsyn, tcpack, tcpwin, icmptype, icmpcode, info, path) VALUES ('xxx','xxx','xxx','xxx','xxx','xxx','xxx','xxx','xxx','xxx','xxx','xxx','xxx','xxx','xxx','xxx','xxx')";
$cmd.ExecuteNonQuery();
$conn.Close();
}
start-job -ScriptBlock $test | Out-Null
get-job | receive-job -AutoRemoveJob -Wait
这是我收到的错误消息
You cannot call a method on a null-valued expression.
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
+ PSComputerName : localhost
You cannot call a method on a null-valued expression.
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
+ PSComputerName : localhost
Property 'CommandText' cannot be found on this object; make sure it exists and is settable.
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : PropertyNotFound
+ PSComputerName : localhost
You cannot call a method on a null-valued expression.
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
+ PSComputerName : localhost
You cannot call a method on a null-valued expression.
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
+ PSComputerName : localhost
脚本块对您创建的模块一无所知。开始作业时,需要将其作为参数传递:
$test = {
param($db_factory)
$cmd, $conn = $db_factory.new_session()
...
}
Start-Job -ScriptBlock $test -ArgumentList $m | Out-Null
...
但是,由于您正在为每个作业创建新连接,因此您也可以将建立数据库连接的代码放在脚本块中:
$test = {
param($db_host, $db_user, $db_pass, $db_name)
$table_name = $env:COMPUTERNAME
$cs = "Server=$db_host;Database=$db_name;User ID=$db_user;Password=$db_pass;"
$conn = New-Object system.data.sqlclient.sqlconnection
$conn.ConnectionString = $cs
$cmd = New-Object System.Data.SqlClient.SqlCommand
$cmd.connection = $conn
$conn.Open()
...
}
Start-Job -ScriptBlock $test -ArgumentList 'my.sqlhost.tld','user','pass','db' |
Out-Null
...
您还可以使用SQL Server管理对象(SMO)
谢谢你的回复。我不想将用于建立的代码放在“开始作业-脚本块”中,因为这会非常重复。我有几个insert是由start job启动的,它们并行运行,但我还有几个其他连接,用于设置数据库和表属性……您的代码的重复性并没有减少。只是有点复杂。
$m = {
param($instanceName, $login, $pass, $dbName)
#import SQL Server module
Import-Module SQLPS -DisableNameChecking
#Create server connection
$srvConn = New-Object Microsoft.SqlServer.Management.Common.ServerConnection
$srvConn.ServerInstance = $instanceName
$srvConn.LoginSecure = $false
$srvConn.Login = $login
$srvConn.Password = $pass
#Initiate the object and set the database
$srv = New-Object Microsoft.SqlServer.Management.SMO.Server($srvConn)
$db = $srv.Databases[$dbName]
#Execute T-SQL
$db.ExecuteNonQuery("INSERT INTO tableXXX (date, time, action, protocol, src_ip, dst_ip, src_port, dst_port, size, tcp_flags, tcpsyn, tcpack, tcpwin, icmptype, icmpcode, info, path) VALUES ('xxx','xxx','xxx','xxx','xxx','xxx','xxx','xxx','xxx','xxx','xxx','xxx','xxx','xxx','xxx','xxx','xxx')")
}
start-job -ScriptBlock $m -ArgumentList 'my.sqlhost.tld','user','pass','db' | Out-Null
get-job | receive-job -AutoRemoveJob -Wait