Sai*_*ham 5 variables powershell global-variables local
非常感谢@brianlist,我已经尝试了你的建议,当我使用它与批量复制时,它给了我这个错误,不确定该值是否已传递给$value.
Cannot convert argument "0", with value: "System.Object[]", for "WriteToServer"
to type "System.Data.DataRow[]": "Cannot convert the "System.Data.DataRow" value
of type "Deserialized.System.Data.DataRow" to type "System.Data.DataRow"."
At C:\test\modified.ps1:11 char:24
+ $bulkCopy.WriteToServer <<<< ($value)
+ CategoryInfo : NotSpecified: (:) [], MethodException
+ FullyQualifiedErrorId : MethodArgumentConversionInvalidCastArgument
Run Code Online (Sandbox Code Playgroud)
Get-Content 'C:\test\computers.txt' | ? { $_.trim() -ne "" } | ForEach-Object {
$value = Invoke-Command -Computer $_ -ScriptBlock {
Param($computer)
#..
$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
$global:mdtable = New-Object System.Data.DataTable
$SqlAdapter.SelectCommand = $SqlCmd
$nRecs = $SqlAdapter.Fill($mdtable)
$mdtable
} -ArgumentList $_ -Credential $cred
} | test
Function test {
$Database = 'Test1'
$table = 'dbo.table_2'
$connectionstring = "Data Source=mylocal;Integrated Security=True;Initial Catalog=$Database"
$Conn = New-Object System.Data.SqlClient.SQLConnection($connectionstring)
$conn.open()
$bulkcopy = New-Object Data.SqlClient.SqlBulkCopy($conn)
$bulkcopy.DestinationTableName = $table
$bulkCopy.WriteToServer($value)
$conn.close()
}
Run Code Online (Sandbox Code Playgroud)
$mdtable您已经从脚本块返回了 的值Invoke-Command。要获取该值之外的值,只需将 的结果分配Invoke-Command给一个变量:
Get-Content 'C:\test\computers.txt'| ? {$_.trim() -ne "" } | ForEach-object{
$value = Invoke-Command -Computer $_ -ScriptBlock {
Param($computer)
# ...
$mdtable
}
Run Code Online (Sandbox Code Playgroud)
在此示例中,$value将包含$mdtable有的内容。
| 归档时间: |
|
| 查看次数: |
177 次 |
| 最近记录: |