Link to home
Start Free TrialLog in
Avatar of AjarnJonesy
AjarnJonesyFlag for United States of America

asked on

powershell Invoke-expression

Im trying to use the invoke-expression in powershell to call a script on an external Machine. I am reading in servers,service,remote script from a servers.txt file. Then calling the script on the external server is my second operation in my main script. I am looking to be able to use a variable to hold the name/path of the script for execution and then give an argument or start or stop. then when it is done i want to write this out to a log file. The code I have so far is incomplete. I also added a copy of my server.txt which will be used with a list of multiple servers.
Headers are computername,operation,service    in this case I need to key on computername and Service (service will be a service script path. I am not sure if I need to use PSremoting or if I can do this with WMI?

elseif($computer.service -eq "script")
    {        
    $service = [IO.File]::ReadAllText(".\external.ps1")
    Invoke-Expression $operation -Command

  Write-Output "$($computer.service) is ""$state"" on $($computer.name)" | Out-File C:\scripts\scriptlog.txt -append
server.txt
SOLUTION
Avatar of footech
footech
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of AjarnJonesy

ASKER

Hi Footech
Thanks for the reply I am attaching my complete to see if that helps..
I am doing a import CSV.. I am trying to get the script to basically do 2 different things.
What I am trying to do is pull in the server name, then stop the W3SVC (world wide web publishing) service. Then add a file path to a script that resides on the server in computername and can be ran with a Start argument (using the invoke-expression). So basically the logic that I am trying to do follows
1. read in server.txt which has server name, service, and a remote script to be ran if needed.
2. look at the W3SVC (or other service if listed) and if not running then start the script
3. write the output to a log file.
3. look at the remote script file path and run that script then write the output to a log file.

Once I get this process working I will then basically copy the script to create a stop script as well. As a MS server guy I have never had to work much with anything other than batch scripts. So this is driving me up the wall as it is not making much sense to me.
thanks again.
SVCStart.txt
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial