Solved

PowerShell Script remote execution

Posted on 2007-11-14
7
11,808 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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Help with MSI file 3 62
Windows 10 8 56
WDS Driver issue 4 114
Impossible to extract MSI from new JAVA releases 2 41
One of the frequent problems with the installations is when some file or registry entry is not removed from the system upon un-installation of the product. Clean removal is always highly desirable. One major reason for that is badly authored inst…
The way I use Experts Exchange to assist me in analyzing and diagnosing a problem is I first enter a Verbose Question at Experts Exchange like: Office 2007 will hang when opening and saving files I then launch WordPad (any text editor will do) an…
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 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…

759 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

Need Help in Real-Time?

Connect with top rated Experts

26 Experts available now in Live!

Get 1:1 Help Now