我在我的Windows 7机器上试试这个
Get-Service –ComputerName myserver
我回来了
Get-Service : Cannot open Service Control Manager on computer 'myserver'. This operation might require other privileges. At Script1.ps1:2 char:4 + gsv <<<< -cn myserver + CategoryInfo : NotSpecified: (:) [Get-Service],InvalidOperationException + FullyQualifiedErrorId : system.invalidOperationException,Microsoft.PowerShell.Commands.GetServiceCommand
在搜索时我发现我应该尝试使用Enable-PSRemoting.
我这样做了,现在当我尝试使用它时,我得到了
WinRM already is set up to receive requests on this machine. WinRM
already is set up for remote management on this machine.
但我仍然得到同样的错误.这是因为我使用的是虚拟机吗?我将虚拟操作系统设置为我的域,我甚至可以使用我的AD帐户凭据登录.
我可以从中获取其他信息.
所以我不能用PowerShell连接它.
解决方法
内置远程处理的命令:
PowerShell v2中的一小组命令具有-ComputerName参数,该参数允许您指定要访问的目标计算机.
Get-Process Get-Service Set-Service Clear-EventLog Get-Counter Get-EventLog Show-EventLog Limit-EventLog New-EventLog Remove-EventLog Write-EventLog Restart-Computer Stop-Computer Get-HotFix
这些命令可以自行进行远程处理,因为底层基础架构已经支持远程处理,或者它们解决了对系统管理特别重要的场景.它们构建在DCOM的顶部,在访问角度,当您可以使用NET.exe或PSExec.exe等命令与远程计算机建立会话时,可以使用它们.
您正在尝试使用其中一个并且您的凭据(-cred参数)存在问题,因为您的令牌凭据不能用于建立与远程计算机的管理会话.
PowerShell远程子系统:
在使用PowerShell远程处理访问远程计算机之前,必须明确启用该计算机上的远程处理服务.您可以使用Enable-PSRemoting cmdlet执行此操作.如果您在工作组中工作,还需要使用此命令启用服务器进入客户端计算机(在客户端计算机上以管理员身份):
Set-Item WSMan:\localhost\Client\TrustedHosts *
然后,您将使用New-PSSession Cmdlet(带-computername和-credentials)来创建会话对象.然后,Invoke-Command(带-session和-scriptblock)cmdlet允许您远程调用另一台计算机上的scriptblock.这是远程处理中大多数功能的基本元素.您还可以使用Enter-PSSession与服务器建立交互式(类似SSL)PowerShell命令行.
有用链接:Layman’s guide to PowerShell 2.0 remoting
测试一下:
$sess = New-PSSession -ComputerName myServer-Credential (Get-Credential) Invoke-Command -Session $sess -ScriptBlock {get-service} ... Remove-PSSession -Session $sess