Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't use Get-Service –ComputerName on remote computer

I have a windows 2003 box setup with virtual box and I can't powershell to work with it.

I try this on my windows 7 machine

Get-Service –ComputerName myserver

I get back

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

While searching around I found I should try and use Enable-PSRemoting.

I did this and now when I try to use it I get

WinRM already is set up to receive requests on this machine. WinRM already is set up for remote management on this machine.

Yet I still get the same error. Is this because I am using a virtual machine? I setup the virtual OS to be on my domain and I can even use my AD account credentials to log in.

I can get other information back from it.

So it is not like I can't connect to it with powershell.

like image 785
chobo2 Avatar asked May 24 '12 20:05

chobo2


1 Answers

With PowerShell V2 you've got two approachs for remote commands.

Commands with built-in remoting :

A small set of commands in PowerShell v2 have a -ComputerName parameter, which allows you to specify the target machine to access.

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

These commands do their own remoting either because the underlying infrastructure already supports remoting or they address scenarios that are of particular importance to system management. They are built on the top of DCOM and, on the access point of view, you can use them when you can establish a session with the remote machine with commands like NET.exe or PSExec.exe.

You are trying to use one of them and you've got a problem with credentials (-cred parameter), because your token credentials can't be used to establish an admin session to the remote machine.

The PowerShell remoting subsystem :

Before you can use PowerShell remoting to access a remote computer, the remoting service on that computer has to be explicitly enabled. You do so using the Enable-PSRemoting cmdlet. If you are working in workgroup you also need to enable the server to enter on your client computer with this command (on your client computer as administrator):

Set-Item WSMan:\localhost\Client\TrustedHosts *

Then, you will use New-PSSession Cmdlet (with -computername and -credentials) to create a session object. Then Invoke-Command (with -session and -scriptblock) cmdlet allows you to remotely invoke a scriptblock on another computer. This is the base element for most of the features in remoting. You can also use Enter-PSSession to establish an interactive (SSL like) PowerShell command line with the server.

Useful link : Layman’s guide to PowerShell 2.0 remoting


Test this :

$sess = New-PSSession -ComputerName myServer-Credential (Get-Credential)
Invoke-Command -Session $sess -ScriptBlock {get-service}
...
Remove-PSSession -Session $sess
like image 112
JPBlanc Avatar answered Nov 15 '22 07:11

JPBlanc