Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell 如何编写多个对象链接方法调用以跨多行代码继续?_Powershell_Powershell 2.0 - Fatal编程技术网

Powershell 如何编写多个对象链接方法调用以跨多行代码继续?

Powershell 如何编写多个对象链接方法调用以跨多行代码继续?,powershell,powershell-2.0,Powershell,Powershell 2.0,我遇到了很多关于如何跨多行继续powershell脚本代码的示例,例如,使用“back tick”(`)调用传递多个参数的方法: 但我想做的是,在多行代码中继续对象方法链链接,例如,我想在多行代码中分解它: $ps = [PowerShell]::Create() [ref]$e = New-Object System.Management.Automation.Runspaces.PSSnapInException $ps.Runspace.RunspaceConfiguration.AddP

我遇到了很多关于如何跨多行继续powershell脚本代码的示例,例如,使用“back tick”(`)调用传递多个参数的方法:

但我想做的是,在多行代码中继续对象方法链链接,例如,我想在多行代码中分解它:

$ps = [PowerShell]::Create()
[ref]$e = New-Object System.Management.Automation.Runspaces.PSSnapInException
$ps.Runspace.RunspaceConfiguration.AddPSSnapIn( "SqlServerCmdletSnapin100", $e ) | Out-Null
# This is ridiculous; how to I break the below code across multiple lines???
$ps.AddCommand( "Invoke-Sqlcmd").AddParameter( "InputFile", $PreCompareInfoObject.PreCompareBatchScript).AddParameter("serverinstance", $DBConnectInfoObject.DBDestServer).AddParameter("database", $DBConnectInfoObject.DBDestDB).AddParameter( "username", $DBConnectInfoObject.DBDestUserName).AddParameter("password",  $DBConnectInfoObject.DBDestPassword).AddParameter( "variable",  $variable).AddParameter("outputsqlerrors",  $true).AddParameter("abortonerror").AddParameter("Verbose") | out-file -filepath $PreCompareInfoObject.PreCompareOutputReport
$ps.Invoke()
我可以这样做,但我不想这样做,由于某些原因,输出比我希望的更详细:

...
$ps.AddCommand( "Invoke-Sqlcmd")
$ps.AddParameter( "InputFile", $PreCompareInfoObject.PreCompareBatchScript)
$ps.AddParameter("serverinstance", $DBConnectInfoObject.DBDestServer)
$ps.AddParameter("database", $DBConnectInfoObject.DBDestDB)
$ps.AddParameter( "username", $DBConnectInfoObject.DBDestUserName)
$ps.AddParameter("password",  $DBConnectInfoObject.DBDestPassword)
$ps.AddParameter( "variable",  $variable)
$ps.AddParameter("outputsqlerrors",  $true)
$ps.AddParameter("abortonerror")
$ps.AddParameter("Verbose") | out-file -filepath $PreCompareInfoObject.PreCompareOutputReport
...
我想我正在考虑这个,但当然,它不起作用:

$ps `
   .AddCommand( "Invoke-Sqlcmd") `
   .AddParameter( "InputFile", $PreCompareInfoObject.PreCompareBatchScript) `
   .AddParameter("serverinstance", $DBConnectInfoObject.DBDestServer) `
   .AddParameter("database", $DBConnectInfoObject.DBDestDB) `
   .AddParameter( "username", $DBConnectInfoObject.DBDestUserName)
任何帮助都将不胜感激

这样写:

$ps. 
 Addcommand("Invoke-SqlCmd"). 
 AddParameter("InputFile", $PreCompareInfoObject.PreCompareBatchScript).


也就是说,用点结束这一行,然后您可以继续下一行,而不使用反勾号。

您可以将所有或大部分参数存储在哈希表中,然后调用
GetEnumerator()
返回键/值对列表,如果愿意,可以运行
ForEach Object
将它们全部添加到
$ps
。这就是它的样子

$params = @{InputFile=$PreCompareInfoObject.PreCompareBatchScript
    "serverinstance"=$DBConnectInfoObject.DBDestServer
    "database"= $DBConnectInfoObject.DBDestDB
     "username"= $DBConnectInfoObject.DBDestUserName
    "password"=  $DBConnectInfoObject.DBDestPassword
     "variable"=  $variable
    "outputsqlerrors"= $true
    }

$params.GetEnumerator() | ForEach-Object {
    $ps.AddParameter($_.Key,$_.Value)
    }

我仍然会在自己的行上运行AddCommand()步骤,但您应该能够用这种方法处理大部分或所有参数。

您的示例应该可以运行-您会遇到什么错误?假设最后一个示例是可能的,则应该删除反勾号之前的尾随空格。嘿,你有机会试试我的方法吗?这不管用。我指出我使用的是Powershell 2.0,您在哪个版本上使用它/在哪个版本上测试它?这会起作用,但它实际上只不过是在循环外多次调用.AddParameter,谢谢。
$params = @{InputFile=$PreCompareInfoObject.PreCompareBatchScript
    "serverinstance"=$DBConnectInfoObject.DBDestServer
    "database"= $DBConnectInfoObject.DBDestDB
     "username"= $DBConnectInfoObject.DBDestUserName
    "password"=  $DBConnectInfoObject.DBDestPassword
     "variable"=  $variable
    "outputsqlerrors"= $true
    }

$params.GetEnumerator() | ForEach-Object {
    $ps.AddParameter($_.Key,$_.Value)
    }