我是Powershell的新手,我正在尝试编写一个检查文件是否存在的脚本; 如果是,则检查进程是否正在运行.我知道有更好的方法可以写这个,但任何人都可以给我一个想法吗?这就是我所拥有的:
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}}
}
Run Code Online (Sandbox Code Playgroud)
第一部分(检查文件是否存在,运行没有问题.但是在检查进程是否正在运行时我有一个例外:
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
Run Code Online (Sandbox Code Playgroud)
任何帮助,将不胜感激!
Ans*_*ers 19
等式比较运算符-eq不是eq.PowerShell中的布尔值"true"是$true.如果要以Test-Path某种方式比较结果,则必须在子表达式中运行cmdlet,否则-eq "True"将作为cmdlet eq参数的附加选项"True".
改变这个:
if(Test-Path "\\$_\c$\Windows\svchosts" eq "True")
Run Code Online (Sandbox Code Playgroud)
进入这个:
if ( (Test-Path "\\$_\c$\Windows\svchosts") -eq $true )
Run Code Online (Sandbox Code Playgroud)
或者(更好),因为Test-Path已经返回一个布尔值,只需执行以下操作:
if (Test-Path "\\$_\c$\Windows\svchosts")
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
46486 次 |
| 最近记录: |