Invoke-Command powershell麻烦

时间:2013-08-07 14:47:56

标签: function powershell invoke-command

朋友我遇到了这个功能的问题,它会在远程服务器上运行,但我有以下输出:

Invoke-Command:找不到接受参数'&的位置参数C:\ testNunit \ DLL \”。 在D:\ test \ Multithread.ps1:65 char:16 + Invoke-Command<<<< -ComputerName $ serv -ScriptBlock $ command([ScriptBlock] :: Create(“& $ OneProject”))--credential $ cred     + CategoryInfo:InvalidArgument:(:) [Invoke-Command],ParameterBindingException     + FullyQualifiedErrorId:PositionalParameterNotFound,Microsoft.PowerShell.Commands.InvokeCommandCommand

  function Nunit  { 

##Parse connection parameters
$Connection = @{"server" = "..."; "username" = "..."; "password" = "...."}

$serv = $connection.Get_Item("server")
$user = $connection.Get_Item("username")
$pass = $connection.Get_Item("password")

$securePassword = ConvertTo-SecureString -AsPlainText $pass -Force

#Create connection credentials object for Invoke-Command
$cred   = new-object -typename System.Management.Automation.PSCredential -argumentlist $user, $securePassword

$NunitExe = "C:\testNunit\bin\nunit-console.exe"
   $OneProject = "C:\testNunit\dll\Foundation.Tests.dll"
$TestProjects = "C:\testNunit\dll\"
foreach( $OneProject in ( $TestProjects))
{
 $WorkingDir = "c:\testNunit"
 $NUnitOutput = "c:\testNunit" + $OneProject + ".xml"

 $command = {&"$NunitExe" "$WorkingDir\$OneProject" \noshadow/framework:"net-3.5" /xml:$NUnitOutput}

 }
    Invoke-Command -ComputerName $serv -ScriptBlock  $command  ([ScriptBlock]::Create("& $OneProject")) -credential $cred 

          }

2 个答案:

答案 0 :(得分:1)

在-Scriptblock参数之后,您指定了两个脚本块 - 一个在$ command中,另一个在Create中。应该只为参数提供一个值。

答案 1 :(得分:0)

有几个问题。首先,您需要将一个参数传递给脚本块,基本上有两个。其次,您需要在-ArgumentList中将变量作为参数传入,否则,scriptblock将无法识别它们。

试试这个:

    Invoke-Command -ComputerName $serv -ScriptBlock  {
     $command = args[0]
     $OneProject = args[1]
 $command & $OneProject} -ArgumentList @($command, $OneProject) -credential $cred