Powershell脚本,用于检查远程计算机列表中是否存在文件

时间:2013-08-09 20:35:12

标签: powershell

我是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}} 
   }

第一部分(检查文件是否存在,运行没有问题。但是在检查进程是否正在运行时我有一个例外:

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

任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:18)

等式比较运算符为-eq,而不是eq。 PowerShell中的布尔值“true”为$true。如果要将Test-Path的结果与您的方式进行比较,则必须在子表达式中运行cmdlet,否则-eq "True"将被视为附加选项eq cmdlet的参数"True"

改变这个:

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

进入这个:

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

或者(更好),因为Test-Path已经返回一个布尔值,只需执行以下操作:

if (Test-Path "\\$_\c$\Windows\svchosts")
相关问题