I'm new at Powershell, and I'm trying to write a script that checks if a file exists; if it does, it checks if a process is running. I know there are much better ways to write this, but can anyone please give me an idea? Here's 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, runs with no problem. But I have an exception when 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!
The equality comparison operator is -eq
, not eq
. The boolean value "true" in PowerShell is $true
. And if you want to compare the result of Test-Path
to something the way you do, you must run the cmdlet in a subexpression, otherwise -eq "True"
would be treated as an additional option eq
with the argument "True"
to the cmdlet.
Change this:
if(Test-Path "\\$_\c$\Windows\svchosts" eq "True")
into this:
if ( (Test-Path "\\$_\c$\Windows\svchosts") -eq $true )
Or (better yet), since Test-Path
already returns a boolean value, simply do this:
if (Test-Path "\\$_\c$\Windows\svchosts")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With