InvalidOperation: (:) [Start-Process], InvalidOperationException

InvalidOperation: (:) [Start-Process], InvalidOperationException

我收到以下错误:

This command cannot be run due to the error: The system cannot find the file specified. + CategoryInfo : InvalidOperation: (:) [Start-Process], InvalidOperationException + FullyQualifiedErrorId : InvalidOperationException,Microsoft.PowerShell.Commands.StartProcessCommand + PSComputerName : XXXXXX.yyy.com

这是代码片段:

if (($ToolsStatus -eq 'toolsOk' -OR $ToolsStatus -eq 'toolsOld') -AND $VMVSSStatus -eq $null -AND $OperatingSystem -match "64-bit" ) {
            try{
                Copy-Item -Path ".bit$ToolsVersion.exe" -Destination "\$FQDN\c$\"  -Container -Recurse -Force -ErrorAction Stop
                "File $ToolsVersion.exe copied on $vmName" | Do-Log
                try {
                    Invoke-Command -ComputerName $FQDN -ScriptBlock { 
                        Start-Process "C:$ToolsVersion.exe" -ArgumentList '/s /v "/qn reboot=r ADDLOCAL=all"' -Wait 
                        Start-Process "C:$ToolsVersion.exe" -ArgumentList '/s /v "/qn reboot=r REMOVE=VSS"' -Wait
                        Start-Process "C:$ToolsVersion.exe" -ArgumentList '/s /v "/qn reboot=r ADDLOCAL=VSS"' -Wait 
                        "Installation completed on $vmName" | Do-Log
                        taskkill /IM vmtoolsd.exe /F
                        "VMtools process killed on $vmName" | Do-Log
                        Start-Service -Name VMTools
                        "VMware Tools service was started on $vmName" | Do-Log
                    } 
                }
                catch [System.Management.Automation.RuntimeException]#PSRemotingTransportException
                {
                     "Unable to install on $vmName. Following error was encountered:" +$_.Exception.GetType().FullName | Do-Log
                }

请帮忙。

我猜 $ToolsVersion 变量在 Invoke-Command 的范围内未定义,因为范围是在远程机器上执行的。

尝试:

Start-Process "C:$Using:ToolsVersion.exe" -ArgumentList '/s /v "/qn reboot=r ADDLOCAL=all"' -Wait 
            

相反。

来自about_Remote_Variables

USING LOCAL VARIABLES

You can also use local variables in remote commands, but you must indicate that the variable is defined in the local session.

Beginning in Windows PowerShell 3.0, you can use the Using scope modifier to identify a local variable in a remote command. ...

更新 1:

要从远程检索日志,您必须将代码更改为:

 Invoke-Command -ComputerName $FQDN -ScriptBlock { 
                    $computerName = $env:COMPUTERNAME
                    Start-Process "C:$ToolsVersion.exe" -ArgumentList '/s /v "/qn reboot=r ADDLOCAL=all"' -Wait 
                    Start-Process "C:$ToolsVersion.exe" -ArgumentList '/s /v "/qn reboot=r REMOVE=VSS"' -Wait
                    Start-Process "C:$ToolsVersion.exe" -ArgumentList '/s /v "/qn reboot=r ADDLOCAL=VSS"' -Wait 
                    $logging =  @()
                    $logging = "$computerName: Installation completed on $vmName"
                    taskkill /IM vmtoolsd.exe /F
                    $logging += "$computerName: VMtools process killed on $vmName" 
                    Start-Service -Name VMTools
                    $logging += "$computerName: VMware Tools service was started on $vmName" 
                    $logging
                } | Do-Log

此处数组 $logging 将通过网络序列化。在你的机器上,它被反序列化,数组的每个条目都被发送到你的本地管道。

希望对您有所帮助。