Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

PowerShell Script remote execution

Posted on 2007-11-14
7
Medium Priority
?
12,148 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

This is a little timesaver I have been using for setting up Microsoft Small Business Server (SBS) in the simplest possible way. It may not be appropriate for every customer. However, when you get a situation where the person who owns the server is i…
INTRODUCTION The purpose of this document is to demonstrate the Installation and configuration of the Data Protection Manager product. Note that this demonstration was prepared on the basis of Windows OS is 2008 R2 and DPM 2010. DATA PROTECTI…
With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…
With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…

618 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