Powershell script Error message

I am running the following code and on execution of the code I am getting an error.
$computers = Import-CSV 'C:\scripts\servers.txt'  

foreach($computer in $computers)
{
    write-host $computer.computername $computer.service $computer.operation
 $objWMIService = Get-WmiObject -Class win32_service -computer $computer.computername -filter "name = '$($computer.service)'"

The error message tells me that $computer.computername
error text is as follows...
Cannot validate argument on parameter 'ComputerName'. The argument is null or empty. Supply an argument that is not null or empty and then try the
command again.
I have tried giving a value but am not understanding this..
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.

piattndCommented:
What is it you're trying to do within powershell?  Knowing your end goal can help us identify the changes you need to make.

As I see it, you've got an array of computers loaded from a text file in $computers.

You then go through the array looking at each computer as $computer.

You then try to reference a .computername and .service from the $computer variable, which it doesn't have.  You're likely wanting to pull information about the computer by using WMI, so your flow needs to be something more like this:

foreach($computer in $computers)
{
$objWMIService = Get-WMIObject - Class win32_service -computer $computer
foreach ($service in $objWMIService)
{
//Do something with the service you found
}
}
0
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
From prior questions (Experts: there is a history of related questions) I take it you are really using a CSV file, not a plain text file containing only computer names. Else you won't need the Import-CSV cmdlet.
So the issue is, as stated above, that $computers does not have the (proper) properties. Likely you didn't start your CSV file with a header, like in
ComputerName, Service, Operation
PC1, W3Svc, Stop

Open in new window

or you used a different delimiter (e.g. semicolon), which you have to provide as -Delimiter parameter on import. Or you didn't name the header in the file correctly, e.g. Computer instead of ComputerName.
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
AjarnJonesyAuthor Commented:
I got ya... All I am doing is importing a server.txt and checking if service is stopped, if stopped then doing a start.  So if I am understanding correctly I need to add A            foreach(.$service in $objWMIService)

foreach($computer in $computers)
{
    $objWMIService = Get-WmiObject -Class win32_service -computer $computer.computername -filter "name = '$($computer.service)'"
         #    if($computer.operation -eq "service")
    {
        if($objWMIService.state -eq "Stopped")
        {
            $objWMIService.StartService() | Out-Null;
            # Refresh the object instance
            $state = (Get-WmiObject Win32_Service -Filter "Name = '$($computer.service)'" -computer $computer.computername).state
}
}
}
0
Newly released Acronis True Image 2019

In announcing the release of the 15th Anniversary Edition of Acronis True Image 2019, the company revealed that its artificial intelligence-based anti-ransomware technology – stopped more than 200,000 ransomware attacks on 150,000 customers last year.

piattndCommented:
Nope, you're not quite understanding.

You are telling the script that $computer is a computer name you have listed in the text file.  Lets say your text file has this:

comp1
comp2
comp3

So the first time it passes through the loop, $computer = comp1.  $computer.computername doesn't exist, so you can't reference that.  $computer.service doesn't exist either, so you can't reference that.

Steps to do this:

-Get the computer name (done by your foreach($computer in $computers) section).
-Connect to the computer via WMI to find out if a service is running.
-Perform any additional work depending on if the service is running.

Try this:

foreach($computer in computers)
{
  $objService = Get-WmiObject -Class win32_service -computer $computer -filter "name='netlogon'"

  if($objService.state = "stopped")
  {
  }
  else
  {
  }
}

Open in new window


Please keep in mind I have not tried this code inside powershell, so it may need to be tweaked.

This link explains what you're trying to accomplish.

http://www.pixelchef.net/powershell-script-find-status-service-remote-computer
0
piattndCommented:
This is another link where he describes getting the actual service:

http://apprepack.blogspot.com/2012/07/get-status-of-service-on-remote-machine.html
0
AjarnJonesyAuthor Commented:
Bingo.. That was it ... I added the foreach(.$service  and corrected the header... Thanks
0
piattndCommented:
Ahh yeah I misunderstood then, my fault.  Good catch Qlemo.
0
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
Slowly, AjarnJonesy. foreach (.$service is not even part of a valid syntax. In PowerShell there is always an object (variable) in front of the dot used to access a property (different from VB, where you can use the WITH statement to implement an "invisible" shortcut reference).
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
Windows Server 2008

From novice to tech pro — start learning today.