• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 12191
  • Last Modified:

PowerShell Script remote execution

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
rtmcmullen
Asked:
rtmcmullen
  • 3
  • 2
1 Solution
 
BSonPoshCommented:
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
 
rtmcmullenAuthor Commented:
The script sets a service to Automatic and then starts the service.
0
 
BSonPoshCommented:
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
Industry Leaders: 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!

 
rtmcmullenAuthor Commented:
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
 
BSonPoshCommented:
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
 
Computer101Commented:
Forced accept.

Computer101
EE Admin
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now