Avatar of SAM IT
SAM IT
 asked on

This script has to run not aganist multiple servers using csv

This comment is working only if i add computer name after -computername.

IF I add csv path for multiple servers its not working

_________________________________________________________________________________
Invoke-Command -scriptblock { iisreset /status } -computername C:\Users\test\users.csv
PowershellScripting Languages

Avatar of undefined
Last Comment
SAM IT

8/22/2022 - Mon
Chris Dent

It will not read a CSV file for you, you have to do that first.

If the CSV file had a column named ComputerName, which contained the list, you could use:
Invoke-Command -scriptblock { iisreset /status } -computername (Import-Csv C:\Users\test\users.csv).ComputerName

Open in new window

If the CSV file is just a raw list of computers (no headers, no other columns) you could use Get-Content instead.
Invoke-Command -scriptblock { iisreset /status } -computername (Get-Content C:\Users\test\users.csv)

Open in new window

Whatever happens you have to handle reading the CSV file first, then pass it into the ComputerName parameter which is expecting an array of strings (String[]).
SAM IT

ASKER
Thanks for response

Getting out put with out server name for service status.

I tried to add | format-table computer, status , no luck

please find the out put screen shot
iis.jpg
Chris Dent

Looking better. The trouble you have now is that the command you're running doesn't return an object, there's nothing for it to add the computer name to.

Even then, there's no Status field by default. This example will give you a ComputerName property because an object is being returned. But it won't give status, you'd have to define status (make up code that creates it, and gives it a value).
Invoke-Command { Get-Process -id $PID | Select Name } -ComputerName SomeComputer

Open in new window

Your help has saved me hundreds of hours of internet surfing.
fblack61
Chris Dent

Let's just see if those values returned by iisreset are strings:
Invoke-Command -scriptblock { New-Object PSObject -Property @{Status=(iisreset /status)} } -computername (Get-Content C:\Users\test\users.csv)

Open in new window

SAM IT

ASKER
Getting below output error

PS C:\Windows\system32> Invoke-Command -scriptblock { New-Object PSObject -Property @{Status=(iisreset /status)} } -computername (Get-Content C:\Users\Narasimha.s\Desktop\iistest.csv)
[Computername] Connecting to remote server Computername failed with the following error message : WinRM cannot process the request. The following 
error occurred while using Kerberos authentication: Cannot find the computer Computername. Verify that the computer exists on the network and that 
the name provided is spelled correctly. For more information, see the about_Remote_Troubleshooting Help topic.
    + CategoryInfo          : OpenError: (Computername:String) [], PSRemotingTransportException
    + FullyQualifiedErrorId : NetworkPathNotFound,PSSessionStateBroken

Status                                             PSComputerName                                    RunspaceId                                       
------                                             --------------                                    ----------                                       
{, Status for Windows Process Activation Servic... ep-ts-wss-01                                      7a544dc1-77a0-46ca-be87-b706941c9127             
{, Status for IIS Admin Service ( IISADMIN ) : ... AP-TS-wss-01 

Open in new window

SAM IT

ASKER
please find the attached scree shot , for similar command getting out put with computername
test.jpg
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Chris Dent

I can't help you much with that one. The error message says one of the computers you're trying to poll doesn't exist, that's not a code problem :)
SAM IT

ASKER
Now I am getting out. perfectly

But there 03 service are running for IIS out put is mentioned in one excel line in landscape format , Cant we make it has a row wise and out put starts from PS computername status
output.jpg
SAM IT

ASKER
using format table now iam getting out in this format pscomputername status, how to get the output of services in coloum wise format
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
ASKER CERTIFIED SOLUTION
Chris Dent

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
SAM IT

ASKER
Sir, you are excellent really . In very short time you have written the script.

Thank you so much . If you are at twitter I will become a fan of you

Thanks for support.
SAM IT

ASKER
Best solution
SAM IT

ASKER
can we run this script against windows servers 2003 servers? if not can we use the same script in to .bat file ?
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Chris Dent

You can run it against 2003 if the 2003 servers have Windows Remoting configured (remoting was introduced with PowerShell 2.0, but it isn't enabled by default).

Ideally the 2003 boxes should have at least PowerShell 3 installed, the script "should" work down to 3.0. I wouldn't guarantee it's right for PowerShell 2.0 though.

I can't convert it into a batch script, I never learnt Windows batch scripting (I really don't like it). Even if you did convert it you'd have to figure out how to remotely execute it (psexec, perhaps), and convert the results into something presentable (batch will only work with strings). Basically it becomes a completely different problem.
SAM IT

ASKER
thanks for response
SAM IT

ASKER
can we build the script using WMI so we can run against 2003 servers...
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23