We help IT Professionals succeed at work.

This script has to run not aganist multiple servers using csv

SAM IT
SAM IT asked
on
171 Views
Last Modified: 2018-12-14
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
Comment
Watch Question

Chris DentPowerShell Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
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 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
Chris DentPowerShell Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
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

Chris DentPowerShell Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
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 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

SAM ITAD windows Admin  

Author

Commented:
please find the attached scree shot , for similar command getting out put with computername
test.jpg
Chris DentPowerShell Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
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 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
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
PowerShell Developer
CERTIFIED EXPERT
Top Expert 2010
Commented:
This problem has been solved!
(Unlock this solution with a 7-day Free Trial)
UNLOCK SOLUTION
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.
SAM ITAD windows Admin  

Author

Commented:
Best solution
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 ?
Chris DentPowerShell Developer
CERTIFIED EXPERT
Top Expert 2010

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

Author

Commented:
thanks for response
SAM ITAD windows Admin  

Author

Commented:
can we build the script using WMI so we can run against 2003 servers...

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions