我正在尝试运行以下Invoke-Command,该命令将在远程计算机上使用“ remove”参数运行rabbitmq-service.bat。该命令通过删除rabbmitmq-service可以完美执行,但出现以下错误“目录名称无效”。
$Cre = Get-Credential -Message "Enter credentials for $fo1"
$fo1Session = New-PSSession -ComputerName Dedicated-Test1 -Credential $Cre
$action = "C:\Program Files\RabbitMQ Server\rabbitmq_server-3.7.10\sbin\rabbitmq-service.bat"
$remove = "remove"
PS C:\WINDOWS\system32> Invoke-Command -Session $fo1Session -ScriptBlock {& $using:action $using:remove -verb runAs}
The directory name is invalid.
+ CategoryInfo : NotSpecified: (The directory name is invalid.:String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError
+ PSComputerName : Dedicated-Test1
The filename, directory name, or volume label syntax is incorrect.
C:\Program Files\erl10.2\erts-10.2\bin\erlsrv: Service RabbitMQ removed from system.
您可以在不使用$ using的情况下使用此代码:
$batch = get-content $action -raw
invoke-command -computername TheRemoteComputer -session $fo1session -scriptblock "& {new-item -path 'C:\Batch.Bat' -type file -value $batch; cmd.exe /c 'C:\Batch.bat $remove'; remove-item 'C:\Batch.bat'}"
用远程计算机的名称替换TheComputerName。