?
Solved

PowerShell Script remote execution

Posted on 2007-11-14
7
Medium Priority
?
12,097 Views
Last Modified: 2010-07-27
I have a simple powershell script that sets a service and starts it.  I want to run this against a list of computers instead of typing each computer in individually... is there a way to do this with powershell?
0
Comment
Question by:rtmcmullen
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
7 Comments
 
LVL 18

Expert Comment

by:BSonPosh
ID: 20280288
What does the Script do specifically? While the native powershell commands like get-service and start-service do not work against remote machines, most of this can be done with .NET.
0
 

Author Comment

by:rtmcmullen
ID: 20280458
The script sets a service to Automatic and then starts the service.
0
 
LVL 18

Expert Comment

by:BSonPosh
ID: 20280676
Try something like this... I havent tested it, but it should work.

###############################
Param($server,$list,$service,[switch]$verbose)
if($Verbose){$VerbosePreference = "Continue"}
if($list)
{
    foreach($srv in $list)
    {
        $query = "Select * from Win32_Service where Name='$service'"
        $myService = get-WmiObject -query $query -computer $srv
        $myService.ChangeStartMode("Automatic")
        $myService.Start()
    }
}
if($server)
{
    $query = "Select * from Win32_Service where Name='$service'"
    $myService = get-WmiObject -query $query -computer $server
    $myService.ChangeStartMode("Automatic")
    $myService.Start()
}
0
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 

Author Comment

by:rtmcmullen
ID: 20281349
I'm a beginner with powershell, can you break this down a little for me?

Say the service is DHCP, and I wanted to call the list it would pulling from ServerList.txt

Thanks
0
 
LVL 18

Accepted Solution

by:
BSonPosh earned 2000 total points
ID: 20282088
What I provide was a script that takes parameters... In this case it takes 4.
1) Server: if you pass -server it will only do that one server
2) List: You can provide a list file of servers.
3) Service: Name of the service you want to modify
4) Verbose: is not used here.

I did have some mistakes that I changed in the following code. To use cut/paste the code into a file called Set-RemoteService.ps1. Make sure to set your executionpolicy to run scripts... it will not by default. You do that by using the set-executionpolicy cmdlet.
PS> Set-Executionpolicy "RemoteSigned"
to run the script you do
PS> C:\PathToScript\Set-RemoteService.ps1 -list c:\ServerList.txt -service "DHCP"

###############################
Param($server,$list,$service,[switch]$verbose)
if($Verbose){$VerbosePreference = "Continue"}
if($list)
{
    foreach($srv in (get-content $list))
    {
        $query = "Select * from Win32_Service where Name='$service'"
        $myService = get-WmiObject -query $query -computer $srv
        $myService.ChangeStartMode("Automatic")
        $myService.Start()
    }
}
if($server)
{
    $query = "Select * from Win32_Service where Name='$service'"
    $myService = get-WmiObject -query $query -computer $server
    $myService.ChangeStartMode("Automatic")
    $myService.Start()
}
0
 
LVL 1

Expert Comment

by:Computer101
ID: 20475685
Forced accept.

Computer101
EE Admin
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Log files are useful in diagnosing and repairing problems.  This is a list of common log files and their standard locations that I've compiled.   While this is not exhaustive, it is a pretty good list that I've found to be useful.  I may update it f…
Citrix XenApp, Internet Explorer 11 set to Enterprise Mode and using central hosted sites.xml file.
This video Micro Tutorial explains how to clone a hard drive using a commercial software product for Windows systems called Casper from Future Systems Solutions (FSS). Cloning makes an exact, complete copy of one hard disk drive (HDD) onto another d…
Windows 8 comes with a dramatically different user interface known as Metro. Notably missing from the new interface is a Start button and Start Menu. Many users do not like it, much preferring the interface of earlier versions — Windows 7, Windows X…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question