Solved

PowerShell Script remote execution

Posted on 2007-11-14
7
11,892 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
  • 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 

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 500 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

When you upgrade from Windows 8 to 8.1 or to Windows 10 or if you are like me you are on the Insider Program you may find yourself with many 450MB recovery partitions.  With a traditional disk that may not be a problem but with relatively smaller SS…
If you don't know how to downgrade, my instructions below should be helpful.
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…
Windows 8 came with a dramatically different user interface known as Metro. Notably missing from that interface was a Start button and Start Menu. Microsoft responded to negative user feedback of the Metro interface, bringing back the Start button a…

776 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