Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell 如何确定变量是否返回可比较语句_Powershell - Fatal编程技术网

Powershell 如何确定变量是否返回可比较语句

Powershell 如何确定变量是否返回可比较语句,powershell,Powershell,我需要确定$locations是否为真。如果为true,则执行第一个操作,如果为false,则执行第二个操作。我遇到的问题是,我找不到一种方法来确定$locations是否为真(等于匹配的正则表达式)。以前遇到过这个问题,if如果变量存在,则返回true,并设置为非假值(不一定为真,而是某个值) 这样做: if ((Get-ChildItem "I:\temp").Length -ne 0) { $locations = (Get-Item "I:\temp"), (Get-ChildI

我需要确定
$locations
是否为真。如果为true,则执行第一个操作,如果为false,则执行第二个操作。我遇到的问题是,我找不到一种方法来确定
$locations
是否为真(等于匹配的正则表达式)。

以前遇到过这个问题,
if
如果变量存在,则返回
true
,并设置为非假值(不一定为真,而是某个值)

这样做:

if ((Get-ChildItem "I:\temp").Length -ne 0) {
    $locations = (Get-Item "I:\temp"), (Get-ChildItem "I:\temp" -Directory -Recurse) | % {
        Get-ChildItem -File $_.FullName |
            Select-String -List -Pattern '^\d{3}-?\d{2}-?\d{4}$' | 
            % Path
    }

    if ($locations) {
        $locations | Out-File "I:\temp\SSN_FILES.txt"
    } else {
        Get-ChildItem "I:\temp" -Exclude "fullpath.txt", "SSN_FILES.txt" | Remove-Item
    }
}

我以前有过,但我不相信这是有效的。原因是我不认为$locations返回布尔语句。我以前有过这个,我不相信它是按我预期的方式工作的。文件中的输出是
True
$locations
应该具有与regex相等的文件路径。您要进行的测试可能是
$locations-ne$null
?如果它包含值,则不应为null。如果正则表达式未返回任何值,则应为null。此操作有效。我想这样做,但没想到会奏效。。。hehehe@schnipdip甜-编辑成ans。这仍然是我的痛点;注意
$locations
变量中捕获的错误/意外输出,以及
-ne$null
测试返回true
if($locations).
测试是否为
$null
或零,或等效值。您可能应该看看
-match
操作符(请参阅)。。
if($locations -eq $true){ # to check it is set to the boolean true
if($locations -ne $null){ # to check if it contains a value