gpt4 book ai didi

powershell - 远程范围到本地

转载 作者:行者123 更新时间:2023-12-04 19:51:53 28 4
gpt4 key购买 nike

非常感谢@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

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()
}

最佳答案

您已经返回了 $mdtable 的值来自 Invoke-Command脚本块。要获得超出此范围的值,只需将 Invoke-Command 的结果赋值即可到一个变量:

Get-Content 'C:\test\computers.txt'| ? {$_.trim() -ne "" } | ForEach-object{
$value = Invoke-Command -Computer $_ -ScriptBlock {
Param($computer)
# ...
$mdtable
}

在本例中, $value将包含什么 $mdtable有。

关于powershell - 远程范围到本地,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34120978/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com