Powershell script for starting service and running remote script

Powershell script for starting service and running remote script

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.

SVCStart.ps1
# Setup trap to catch exceptions
trap [Exception]
{
write-error $(“TRAPPED: ” + $_.Exception.Message);
}
$start = $true

#Assuming headers of “computername”,”operation”,”service” 

$computers = Import-CSV ‘C:scriptsservers.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:scriptslog.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:scriptsexternallog.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. 


Servers.txt

computername,operation,service <–headers
c1399,w3svc,c:scriptsexternal.ps1 <–Server name, service, external script location
Wn2k8srv,script,c:scriptsdotwhatever.bat
XcutionDT,w3svc,c:scriptsvstudio.ps1
XcutionSRV,script,c:scriptsgetprocess.ps1

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..

"You need a similar assignment done from scratch? Our qualified writers will help you with a guaranteed AI-free & plagiarism-free A+ quality paper, Confidentiality, Timely delivery & Livechat/phone Support.


Discount Code: CIPD30



Click ORDER NOW..

order custom paper