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
AjarnJonesyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

footechCommented:
Sorry, but it's not really clear what you're after.
"service will be a service script path" - what does that mean?
PS Remoting can be extremely useful, but depending on what you want to do it may not be needed.  Many commands have a -computername parameter which you can use.  For PS Remoting you can use the Invoke-Command cmdlet.
To read in a file like server.txt that you attached it is best to use Import-CSV which makes it easy to iterate through each line and reference the value of any field that you want.
I can't really provide much in the way of code without a better picture of your goal, but here's a bit a of a guess.
Import-Csv server.txt | ForEach `
{
    $comp = $_.computername
    $service = $_.service
    $state = (Get-WmiObject Win32_Service -ComputerName $comp -filter "Name = '$service'").state
    Write-Output "$service is ""$state"" on $comp"
} | Out-File C:\scripts\scriptlog.txt -append 

Open in new window

0
AjarnJonesyAuthor Commented:
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
0
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
One of the reasons for confusion is because you try to do different things not really related with one script and one control file. You need a clear structure. The file you provided contains a service name as operation, with a script as service - this was not intended when introducing that CSV format.
Restrict your file to doing exactly ONE type of operation, or you will have to extend the file and logic to an extend it probably only gets more confusing.
Further it is not clear what the "Start" parameter should be - is it fixed (e.g. the computername), or needs to be provided statically by the file, or dynamically thru some expression evaluation?

You might want a CSV like:
ComputerName, Operation, Name, Condition, Script, Parameter
PC1, StopService, W3Svc, Running, SvcWasRunningNowStopped.ps1, $(get-date)
PC2, CheckService, W3Svc, Stopped, SvcWasNotRunning.ps1,

Open in new window

which clearly states what to check, and what to do.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Legacy OS

From novice to tech pro — start learning today.