简体   繁体   English

远程执行Powershell脚本

[英]Executing a powershell script remotely

I have a powershell script stored on a server and need certain users to be able to send a command to run this script from their workstations. 我有一个存储在服务器上的powershell脚本,需要某些用户能够发送命令从其工作站运行此脚本。 I have went onto the server and run the command to allow remote powershell. 我已经进入服务器并运行命令以允许远程Powershell。 Below is the script I am running along with a screen shot of my error. 以下是我正在运行的脚本以及错误的屏幕截图。

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

错误信息

you must perform some tuning to run remote powershell. 您必须执行一些调整才能运行远程Powershell。 may be some of these rules doesn't apply. 可能其中一些规则不适用。

  1. enable ps-remoting 启用ps远程处理

powershell -noprofile -command Enable-PsRemoting -Force

  1. enable wimRm service (Windows remote administration) 启用wimRm服务(Windows远程管理)

sc config "WinRM" start= delayed-auto

sc start "WinRM"

  1. add firewall rules for winRm service 为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. lookup WS-Management TrustedHost content 查找WS-Management TrustedHost内容

winrm g winrm/config/client

you will get something like 你会得到类似

    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. update list of trusted hosts to fit the workstations allowed to connect 更新可信主机列表以适合允许连接的工作站

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

you can check if it works with 您可以检查它是否适用于

winrm quickconfig

I think that's all. 我认为就这些。 hope it helps 希望能帮助到你

@user326334 @ user326334

I post another answer because I need to clarify myself. 我要发表另一个答案,因为我需要澄清自己。 perhaps I completely misunderstood you, and I need to view exact description of error. 也许我完全误解了您,并且我需要查看错误的确切描述。

but some comments... 但是一些评论...

Invoke-command may run powershell commands directly. 调用命令可以直接运行powershell命令。

the script you're calling contains spaces in path, consider quoting. 您正在调用的脚本的路径中包含空格,请考虑使用引号。

I may miss something but I think you are mixing Invoke-Command parameters with powershell cmdLets as -credential... with read-host -Prompt... 我可能会错过一些东西,但我认为您正在将Invoke-Command参数powershell cmdLets混合-credential ...read-host -Prompt ...

try this and let me know what's happening 试试这个,让我知道发生了什么

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

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

for instance, this work perfectly for me 例如,这对我来说非常合适

$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}

if still in trouble, may be the authentication mode you're using. 如果仍然有问题,则可能是您使用的身份验证模式。

please visit this and this for further information 请访问以获取更多信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM