为什么带有多个-AND条件的If语句不能正常运行?

时间:2019-05-30 06:45:15

标签: powershell

我想在我的Powershell If语句中满足多个条件,并尝试使用-AND运算符来实现它,但是它似乎只是直接跳转到else子句。

在下面的代码中,我要确保两个文件夹都没有文件,并且“ watcher.txt”文件存在。

$Success = "C:\temp\Success"
$Failed = "C:\temp\failed"
$watcher = "C:\temp\watcher.txt"

$directoryInfo3 = get-childitem "$Success\\*" -file | Measure-Object
$directoryInfo3.count
$directoryInfo4 = get-childitem "$Failed\\*" -file | Measure-Object
$directoryInfo4.count
    if ((test-path -path $Watcher) -AND 
        ($directoryInfo3 -eq 0) -AND 
        ($directoryInfo4 -eq 0)){
         Write-host "Success folder and Failed folder are empty, and Watcher file exists."}
         else
         {Write-host "Not complete yet, rechecking folders."}

如果所有三个语句都为真,那么我应该看到“成功文件夹和失败文件夹为空,并且存在Watcher文件。”

相反,我看到的是“尚未完成,正在重新检查文件夹”。不管条件是什么,即使三个条件都成立。

2 个答案:

答案 0 :(得分:3)

您需要调用$directoryInfo3.count才能使该语句为true。 即:

PS C:\> $dir = gci c:\temp\ -file | measure
PS C:\> $dir


Count    : 36
Average  :
Sum      :
Maximum  :
Minimum  :
Property :

PS C:\> $dir -eq 36
False
PS C:\> $dir.count -eq 36
True

答案 1 :(得分:1)

由于您似乎对@Component({ selector: 'app-a', template: ` <h1>A</h1> <p> params: {{params | json}}</p> <p> query params: {{queryParams | json}}</p> <p> url: {{url}}</p> <p> full url: {{fullUrl}}</p>` }) export class AComponent { params; queryParams; url; fullUrl; constructor(private route: ActivatedRoute, private router: Router ) { } ngOnInit() { this.params = this.route.snapshot.params; this.queryParams = this.route.snapshot.queryParams; this.url = this.route.snapshot.url.join(''); this.fullUrl = this.router.url; } } 的数量或内容不感兴趣,
您可以在所有3种情况下都使用Get-ChildItem

Test-Path

此处## Q:\Test\2019\05\30\SO_56373080.ps1 $Success = "C:\temp\Success" $Failed = "C:\temp\failed" $watcher = "C:\temp\watcher.txt" if ( (Test-Path -Path $Watcher -PathType Leaf) -AND !(Test-Path -Path $Success\* -PathType Leaf) -AND !(Test-Path -Path $Failed\* -PathType Leaf)){ Write-host "Success folder and Failed folder are empty, and Watcher file exists." } else { Write-host "Not complete yet, rechecking folders." } =不反转布尔结果。