0

I writing some scripts to start and stop some services remotely using powershell.

I'm running the following commands to do the task:

To get session :

$UserName = "IpAddress\username"
$Password = ConvertTo-SecureString "password" -AsPlainText -Force
$psCred = New-Object System.Management.Automation.PSCredential($UserName, $Password)
$s = New-PSSession -ComputerName IpAddress -Credential $psCred

To stop the service :

Invoke-Command -Session $s -ScriptBlock {Stop-Service -Name "ServiceName" -Force}

These are working perfectly fine as expected.
Now my requirement is to set the start type of the service to delayedstart.For that I'm writing the below command:

Invoke-Command -Session $s -ScriptBlock {sc config "ServiceName" start=delayed-auto}

But this is giving error :

A positional parameter cannot be found that accepts argument 'start=delayed-auto'.

When start and stop commands are working, why is this not working? Am I missing anything here?

CrazyCoder
  • 2,194
  • 10
  • 44
  • 91
  • 1
    I think it is missing the server name as the first parameter: `"sc.exe \\$Computer config $Service start= delayed-auto"`. However, it can also be done by adding a REG_DWORD value called `DelayedAutoStart` under `HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services\` to the registry. This can be done with `Set-ItemProperty -Path "Registry::HKLM\System\CurrentControlSet\Services\$ServiceName" -Name "DelayedAutostart" -Value 1 -Type DWORD` – Theo Oct 26 '18 at 11:08
  • 3
    @CrazyCoder - the term `sc` is an alias for `Set-Content`. [*grin*] you need to use `sc.exe` to get the external exe to run. – Lee_Dailey Oct 26 '18 at 11:17
  • @Lee_Dailey , it solved my problem – CrazyCoder Oct 26 '18 at 13:04
  • @CrazyCoder - excellent! i'm pleased to have been a help ... [*grin*] – Lee_Dailey Oct 26 '18 at 13:08

0 Answers0