locked
Remote Powershell wmi Start Service RRS feed

  • Question

  • Hi

    I am running into the following error running the following script:

    $Server = "ServerName"
    $Service = "Print Spooler"
    $MyDomain=’Domain’ 
    $MyClearTextUsername=’srvpowershell’ 
    $MyClearTextPassword=’pasword’
    $MyUsernameDomain=$MyDomain+’\’+$MyClearTextUsername
    $SecurePassword=Convertto-SecureString –String $MyClearTextPassword –AsPlainText –force
    $MyCredentials=New-object System.Management.Automation.PSCredential $MyUsernameDomain,$SecurePassword
    
    $Session = New-PSSession -ComputerName $Server -Credential $MyCredentials
    
    if(-not($Session))
    {
        Write-Warning "$Server inaccessible!"
    }
    else
    {
        Write-Host "Great! $Server is accessible!"
        Get-Service -Name $Service -ComputerName $Server | Set-Service -Status Running
        $wshell = New-Object -ComObject Wscript.Shell
        $wshell.Popup("Starten $Service Voltooid",0,"Done",0x1)
       
    }

    I am getting the following error:

    The server I want to connect to is a server 2012 r2

    The server where I run the script from is a remote desktop server 2008 r2

    I ran the following guide:

    https://helpcenter.gsx.com/hc/en-us/articles/202447926-How-to-Configure-Windows-Remote-PowerShell-Access-for-Non-Privileged-User-Accounts

    Hope some one can help me solve this issue, Thanks in advanced.

    Greetings  Roel Knippen

    Tuesday, June 20, 2017 6:20 AM

Answers

  • You are not using a remote session even though you created one.  Access is denied because your account is not an administrator.

    This is how to do a single remote command:

    Invoke-Command -ComputerName $Server -Credential $MyCredentials -ScriptBlock {
    		Set-Service -Name 'Print Spooler' -Status Running
    }

    help about_remote

    Start by learning how to use PowerShell remoting.  You cannot do this by guesswork.


    \_(ツ)_/

    • Marked as answer by Roel Knippen Wednesday, June 21, 2017 5:59 AM
    Tuesday, June 20, 2017 6:39 AM

All replies

  • You are not using a remote session even though you created one.  Access is denied because your account is not an administrator.

    This is how to do a single remote command:

    Invoke-Command -ComputerName $Server -Credential $MyCredentials -ScriptBlock {
    		Set-Service -Name 'Print Spooler' -Status Running
    }

    help about_remote

    Start by learning how to use PowerShell remoting.  You cannot do this by guesswork.


    \_(ツ)_/

    • Marked as answer by Roel Knippen Wednesday, June 21, 2017 5:59 AM
    Tuesday, June 20, 2017 6:39 AM
  • Hi,

    Be careful with Start/Stop/Restart-Service  cmdlets. They are working with remote computer in a different way depends on PS version. As example in v2 (installed in win7) they will work with local service with the same name instead of remote. Also commands with -Computername parameter use in most cases DCOM, not a PS Session.

    If you need list of commands that can be used with PSSession, then you can use the following code:

    get-command * -CommandType Cmdlet | ?{$_.Parameters.Keys | ?{$_ -eq "Session"} }

    As it was suggested by jrv, Invoke-command is your option here to run command remotely.

    Sergei.

    Tuesday, June 20, 2017 9:24 AM

  • Actually Get-Service use the Service Controller API.

    [System.ServiceProcess.ServiceController]::GetDevices('remotepc')

    See: https://msdn.microsoft.com/en-us/library/system.serviceprocess.servicecontroller(v=vs.110).aspx

    This uses the SC management ports and not DCOM.  It always executes in the context of the current user account.

    To remote with Get-Service only Invoke-Command will work correctly  There is no need for an explicit session as the command takes credentials.

    Wmi/Cim are more direct:

    $service =  Get-WmiObject Win32_Service -Filter "DisplayName='Print Spooler'"
    $service.StartService()

    Or:

    Invoke-CimMethod -Query "Select * From Win32_Service where DisplayName = 'Print Spooler'" -MethodName StartService -CimSession $session


    \_(ツ)_/




    • Edited by jrv Tuesday, June 20, 2017 9:53 AM
    • Proposed as answer by Hello_2018 Wednesday, June 21, 2017 5:37 AM
    Tuesday, June 20, 2017 9:52 AM