【问题标题】:Executing a powershell script remotely远程执行powershell脚本
【发布时间】:2016-06-24 04:36:18
【问题描述】:

我有一个存储在服务器上的 powershell 脚本,并且需要某些用户能够发送命令以从他们的工作站运行此脚本。我已经进入服务器并运行命令以允许远程 powershell。下面是我正在运行的脚本以及我的错误的屏幕截图。

invoke-command { powershell.exe -noprofile -executionpolicy Bypass C:\script directory } -computername  -credential (domain).local\Administrator

【问题讨论】:

  • 格式有什么问题?
  • 复制/粘贴错误消息会很有帮助。一些公司网络阻止了 imgur.com,因此我们中的一些人看不到您的错误。
  • 您尝试远程进入机器以启用远程处理?
  • [server] 连接到远程服务器失败并显示以下错误消息:WinRM 无法处理请求。使用 Ker beros 身份验证时发生以下错误:当前没有可用于为登录请求提供服务的登录服务器。可能的原因有: - 指定的用户名或密码无效。 -当没有指定身份验证方法和用户名时使用 Kerberos。 .
  • -更改认证方式;将目标计算机添加到 WinRM TrustedHosts 配置设置或使用 HTTPS 传输。请注意,TrustedHosts 列表中的计算机可能未经过身份验证。 - 有关 WinRM 配置的更多信息,请运行以下命令:winrm help config。有关详细信息,请参阅 about_Remote_Troubleshooting 帮助主题。 + CategoryInfo : OpenError: (:) [], PSRemotingTransportException + FullyQualifiedErrorId : PSSessionStateBroken

标签: powershell batch-file


【解决方案1】:

您必须执行一些调整才能运行远程 powershell。可能有些规则不适用。

  1. 启用 ps 远程处理

powershell -noprofile -command Enable-PsRemoting -Force

  1. 启用 wimRm 服务(Windows 远程管理)

sc config "WinRM" start= delayed-auto

sc start "WinRM"

  1. 为 winRm 服务添加防火墙规则

netsh advfirewall firewall set rule name="Windows Remote Management (HTTP-In)" profile=domain new enable=yes

netsh advfirewall firewall set rule name="Windows Remote Management (HTTP-In)" profile=private,public new enable=yes profile=private,public

  1. 查找 WS-Management TrustedHost 内容

winrm g winrm/config/client

你会得到类似的东西

    Client
      NetworkDelayms = 5000
      URLPrefix = wsman
      AllowUnencrypted = false
    Auth
      Basic = true
      Digest = true
      Kerberos = true
      Negotiate = true
      Certificate = true
      CredSSP = false
    DefaultPorts
      HTTP = 5985
      HTTPS = 5986
    TrustedHosts = MyComputer
  1. 更新受信任主机列表以适应允许连接的工作站

winrm s winrm/config/client @{TrustedHosts="server,computer01,computer02,etc"}

你可以检查它是否适用

winrm quickconfig

我想就是这样。希望对你有帮助

【讨论】:

  • 尝试添加工作站时出现此错误 PS C:\Users\Administrator> winrm s winrm/config/client @{TrustedHosts="DM30167"} 错误:命令行使用无效。输入“winrm -?”寻求帮助。
  • @user326334 我发布了一个新答案,因为我在 cmets 格式化方面遇到了困难。希望有帮助
  • 好的,我完成了所有这些,尝试从添加到受信任主机的机器上远程运行脚本,但仍然遇到相同的错误。下面是我正在使用的脚本。调用命令 { powershell.exe -noprofile -executionpolicy 绕过 C:\Applicat\OCI Software\permScript.ps1 } -computername server -credential domain.local\Administrator Read-Host -Prompt "exit"
【解决方案2】:

@user326334

我发布另一个答案是因为我需要澄清自己。也许我完全误解了你,我需要查看错误的确切描述。

但是有些cmets...

Invoke-command 可以直接运行 powershell 命令。

您正在调用的脚本在路径中包含空格,请考虑引用。

我可能会错过一些东西,但我认为您将 Invoke-Command 参数powershell cmdLets 作为 -credential... 混合使用>read-host -Prompt...

试试这个,让我知道发生了什么

$s = New-PSSession -computer "server" -credential "domain.local\Administrator"

Invoke-Command -Session $s -ScriptBlock { ... }

例如,这对我来说非常适合

$s = New-PSSession -computer "LENOVO" -credential "lenovo\administrador"
Invoke-Command -Session $s -ScriptBlock { ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal‌​.WindowsBuiltInRole]::Administrator) }
Invoke-Command -Session $s -ScriptBlock { cmd /c "sc query remoteregistry"; $lastexitcode } 
Invoke-Command -Session $s -ScriptBlock { Get-WSManInstance -ResourceURI winrm/config/client | select -ExpandProperty TrustedHosts}

如果仍然有问题,可能是您正在使用的身份验证模式。

请访问thisthis了解更多信息

【讨论】:

    猜你喜欢
    • 2022-10-12
    • 2020-11-12
    • 1970-01-01
    • 1970-01-01
    • 2012-05-30
    • 2013-10-15
    • 1970-01-01
    • 1970-01-01
    • 2011-07-02
    相关资源
    最近更新 更多