Reputation: 39
I have a little script where the user enters the script name (the description name) and has the option to modify the properties. This is done with a GUI interface: a textbox and some drop-down lists. I tried several ways to make it but none of them worked.
Here is how I retrieve the service information:
$thisservice = Get-WmiObject -Class Win32_Service -ComputerName $Server | Where { $_.displayName -eq $serviceBox.text }
I look at $server
computer and match up the "service display name" to the value contained in the textbox field: $_.displayName -eq $serviceBox.text
. This works well, if I list the data I get exactly what is in the service console: .name, .description, .state, .startmode, .displayname
I store the service name in a variable:
$servicename = $thisservice.name
I know you can not start a service unless it is on "Manual" or "auto" so first I set it to auto. Here I tryed to different ways but none worked:
$servicename.ChangeStartMode("Auto")
and
Set-Service $servicename -StartupType Auto
and then I start the service (but this doesn't work either even if I preset the state-mode on Auto form the service manager):
$servicename.startservice()
Also the $servicename.stopservcie()
fails.
I tried this on 2 different machines, on both I have admin rights, I tryed running the script with Admin mode, same result. I even tried it directly from PS console and I couldn't change the service state! What am I doing wrong?
Upvotes: 1
Views: 697
Reputation: 22871
Your problem is here: $servicename = $thisservice.name
By doing that you set $servicename
to be a string, not the service object. Therefore you can't call service methods on it any more.
You should be able to just do:
$thisservice.ChangeStartMode("Automatic")
$thisservice.StartService()
Upvotes: 3