Set-Service: Cannot stop service because it is dependent on other services - powershell

When I run the following command:
Set-Service -ComputerName appserver -Name MyService -Status Stopped
I get an error message:
Set-Service : Cannot stop service 'My Service (MyService)' because it is
dependent on other services.
At line:1 char:12
+ Set-Service <<<< -ComputerName appserver -Name MyService -Status Stopped
+ CategoryInfo : InvalidOperation: (System.ServiceProcess.ServiceController:ServiceController) [Set-Service], ServiceCommandException
+ FullyQualifiedErrorId : ServiceIsDependentOnNoForce,Microsoft.PowerShell.Commands.SetServiceCommand
I can stop the service from the services.msc GUI, and I can start the service with Set-Service, but I can't stop it again.
It is true that the service depends on some other services, but I don't understand why that would prevent me from stopping it—nothing else depends on it.

The Set-Service cmdlet is for changing the configuration of a service. That you can use it to stop a service by changing its status is just coincidental. Use the Stop-Service cmdlet for stopping services. It allows you to stop dependent services as well via the parameter -Force. You'll need to retrieve the service object with Get-Service first, though, since Stop-Service doesn't have a parameter -ComputerName.
Get-Service -Computer appserver -Name MyService | Stop-Service -Force

I eventually resolved this problem with the following code, which calls sc to stop the service and then waits for it to finish stopping. This achieves the same result as expected from Set-Service -Status Stopped; that is, when it returns the service has been stopped. (sc on its own starts to stop the service, but does not wait until it has finished stopping.)
Start-Process "$env:WINDIR\system32\sc.exe" \\APPSERVER,stop,MyService -NoNewWindow -Wait
while ((Get-Service -ComputerName APPSERVER -Name MyService |
Select -ExpandProperty Status) -ne 'Stopped') {
Write-Host "Waiting for service to stop..."
Start-Sleep -Seconds 10
}

Related

How to create a servicename using powershell

I am trying to create a servicename that should be displayed when I go to "service.msc", powershell script is not able to create.
I tried the following code but its not able to create the service. I am running this as a "Administrator"
$serviceName="RSCDsvc"
$serviceDisplayName="BMC BladeLogic Server Automation RSCD Agent"
$serviceDescription="BMC BladeLogic Server Automation Remote System Call Daemon"
$serviceExecutable="RSCDsvc.exe"
LogWrite $bsaLog "Installing service.."
$binaryPath="C:\Program Files\BMC Software\BladeLogic\RSCD\RSCDsvc.exe"
New-Service -name $serviceName -DisplayName $serviceDisplayName -binaryPathName $binaryPath -StartupType Automatic -Description $serviceDescription
Start-Service -Name $serviceName
Get-Service $serviceName
LogWrite $bsaLog "Service Installed.."
I want to see the servicename when I type "services.msc" I should be able to see "RSCDsvc" with display "BMC Bladelogic Server Automation RSCD Agent"
When I run these commands in the powershell prompt individually I get the following error, I could see this service running when I go to taskmanager and go to "Details" tab I could see the service, but not when I go to "services.msc"
New-Service : Service 'BMC BladeLogic Server Automation RSCD Agent (RSCDsvc)' cannot be created due to the following error: The
specified service already exists
At line:1 char:1
+ New-Service -name $serviceName -DisplayName $serviceDisplayName -bina ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : PermissionDenied: (RSCDsvc:String) [New-Service], ServiceCommandException
+ FullyQualifiedErrorId : CouldNotNewService,Microsoft.PowerShell.Commands.NewServiceCommand

Unable to stop Remote Service using Stop-Service

Get-Service -ComputerName "SERVERNAME" -Name "SERVICENAME" | Stop-Service -ErrorAction Inquire
I am trying to stop a windows service remotely, but it returns an error:
Stop-Service : Cannot find any service with service name 'SERVICENAME.exe'
Even though I specify the correct service name, it seems to add the exe part to the name.
When I Run Get-Service it works perfectly and gets the Service details, but the moment I add | Stop-Service it returns the error above.
Get-Service -ComputerName "SERVERNAME" -Name "SERVICENAME"
I have also tried Get-WmiObject but with no luck.
Everything else seems to work, all commands return the results required except the stop or start service commands where I get this error.
You probably need to pass a -computer name argument to the Stop-Service command. Feels like you may be getting the service from remote computer, then trying to stop the service on local computer.
Try (Get-Service -computername remotepc -name servicename).Stop()

PS4, Get-WMIObject returns PID=0 for an in-house developed service

