[当尝试对本地主机调用命令时,我拒绝访问。
我已经确认启用了PS远程处理,并且该帐户是Administrator。此外,从远程计算机进行远程处理可以正常工作。
Invoke-Command -computername LocalHost -scriptblock {hostname}
我希望返回本地计算机的主机名,但是我收到访问被拒绝的错误。
在主机和远程计算机上]
Set-Service winrm -StartupType Automatic Start-Service winrm
启用PSREmoting
在主机和远程计算机上]
EnablePSRemoting -Force
将计算机添加到受信任的主机
在远程计算机上
Set-Item wsman:\localhost\Client\TrustedHosts -Value "$(hostname),*$((Get-WmiObject Win32_ComputerSystem).Domain)"
在Powershell远程处理中启用多跳]
确定允许哪些主机通过Creds
Enable-WSManCredSSP –Role Client –DelegateComputer "$(hostname),*$((Get-WmiObject Win32_ComputerSystem).Domain)"
在源计算机上。
Enable-WSManCredSSP –Role Server
您必须指定身份验证和凭据
在主机上]
$Cred = [System.Management.Automation.PSCredential]::new("<username>",$("<Password>" | ConvertTo-SecureString -AsPlainText -Force)) invoke-command -ComputerName localhost -ScriptBlock {Write-Host $args[0]} -ArgumentList "Hello!, It Works" -Authentication Credssp -Credential $cred
参考