9

私は 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

どんな助けでも大歓迎です!

4

1 に答える 1

20

等値比較演算子は-eq, ではありませんeq。PowerShell のブール値「true」は$true. Test-Pathまた、結果を何かと比較したい場合は、部分式でコマンドレットを実行する必要があります。そうしないと、コマンドレットへの引数を持つ-eq "True"追加オプションとして扱われます。eq"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")
于 2013-08-09T20:47:16.877 に答える