小编Mic*_*ael的帖子

在PowerShell中使用带有变量的"-Filter"

我试着过滤掉这样的东西:

Get-ADComputer -Filter {name -like "chalmw-dm*" -and Enabled -eq "true"} ...
Run Code Online (Sandbox Code Playgroud)

这就像一个魅力,正是我想要的......

现在我希望"name-like ..."部分作为这样的变量:

Get-ADComputer -Filter {name -like '$nameregex' -and Enabled -eq "true"} |
Run Code Online (Sandbox Code Playgroud)

我检查了几个问题(例如,PowerShell AD模块 - 过滤器中的变量),但这对我不起作用.

我尝试了以下内容:

$nameRegex = "chalmw-dm*"
$nameRegex = "`"chalmw-dm*`""
Run Code Online (Sandbox Code Playgroud)

而且在Get-ADComputer与那些'和没有的命令中.

谁能给我一些提示?

powershell filter

7
推荐指数
1
解决办法
11万
查看次数

查找array1中但不在array2中的项目

我的电脑中提取两列由两种不同的工具,Array1Array2.

现在我需要提取那些Array1但不在其中的那些Array2.

我设法通过这样做获得所有匹配的:

$matchingComp = @()

foreach ($SCCMcomputer in $SCCMcomputers) {
    foreach ($eWPTcomputer in $eWPTcomputers) {
        if ($SCCMcomputer.Computername -eq $eWPTComputer.Computername) {
            $obj = New-Object PSObject
            $obj | Add-Member -MemberType NoteProperty -Name "ComputerName" -Value $SCCMcomputer.Computername
            $matchingComp +=$obj
        }
    }
}

$matchingComp | Export-Csv $inEWPT -Delimiter "," -NoTypeInformation
Run Code Online (Sandbox Code Playgroud)

但我仍然需要那些$SCCMcomputer但不在$eWPTcomputers......

我已经在SO上找到了一些与其他语言(例如Perl)的解决方案,但对于PowerShell没有.

UPDATE

我仍然无法使用以下公式在Excel中获得正确的输出:

在此输入图像描述

输出看起来像:

在此输入图像描述

意味着有些人在这里,有些则没有.powershell中的输出是这样的

在此输入图像描述

表示0KB是emtpy.

$SCCMcomputers | Export-Csv $sccmexport -Delimiter "," -NoTypeInformation
$eWPTcomputers | Export-Csv $ewptexport -Delimiter "," -NoTypeInformation
Compare-Object …
Run Code Online (Sandbox Code Playgroud)

arrays powershell foreach

4
推荐指数
1
解决办法
2万
查看次数

标签 统计

powershell ×2

arrays ×1

filter ×1

foreach ×1