在开发团队中,我希望开发人员在本地或我们的测试平台远程执行相同的测试脚本。
这是我想用作每个脚本的前提
# Test local/remote execution by reading C:\ directory
param(
[switch] $verbose,
[switch] $remote,
[string] $ip,
[string] $user,
[string] $password
#Add here script specific parameters
)
Write-Host "Command invokation incoming parameter count : " $psboundparameters.count
if ($remote) {
$Params = @{}
$RemoteParams = @{}
$pass = ConvertTo-SecureString -String $password -AsPlainText -Force
$Params.Credential = new-object -TypeName System.management.automation.PSCredential -argumentlist $user, $pass
$Params.ComputerName = $ip
$Params.FilePath = $MyInvocation.MyCommand.Name
$null = $psboundparameters.Remove('remote')
$null = $psboundparameters.Remove('ip')
$null = $psboundparameters.Remove('user')
$null = $psboundparameters.Remove('password')
foreach($psbp in $PSBoundParameters.GetEnumerator())
{
$RemoteParams.$($psbp.Key)=$psbp.Value
}
Write-Host $RemoteParams
Invoke-Command @Params @Using:RemoteParams
Exit
}
Write-Host "Command execution incoming parameters count : " $psboundparameters.count
# Here goes the test
Get-ChildItem C:\
但是,当我执行此操作时,出现以下错误:
Invoke-Command : A positional parameter cannot be found that accepts argument '$null'.
似乎@Using:RemoteParams不是这样做的正确方法,但我在这里很迷茫。提前致谢