IF声明不在Powershell中工作

Sea*_*ong 5 powershell if-statement powershell-2.0

我一直试图在我的小PowerShell v2脚本中使用IF-ELSE子句,我认为我的解析存在一些问题.这是我目前的代码:

$dir = test-path C:\Perflogs\TestFolder 

IF($dir -eq "False") 
{
New-Item C:\Perflogs\TestFolder -type directory
get-counter -counter $p -Continuous | Export-Counter  C:\PerfLogs\TestFolder\Client_log.csv -Force -FileFormat CSV -Circular -MaxSize $1GBInBytes
}
Else
{
get-counter -counter $p -Continuous | Export-Counter  C:\PerfLogs\TestFolder\Client_log.csv -Force -FileFormat CSV -Circular -MaxSize $1GBInBytes
}
Run Code Online (Sandbox Code Playgroud)

所以基本上我希望它建立$ dir变量作为测试,以查看我想要的路径是否存在.如果没有,它应该创建该文件夹并运行计数器.如果是,它不应该创建文件夹,但仍应运行计数器.

我在其他地方定义了$ p,并且get-counters语句工作正常.现在,无论文件夹是否存在,我都会收到有关新项目无效的错误.

在进行测试后,我是否使用了错误的运算符-eq?

x0n*_*x0n 12

你应该有:

if ($dir -eq $false) 
Run Code Online (Sandbox Code Playgroud)

因为字符串"False"不等于布尔值$false.