Powershell script for starting service and running remote script

Sep 22nd, 2013
Price: $80 USD

Question description

PowerShell script for starting service and calling external script on remote machine.. I have most of the script written where it will start the w3svc service (which is what I need). Then I have added an extra elseif statement which will be used to call an external script for each machine listed in servers.txt. These may be powershell scripts or bat scripts.. All I am looking to do is look at servers.txt pull in the server name and then look at the name/location of the script residing on that server and run that script (on the remote server) with a start or stop command. I then am using a write-output in both operations to log exactly what has been done to a log file. Once I get this start script going then I need to use the same script to create a stop script which will use the same servers.txt to stop the w3svc service and run an external script with a stop argument.
I am adding the code that I have already. I have tried to use WMI for this so I can leverage Powershell 3 but am not opposed to something different if it is cleaner. Also if there is anything that needs cleaning up in my current script I would appreciate the input.

# Setup trap to catch exceptions
trap [Exception]
write-error $("TRAPPED: " + $_.Exception.Message);
$start = $true

#Assuming headers of "computername","operation","service" 

$computers = Import-CSV 'C:\scripts\servers.txt' 

foreach($computer in $computers)
write-host $computer.computername $computer.service $computer.operation
$objWMIService = Get-WmiObject -Class win32_service -computer $computer.computername -filter "name = '$($computer.service)'"
if($computer.operation -eq "service")
if($objWMIService.state -ne "Running")
$objWMIService.StartService() | Out-Null;
# Refresh the object instance 
$state = (Get-WmiObject Win32_Service -Filter "Name = '$($computer.service)'" -computer $computer.name).state
Write-Output "$($computer.service) is ""$state"" on $($computer.name)" | Out-File C:\scripts\log.txt -append
elseif($computer.operation -eq "script")

$operation = [IO.File]::ReadAllText(".\external.ps1")
Invoke-Expression $service
Write-Output "$($service.service) is ""$state"" on $($computer.name)" | Out-File C:\scripts\externallog.txt -append

#As part of the ElseIF I have to call whatever script (may be different names) for each server listed under service. 
# As part of calling the EXTERNAL script I need to be able to give it a start of stop argument. 
# so this portion of the start script is going to be pulling from the third placement on server.txt under the header of service.

#[the script will live on the remote server, this is where I want to use psremoting (or WMI If possible )to run the script *on* the remote server. 


computername,operation,service <--headers
c1399,w3svc,c:\scripts\external.ps1 <--Server name, service, external script location

Optional Information: 
Language (or Software): Other

Already Tried: 
I have written most of the script but I cant figure out how to use invoke-expression to run the external script and add in a start argument..

Tutor Answer

(Top Tutor) aroojch
School: University of Maryland

Studypool has helped 1,244,100 students

Review from student
" Totally impressed with results!! :-) "
Ask your homework questions. Receive quality answers!

Type your question here (or upload an image)

1819 tutors are online

Brown University

1271 Tutors

California Institute of Technology

2131 Tutors

Carnegie Mellon University

982 Tutors

Columbia University

1256 Tutors

Dartmouth University

2113 Tutors

Emory University

2279 Tutors

Harvard University

599 Tutors

Massachusetts Institute of Technology

2319 Tutors

New York University

1645 Tutors

Notre Dam University

1911 Tutors

Oklahoma University

2122 Tutors

Pennsylvania State University

932 Tutors

Princeton University

1211 Tutors

Stanford University

983 Tutors

University of California

1282 Tutors

Oxford University

123 Tutors

Yale University

2325 Tutors