I’m studying for my Computer Science class and need an explanation.
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
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")
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.
c1399,w3svc,c:\scripts\external.ps1 <--Server name, service, external script location
Language (or Software): Other
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..
Student has agreed that all tutoring, explanations, and answers provided by the tutor will be used to help in the learning process and in accordance with Studypool's honor code & terms of service.