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
LVL 3
SAM ITAD windows Admin Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

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

Chris DentPowerShell DeveloperCommented:
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[]).
0
SAM ITAD windows Admin  Author Commented:
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
0
Chris DentPowerShell DeveloperCommented:
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

0
Defend Against the Q2 Top Security Threats

Were you aware that overall malware worldwide was down a surprising 42% from Q1'18? Every quarter, the WatchGuard Threat Lab releases an Internet Security Report that analyzes the top threat trends impacting companies worldwide. Learn more by viewing our on-demand webinar today!

Chris DentPowerShell DeveloperCommented:
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

0
SAM ITAD windows Admin  Author Commented:
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

0
SAM ITAD windows Admin  Author Commented:
please find the attached scree shot , for similar command getting out put with computername
test.jpg
0
Chris DentPowerShell DeveloperCommented:
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 :)
0
SAM ITAD windows Admin  Author Commented:
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
0
SAM ITAD windows Admin  Author Commented:
using format table now iam getting out in this format pscomputername status, how to get the output of services in coloum wise format
0
Chris DentPowerShell DeveloperCommented:
You'll need to parse the text output from iisreset. This might work, it uses a regular expression to rip apart the response.
Invoke-Command -scriptblock {
    (iisreset /status) -split '\r?\n' | Where-Object { $_ -match 'Status for (?<Name>[^:]+) *: *(?<Status>.+)' } | ForEach-Object {
        New-Object PSObject -Property @{
            Name = $matches.Name.Trim()
            Status = $matches.Status.Trim()
        }
    }
} -computername (Get-Content C:\Users\test\users.csv)

Open in new window

With that you could get one line per service on each server.
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
SAM ITAD windows Admin  Author Commented:
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.
0
SAM ITAD windows Admin  Author Commented:
Best solution
0
SAM ITAD windows Admin  Author Commented:
can we run this script against windows servers 2003 servers? if not can we use the same script in to .bat file ?
0
Chris DentPowerShell DeveloperCommented:
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.
0
SAM ITAD windows Admin  Author Commented:
thanks for response
0
SAM ITAD windows Admin  Author Commented:
can we build the script using WMI so we can run against 2003 servers...
0
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
CSV

From novice to tech pro — start learning today.