簡體   English   中英

遠程執行Powershell腳本

[英]Executing a powershell script remotely

我有一個存儲在服務器上的powershell腳本,需要某些用戶能夠發送命令從其工作站運行此腳本。 我已經進入服務器並運行命令以允許遠程Powershell。 以下是我正在運行的腳本以及錯誤的屏幕截圖。

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

錯誤信息

您必須執行一些調整才能運行遠程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

我認為就這些。 希望能幫助到你

@ user326334

我要發表另一個答案,因為我需要澄清自己。 也許我完全誤解了您,並且我需要查看錯誤的確切描述。

但是一些評論...

調用命令可以直接運行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}

如果仍然有問題,則可能是您使用的身份驗證模式。

請訪問以獲取更多信息

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM