Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Services on remote machines

207 views
Skip to first unread message

BarbS

unread,
Jul 2, 2008, 7:49:00 PM7/2/08
to
Would appreciate any help with the following script. The script reads a text
file containing a list of services that I want to change the startup type on.
For example:
alerter,disables. This works fine on the local machine. How do I read in a
list of remote machines along with the list of services.
I want to set the startup type on services of remote servers. Thanks You.

foreach ($service in get-content "V:\Program Files\PowerGUI\my
scripts\windows 2003 elective services.txt") {
$newAry = $service.split(',')
$strsvcs = $newAry[0]
$strtype = $newAry[1]
$chkservice = get-service -name $strsvcs -ErrorAction silentlycontinue
If ( ! $chkservice)
{"not installed"}
Else
{set-service $strsvcs -startuptype $strtype
"$strsvcs is $strtype"}
}

Brandon [MVP]

unread,
Jul 2, 2008, 7:56:41 PM7/2/08
to
get-service doesnt work remotely.. you need to use .NET or WMI. In your case
I would use WMI.

$service = get-wmiobject win32_service -computer Server1 -filter
"name='<ServiceName>'"
$service.state

"BarbS" <Ba...@discussions.microsoft.com> wrote in message
news:ECD22465-1A70-4CD6...@microsoft.com...

Marco Shaw [MVP]

unread,
Jul 2, 2008, 8:16:25 PM7/2/08
to
Brandon [MVP] wrote:
> get-service doesnt work remotely.. you need to use .NET or WMI. In your
> case I would use WMI.

The user didn't state whether they are using v1 or v2. v2 CTP2 does
support a -computername parameter.

Marco

--
*Microsoft MVP - Windows Server - Admin Frameworks
https://mvp.support.microsoft.com/profile/Marco.Shaw
*PowerShell Co-Community Director - http://www.powershellcommunity.org
*Blog - http://marcoshaw.blogspot.com

Marco Shaw [MVP]

unread,
Jul 2, 2008, 8:46:42 PM7/2/08
to

Half-example of a way to do this with variables and WMI:

$servers="localhost"

$services="alerter","winrm"

foreach($server in $servers){
$result=gwmi -computer $server win32_service
foreach($service in $services){
$result|where{$_.name -eq $service}
}
}

Now, do you want to change all the startup types to the same value or
some may differ?

Brandon [MVP]

unread,
Jul 2, 2008, 9:05:33 PM7/2/08
to
IMO it is generally best to pass the Service name via -Filter as it is much
more efficient. Using where-object to filter it requires the server return
all the services over the wire. Although, In this case if you have more than
just a couple of services it may actually be best to get them all and filter
locally.

"Marco Shaw [MVP]" <marco.shaw@_NO_SPAM_gmail.com> wrote in message
news:OLVbkZK3...@TK2MSFTNGP05.phx.gbl...

Marco Shaw [MVP]

unread,
Jul 2, 2008, 9:12:53 PM7/2/08
to
Brandon [MVP] wrote:
> IMO it is generally best to pass the Service name via -Filter as it is
> much more efficient. Using where-object to filter it requires the server
> return all the services over the wire. Although, In this case if you
> have more than just a couple of services it may actually be best to get
> them all and filter locally.

Yeah... It was a toss up between one WMI call or multiple... I should
have explained what I was thinking.

BarbS

unread,
Jul 3, 2008, 10:54:03 AM7/3/08
to
Thank You. Service startup will have different values. Your answer helped.

BarbS

unread,
Jul 3, 2008, 10:55:01 AM7/3/08
to
Thank You.

Marco Shaw [MVP]

unread,
Jul 3, 2008, 11:01:38 AM7/3/08
to
BarbS wrote:
> Thank You. Service startup will have different values. Your answer helped.

That means you're all set?

BarbS

unread,
Jul 3, 2008, 5:53:02 PM7/3/08
to
Yes. Thank you for your help.

Root

unread,
Jul 7, 2008, 6:58:05 AM7/7/08
to

Hi Marco Shaw,

I would like to jump in at this point.
I seem to have the same problem as BarbS: I can easily disable a local
service, but I couldn't manage to disable a service on a remote machine.

This one works fine:
$MYSERVICE = get-wmiobject win32_service -computer myremotehost -filter
"Name='Themes'"
$MYSERVICE.state = "Stopped"
$MYSERVICE.put()

This one does NOT work (on any machine with any service):
$MYSERVICE = get-wmiobject win32_service -computer myremotehost -filter
"Name='Themes'"
$MYSERVICE.startmode = "Disabled"
$MYSERVICE.put()

I get sort of an acknowledge like
Path : \\dzba9003\root\cimv2:Win32_Service.Name="Themes"
RelativePath : Win32_Service.Name="Themes"
Server : myremotehost
NamespacePath : root\cimv2
ClassName : Win32_Service
IsClass : False
IsInstance : True
IsSingleton : False

I still wonder why, and I can't see this has been solved in this thread so
far.
Can anybody give me a hint?

Greetings
Root

Shay Levy [MVP]

unread,
Jul 7, 2008, 8:09:41 AM7/7/08
to

Try:

(gwmi win32_service -filter "name='alerter'" -computer localhost).ChangeStartMode("Automatic")


---
Shay Levy
Windows PowerShell MVP
blog: http://blogs.microsoft.co.il/blogs/ScriptFanatic

R> Hi Marco Shaw,
R>
R> I would like to jump in at this point.
R> I seem to have the same problem as BarbS: I can easily disable a
R> local
R> service, but I couldn't manage to disable a service on a remote
R> machine.
R> This one works fine:
R> $MYSERVICE = get-wmiobject win32_service -computer myremotehost
R> -filter
R> "Name='Themes'"
R> $MYSERVICE.state = "Stopped"
R> $MYSERVICE.put()
R> This one does NOT work (on any machine with any service):
R> $MYSERVICE = get-wmiobject win32_service -computer myremotehost
R> -filter
R> "Name='Themes'"
R> $MYSERVICE.startmode = "Disabled"
R> $MYSERVICE.put()
R> I get sort of an acknowledge like
R> Path : \\dzba9003\root\cimv2:Win32_Service.Name="Themes"
R> RelativePath : Win32_Service.Name="Themes"
R> Server : myremotehost
R> NamespacePath : root\cimv2
R> ClassName : Win32_Service
R> IsClass : False
R> IsInstance : True
R> IsSingleton : False
R> I still wonder why, and I can't see this has been solved in this
R> thread so
R> far.
R> Can anybody give me a hint?
R> Greetings
R> Root
R> "Marco Shaw [MVP]" wrote:
R>

Shay Levy [MVP]

unread,
Jul 7, 2008, 8:16:49 AM7/7/08
to

Possible values for ChangeStartMode("<StartMode>"):

Boot
Device driver started by the operating system loader. This value is valid
only for driver services.

System
Device driver started by the operating system initialization process. This
value is valid only for driver services.

Automatic
Service to be started automatically by the Service Control Manager during
system startup.

Manual
Service to be started by the Service Control Manager when a process calls
the StartService method.

Disabled
Service that can no longer be started.

To stop the service use the stopService() method:

(gwmi win32_service -filter "name='alerter'" -computer localhost).stopService()


---
Shay Levy
Windows PowerShell MVP
blog: http://blogs.microsoft.co.il/blogs/ScriptFanatic

SL> Try:
SL>
SL> (gwmi win32_service -filter "name='alerter'" -computer
SL> localhost).ChangeStartMode("Automatic")
SL>
SL> ---
SL> Shay Levy
SL> Windows PowerShell MVP
SL> blog: http://blogs.microsoft.co.il/blogs/ScriptFanatic

Root

unread,
Jul 7, 2008, 8:13:02 AM7/7/08
to

Hi Shay Levy,

your answer was about "localhost", but it worked on my remotehost, too.
So, my question is solved.
Thanks so much!

(though, I still wonder why our teacher only told us the "put()" method for
setting WMI-Object properties.)

best regards,

Root

NS

unread,
Dec 19, 2008, 7:05:01 AM12/19/08
to
Can anyone point out where i'm going wrong pls ? I can get my command working
when on just a single remote machine but when i adapt to use a variable for a
list of machines in text file it fails

$complist = get-content .\testlist.txt
$services = (Get-WmiObject win32_service -computername $complist |
where-Object { ($_.name -eq "spooler") -and ($_.state -eq
"Stopped")}).startservice()

Marco Shaw [MVP]

unread,
Dec 19, 2008, 8:39:47 AM12/19/08
to
NS wrote:
> Can anyone point out where i'm going wrong pls ? I can get my command working
> when on just a single remote machine but when i adapt to use a variable for a
> list of machines in text file it fails
>
> $complist = get-content .\testlist.txt
> $services = (Get-WmiObject win32_service -computername $complist |
> where-Object { ($_.name -eq "spooler") -and ($_.state -eq
> "Stopped")}).startservice()

Do you have one system per line in testlist.txt?

Anything end up in $services?

Marco

--
*Microsoft MVP - Admin Frameworks
https://mvp.support.microsoft.com/profile/Marco.Shaw
*Co-Author - Sams Windows PowerShell Unleashed 2nd Edition (due December
15th, 2008)

Shay Levy [MVP]

unread,
Dec 19, 2008, 8:54:50 AM12/19/08
to
Hi NS,

Your command returns a collection of service objects, use foreach to start
them one by one:


---
Shay Levy
Windows PowerShell MVP

http://blogs.microsoft.co.il/blogs/ScriptFanatic
PowerShell Toolbar: http://tinyurl.com/PSToolbar

N> Can anyone point out where i'm going wrong pls ? I can get my command
N> working when on just a single remote machine but when i adapt to use
N> a variable for a list of machines in text file it fails
N>
N> $complist = get-content .\testlist.txt
N> $services = (Get-WmiObject win32_service -computername $complist |
N> where-Object { ($_.name -eq "spooler") -and ($_.state -eq
N> "Stopped")}).startservice()
N> "Root" wrote:
N>

Shay Levy [MVP]

unread,
Dec 19, 2008, 9:02:02 AM12/19/08
to
Hi NS,

Your command returns a collection of service objects, use foreach to start
them one by one:

gwmi win32_service -computer (gc .\testlist.txt) -filter "name='spooler'
and state='Stopped'" | foreach {$_.startservice()}

---
Shay Levy
Windows PowerShell MVP

N> Can anyone point out where i'm going wrong pls ? I can get my command
N> working when on just a single remote machine but when i adapt to use
N> a variable for a list of machines in text file it fails
N>
N> $complist = get-content .\testlist.txt
N> $services = (Get-WmiObject win32_service -computername $complist |
N> where-Object { ($_.name -eq "spooler") -and ($_.state -eq
N> "Stopped")}).startservice()
N> "Root" wrote:
N>

0 new messages