调用命令localhost访问被拒绝

时间:2019-10-28 16:15:52

标签: powershell powershell-remoting invoke-command

在尝试对本地主机调用命令时,访问被拒绝。

我已经确认启用了PS远程处理,并且该帐户是Administrator。此外,从远程计算机进行远程处理可以正常工作。

Invoke-Command -computername LocalHost -scriptblock {hostname} 

我希望返回本地计算机的主机名,但是我收到访问被拒绝的错误。

1 个答案:

答案 0 :(得分:0)

启用PSRemoting服务以自动启动

在主机和远程计算机上

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

参考

https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_remote_troubleshooting?view=powershell-6

相关问题