【问题标题】:Accessing cmd remotely using PowerShell使用 PowerShell 远程访问 cmd
【发布时间】:2021-09-12 03:41:07
【问题描述】:

我需要使用在我的计算机上运行的 PowerShell 脚本在远程计算机的 cmd 上运行几个 bcdedit 命令。我能够创建一个 PSSession,但我不确定如何在远程计算机上运行 cmd。当我在“Invoke-Command”行中运行代码时,我收到错误Connection to remote server failed with the following error message: Access is denied. 当我刚刚运行 Invoke-Command 时,系统提示我输入 ScriptBlock,但是当我这样做时,我又收到另一个错误:“不能绑定参数 'ScriptBlock' 无法将 System.String 类型的 "cmd /c 'bcdedit /copy {current} /d "Description"'} 值转换为 System.Management.Automation.ScriptBlock 类型

我以前从未使用过 PowerShell。我需要在几个小时内完成这项工作,而我现在完全一无所知。

Enable-PSRemoting -Force
Set-Item WSMan:\localhost\Client\TrustedHosts $ip -Concatenate -Force
$session = New-PSSession -ComputerName $ip -Credential $cred -ConfigurationName $config -UseSSL -SessionOption $sessopt

#problematic code
Invoke-Command -ComputerName $ip -ScriptBlock {cmd /c 'bcdedit /copy {current} /d "Description"'}

#works fine
Restart-Computer -ComputerName $ip -Force
ping.exe -t $ipaddr | Foreach{"{0}-{1}" -f (Get-Date -f "yyyy/MM/dd HH:mm:ss"), $_}

假设 $ip、$ipaddr、$config、$sessopt 和 $cred 存储有效参数。

【问题讨论】:

  • 当您可以在 powershell 中执行此操作时,为什么还要调用 cmd? -ScriptBlock { bcdedit /copy {current} /d "Description" }
  • 因为 bcdedit /copy {current} /d "Description" 在 powershell 中不起作用。它只适用于 cmd
  • 应该可以,powershell显然可以运行任何exe文件。但可能你需要引用{},因为这些是特殊字符:-ScriptBlock { bcdedit /copy {current}` /d "Description" }`
  • 很高兴您发现了您的特定远程问题,如您自己的回答中所解释的那样,但由于该解决方案特定于您的环境 - 并且只是一个 i> 基于您问题中描述的症状的可能解决方案 - 这并不是那么有帮助(并且需要将 -Credential 参数传递给 Restart-Computer 也与您的问题相矛盾)。因此,我鼓励您accept 提供一般背景和故障排除信息的答案。

标签: powershell cmd powershell-remoting cmdlet


【解决方案1】:
  • 可以直接在PowerShell中运行bcedit.exe,但是因为在PowerShell中{}元字符,你需要引用 em> 标识符如{current}:

    • bcdedit /copy '{current}' /d 'Description'
    • 有关 PowerShell 元字符的讨论和列表,请参阅 this answer
  • 如果您在连接到远程计算机时遇到错误,这意味着您的用户帐户没有足够的权限来远程连接目标计算机未设置 PowerShell 远程处理

    • 请注意,Enable-PSRemoting -Force 必须在目标(服务器)机器上运行,而不是在调用(客户端)机器上。

    • 请参阅概念性 about_Remote_Troubleshooting 主题。

    • Restart-Computer cmdlet 的 -ComputerName 参数使用 PowerShell 远程处理,所以它成功的事实暗示 PowerShell 远程处理,例如通过 @987654324 @,工作。

当我刚刚运行 Invoke -Command 时,系统会提示我输入 ScriptBlock

PowerShell 对未在命令行中指定的 强制 参数值的自动提示功能具有严重的限制,并且无法提示输入 script-block 参数值就是其中之一 - 请参阅GitHub issue #4068;然而,这个额外的问题是偶然你真正的问题。

【讨论】:

  • 不幸的是,我仍然无法让我的 Invoke-Command 工作,并且仍然给我“拒绝访问”错误。奇怪的是我能够运行 Enter-PSSession 并远程访问同一台计算机。 Invoke-Command 有替代方案吗?
  • 另外,除了我用的那个,有没有办法让cmd远程运行?我认为我的经理希望我使用它
  • @pesky_programmer cmd 很久以前就具有显着的 DOS 兼容性,因此它的功能非常有限。它无法远程运行命令,您必须使用一些 3rd 方解决方案,例如 psexec。 PowerShell OTOH 基于 .NET,可以做任何 .NET 可以做的事情。这些天只是避免使用 cmd
  • @pesky_programmer,这里不需要涉及cmd.exe,和你的问题无关。相反,您的 PowerShell 远程设置似乎有问题。 phuclv 提到的psexec 实用程序是一种替代方法,可能为您工作而无需额外设置,但您必须先安装它(在调用计算机上)。
  • 为了完整起见,一个类似于 PowerShell 的 cmd.exe 远程解决方案 - 基于 winrs.exeWindows Remote Management - 但它的要求类似于 PowerShell 的,因此您不妨设置 PowerShell 的远程处理,它为您提供了更多功能。 /cc @phuclv.
【解决方案2】:

感谢所有建议,我能够通过将 -Credential 添加到 Invoke-Command 和 Restart-Computer 命令来修复错误:

#problematic code
Invoke-Command -ComputerName $ip -Credential $cred -ScriptBlock {cmd /c 'bcdedit /copy {current} /d "Description"'}
Restart-Computer -ComputerName $ip -Credential $cred -Force

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-25
    • 2017-10-29
    • 1970-01-01
    • 2014-06-27
    相关资源
    最近更新 更多