0

I want to set the windows service to run as a particular user(admin). This is my first attempt.

$myArgs = 'config "service22" start= demand obj= "'+$machineName+'\'+$userName+'" password= "'+$userPassword+'"'
Start-Process -FilePath sc.exe -ArgumentList $myArgs

I also tried this by changing the position of the variable many times(all places tried)

$svc = Get-WmiObject win32_service -filter "name='service22'"
$svc.change($null,$null,$null,$null,$null,$null,$null,$null,$null,$userName,$userPassword)

Nothing works with scripts but manually I could set it.

Blue Clouds
  • 7,295
  • 4
  • 71
  • 112
  • [You may want to look at this](https://stackoverflow.com/questions/34086780/powershell-start-process-with-other-user-credential-and-wait) as far as i can see you call Start-Process the wrong way – crdy Jan 26 '21 at 08:41
  • I dont see anything wrong with your second query, did you stopped and started the service for the changes to take effect – TheGameiswar Jan 26 '21 at 08:47
  • Does this return anything `Get-WmiObject win32_service -filter "name='service22'"` – TheGameiswar Jan 26 '21 at 08:49
  • this is what it return when I do write-host: \\vmname\root\cimv2:Win32_Service.Name="service22" – Blue Clouds Jan 26 '21 at 09:11

2 Answers2

1

No need to overcomplicate this.

Just:

Invoke-Command -ComputerName computername -ScriptBlock {
    Set-Service -Name servicename -Credential (Get-Credential user@domain.com)
}

PS: Set-Service have Credential parameter in powershell v7 (tested), non in Windows Powershell

BeerRider
  • 317
  • 2
  • 7
0

got the tip from here

just had to add type=own at the end

$myArgs = 'config "service22" start= demand obj= "'+$machineName+'\'+$userName+'" password= "'+$userPassword+'" type=own'
Start-Process -FilePath sc.exe -ArgumentList $myArgs
Blue Clouds
  • 7,295
  • 4
  • 71
  • 112