[英]Executing a powershell script remotely
您必須執行一些調整才能運行遠程Powershell。 可能其中一些規則不適用。
powershell -noprofile -command Enable-PsRemoting -Force
sc config "WinRM" start= delayed-auto
sc start "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
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
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.