2
votes

I'm writing a webservice with PowerShell commands where I want to start and stop services on the local computer and also on remote computer.

It's not a problem to start and stop the services on remote computers. I do this with an WmiObject as you can see below.

If I want to start a local service it says that I don't have the permissions.

I can't use an WmiObject with Credentials if I want to start an local service.

What can I do to start the service with admin rights?

My Script (strScriptText):

$username = "domain\administrator"
$pw = convertto-securestring "password" -asplaintext -force
$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $username, $pw
$computername = "serverAB"
if ( $computername.Contains("serverAB")){(Get-WmiObject -class Win32_Service -filter "name='AppIDSvc'").startservice().returnvalue}
else {(Get-WmiObject -class Win32_Service -ComputerName $computername -Credential $cred -filter "name='AppIDSvc'").startservice().returnvalue}

vb:

 runspace = RunspaceFactory.CreateRunspace()
        runspace.Open()
 pipeline = runspace.CreatePipeline()

pipeline.Commands.AddScript(strScriptText)
                pipeline.Commands.Add("Out-String")
1

1 Answers

0
votes

Can't you try to use the old .NET method through PowerShell.

# Create an authentication object
$ConOptions = New-Object System.Management.ConnectionOptions
$ConOptions.Username = "dom\jpb"
$ConOptions.Password = "pwd"
$ConOptions.EnablePrivileges = $true
$ConOptions.Impersonation = "Impersonate"
$ConOptions.Authentication = "Default"

# Creation of a rmote or local process
$scope = New-Object System.Management.ManagementScope("\\dom.fr\root\cimV2", $ConOptions)
$ObjectGetOptions = New-Object System.Management.ObjectGetOptions($null, 
                                                          [System.TimeSpan]::MaxValue, $true)
$proc = New-Object System.Management.ManagementClass($scope, 
                                      "\\dom.fr\ROOT\CIMV2:Win32_Process", $ObjectGetOptions)

# Equivalent to : 
# $proc = [wmiclass]"\\.\ROOT\CIMV2:Win32_Process"
# $res = $proc.Create("cmd.exe")