【问题标题】:Power shell Invoke-Command NotSpecified: (The directory name is invalid.:String) [], RemoteException errorPower shell Invoke-Command NotSpecified: (目录名无效.:String) [], RemoteException 错误
【发布时间】:2019-12-19 16:15:24
【问题描述】:

我正在尝试运行以下 Invoke-Command,它将在远程计算机上运行 rabbitmq-service.bat,并带有“remove”参数。该命令在删除 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 的值是多少?
  • 我使用 $using 作为本地变量在远程机器上执行。
  • 是否包含计算机列表?
  • 我需要在远程机器上使用“remove”参数运行“C:\Program Files\RabbitMQ Server\rabbitmq_server-3.7.10\sbin\rabbitmq-service.bat”。任何示例代码都会对我有所帮助。我确定远程机器上的路径没问题。
  • @WasifHasan 不,我在 $fo1Session 中只有一台远程计算机。

标签: powershell invoke-command remoteexception


【解决方案1】:

您可以在不使用 $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 替换为远程计算机的名称。

【讨论】:

  • $batch = get-content $action -raw invoke-command -computername Dedicated-Test1 -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'}" 调用命令:无法绑定参数 'ScriptBlock'。无法转换“& {new-item -path 'C:\Batch.Bat' -type file -value @echo off
猜你喜欢
  • 2017-04-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-18
相关资源
最近更新 更多