Thanks in advance.
Here is a Powershell4 script that we're running:
$process = Get-WMiObject Win32_Service -Filter "Name ='HotKeyService'"
write-host Process Name = $process.name
write-host Process ID = $process.processid
$oopid = $process.processid
stop-process -id $oopid -force
wait-process -id $oopid -timeout 60 -WarningAction SilentlyContinue
-------------------------------
working directory: C:\Program Files\HK\HK.HotKeyService
With this I get:
Process Name = HotKeyService
Process ID = 0
Error#1
stop-process : Cannot stop process "Idle (0)" because of the following
error: Access is denied
At C:\Windows\TEMP\tmp206027652026805712.ps1:6 char:1
+ stop-process -id $oopid -force
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : CloseError: (System.Diagnostics.Process (Idle):P
rocess) [Stop-Process], ProcessCommandException
+ FullyQualifiedErrorId : CouldNotStopProcess,Microsoft.PowerShell.Command
s.StopProcessCommand
Error#2
wait-process : This command stopped operation because it cannot wait
on
'System Idle' process. Specify another process and Run your command again.
At C:\Windows\TEMP\tmp206027652026805712.ps1:7 char:1
+ wait-process -id $oopid -timeout 60 -WarningAction SilentlyContinue
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (System.Diagnostics.Process
(Idl
e):Process) [Wait-Process], ProcessCommandException
+ FullyQualifiedErrorId :
WaitOnIdleProcess,Microsoft.PowerShell.Commands.
WaitProcessCommand
===============================
command exit code: 0
The code above is part of a larger process that gets run on many services on Windows 2012 servers and Win 7 workstations. Sometimes though it fails and I don't know why. In this case, the correct service name is retrieved which can be seen in the command ouptut but the PID is 0, which can't possibly be correct as that's the system idle process's PID.
The parent software calling this PS script later shows that files in the HotKey install folder cannot be updated because, "(The process cannot access the file because it is being used by another process)", which makes sense because the HotKeyService service didn't stop correctly.
I ran the parent software again and it worked correctly on the same service.
My question is how could Get-WMIObject return the correct service name but then on the next step show a PID of zero? I can't figure this one out.
Thanks for the assistance.
KJ
When you see a Win32_Service instance with a ProcessId value of 0, it simply means that the service isn't running.
You'll find that:
(Get-WMiObject Win32_Service -Filter "Name ='HotKeyService'").State
is Stopped

Powershell Workflow to reboot computers

Code:
workflow Test-RemoteReboot{
param ([string[]]$serverNames)
foreach -parallel($server in $serverNames){
Restart-Computer -PSComputerName $server -Wait -Force
}
}
Test-RemoteReboot SP,SP2
Issue:
this is a small excerpt from a pretty long workflow I built in powershell. By all accounts, this should work but I get the following error (even when running this script in isolation):
Microsoft.PowerShell.Utility\Write-Error : The running command stopped because the preference variable "ErrorActionPreference" or common parameter is set to Stop: The computer SP2 is
skipped. Fail to retrieve its LastBootUpTime via the WMI service with the following error message: The RPC server is unavailable. (Exception from HRESULT: 0x800706BA).
At line:433 char:25
+ ... Receive-Job -Job $job -Wait -Verbose -Debug -ErrorAction ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Write-Error], ActionPreferenceStopException
+ FullyQualifiedErrorId : System.Management.Automation.ActionPreferenceStopException,Microsoft.PowerShell.Commands.WriteErrorCommand
+ PSComputerName : [localhost]
I looked into the RPC server unavailable issue. Firewall is down, RPC Service is running, Servers are on domain, Workflow running from domain member computer, as domain admin. I can successfully PSRemote into the machines and do whatever. I can even do this:
workflow Test-LocalRebootRemotely{
param ([string[]]$serverNames)
foreach -parallel($server in $serverNames){
InlineScript { Restart-Computer -Force } -PSComputerName $server
}
}
Test-LocalRebootRemotely SP,SP2
The problem is, is that I need a return value to determine if I need to reboot and handle other logic outside of the remote computer. I do notice that there is an abnormal pause just before it errors. So maybe it is a timeout? anyone have a similar issue?
You can try the Restart-Computer cmdlet with the -Protocol WSMan parameter instead of the default DCOM over RPC protocol (-Protocol DCOM).
This would confirm that your RPC network packets are blocked somewhere.

Set-Service fails when executing as part of a script but passes when running manually

As part of my script, I have the following code inside a powershell module.
Set-Service -Name $ServiceName -ComputerName $ComputerName -Status $ServiceStatus -Confirm:$false -Verbose;
When this runs in the script I get the following error
Set-Service : Service 'Service Name' cannot be configured due to the following error: The specified service does not exist as an installed service
But then when running manually as part of my testing the command runs without error.
Set-Service -Name "Service Name" -ComputerName "machine name" -Status Stopped -Confirm:$false -Verbose;