如何修复PowerShell代码以在阵列中搜索ADGroups?

7fhtutme  于 2023-05-22  发布在  Shell
关注(0)|答案(1)|浏览(139)

代码如下:

Clear-Host

Import-Module ActiveDirectory

$ErrorView = 'CategoryView'

Write-Host 'Searching for ADGroups...'

$ADGroups = @('Remote Desktop Users', 'M365 Migration')

If(Get-ADGroup -Filter "Name -In '$ADGroups'") {
    Write-Host 'ADGroups found:'
    Write-Host (Get-ADGroup -Filter "Name -In '$ADGroups'") -ForegroundColor Green
}
Else {
    Write-Host 'No ADGroups found' -ForegroundColor Red
}

If(Get-ADGroup -Filter "Name -Eq 'Remote Desktop Users'") {
    Write-Host 'Searching for RDLicenseConfiguration...'
    Write-Host (Get-RDLicenseConfiguration -ErrorAction Continue) -ForegroundColor Green

    Write-Host 'Searching for RDDeploymentGatewayConfiguration...'
    Write-Host (Get-RDDeploymentGatewayConfiguration -ErrorAction Continue) -ForegroundColor Green
}

If(Get-ADGroup -Filter "Name -Eq 'Remote Desktop Users'") {
    #M365 Migration
    Write-Host 'Searching for applocker policy...'
    Write-Host (Get-AppLockerPolicy -Local -ErrorAction Continue) -ForegroundColor Green
}

除了第13行的代码由于语法错误而不能正常运行外,它可以正常工作。
我现在得到的错误:

ParserError: (:) [Get-ADGroup], ADFilterParsingException

问题是第13行的代码,我尝试使用"Name -Like '$ADGroups*'",但也没有工作。
先谢谢你。

tp5buhyn

tp5buhyn1#

Active Directory filter不支持-in,我所要做的就是添加上面的foreach循环来循环数组中的每个对象,并将比较运算符更改为-eq,再次感谢Doug的答案。

Clear-Host

Import-Module ActiveDirectory

$ErrorView = "CategoryView"

Write-Host "Searching for ADGroups..."

$ADGroups = @("Remote Desktop Users", "M365 Migration")

Foreach($group In $ADGroups){
    If(Get-ADGroup -Filter "Name -Eq '$group'"){
        Write-Host "ADGroups found:"
        Write-Host (Get-ADGroup -Filter "Name -Eq '$group'" | Select-Object -ExpandProperty Name) -ForegroundColor Green
    }
}

If(Get-ADGroup -Filter "Name -Eq 'Remote Desktop Users'"){
    Write-Host "Searching for RDLicenseConfiguration..."
    Write-Host (Get-RDLicenseConfiguration -ErrorAction Continue) -ForegroundColor Green

    Write-Host "Searching for RDDeploymentGatewayConfiguration..."
    Write-Host (Get-RDDeploymentGatewayConfiguration -ErrorAction Continue) -ForegroundColor Green
}

If(Get-ADGroup -Filter "Name -Eq 'M365 Migration'"){
    Write-Host "Searching for applocker policy..."
    Write-Host (Get-AppLockerPolicy -Local -ErrorAction Continue) -ForegroundColor Green
}

相关问题