Powershell script to check if a file exists in the list of remote computers

I am new to Powershell and I am trying to write a script that checks if a file exists; if he does, he checks to see if the process is running. I know there are much better ways to write this, but can someone please give me an idea? Here is what I have:

Get-Content C:\temp\SvcHosts\MaquinasEstag.txt | ` Select-Object @{Name='ComputerName';Expression={$_}},@{Name='SvcHosts Installed';Expression={ Test-Path "\\$_\c$\Windows\svchosts"}} if(Test-Path "\\$_\c$\Windows\svchosts" eq "True") { Get-Content C:\temp\SvcHosts\MaquinasEstag.txt | ` Select-Object @{Name='ComputerName';Expression={$_}},@{Name='SvcHosts Running';Expression={ Get-Process svchosts}} } 

The first part (check if the file exists, works without problems. But I have an exception, checking if the process is running:

 Test-Path : A positional parameter cannot be found that accepts argument 'eq'. At C:\temp\SvcHosts\TestPath Remote Computer.ps1:4 char:7 + if(Test-Path "\\$_\c$\Windows\svchosts" eq "True") + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidArgument: (:) [Test-Path], ParameterBindingException + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.TestPathCommand 

Any help would be appreciated!

+8
powershell
source share
1 answer

The comparison operator is -eq , not eq . The boolean value "true" in PowerShell $true . And if you want to compare the result of Test-Path with something the way you do it, you must run the cmdlet in a subexpression, otherwise -eq "True" will be considered as an additional eq option with the argument "True" cmdlet.

Change this:

 if(Test-Path "\\$_\c$\Windows\svchosts" eq "True") 

in it:

 if ( (Test-Path "\\$_\c$\Windows\svchosts") -eq $true ) 

Or (even better), since Test-Path already returns a boolean, just do the following:

 if (Test-Path "\\$_\c$\Windows\svchosts") 
+18
source share

All Articles