Powershell过滤无法识别变量

时间:2019-02-14 15:39:56

标签: powershell filter window

嗨,我有下面的代码将组分配给用户

$User1 = Read-Host -Prompt 'Enter the username of the employee you wish to change'
    $CountryList = Read-Host -Prompt 'Enter Country ISO - PL, CZ, HU, SE, 

RO'
    $CountryList = Read-Host -Prompt 'Enter Country ISO - USA, CZ, HU, SE, RO'
    Disable-AdAccount -Identity  $User1
    Set-aduser $User1 -clear manager
    Get-ADUser -Identity $User1 -Properties MemberOf | ForEach-Object {
    $_.MemberOf | Remove-ADGroupMember -Members $_.DistinguishedName -Confirm:$false |

    $Group_Name = Get-ADGroup -Filter {Name -like "*#GG-$CountryList-O365-EXCHANGE"}
    Add-ADGroupMember -Identity $Group_Name -Members $User1
}

问题是过滤器无法识别变量$ CountryList

我该如何解决?

1 个答案:

答案 0 :(得分:1)

我将在此处使用另一个变量,并将其传递给$Group_Name

$CountryList = Read-Host -Prompt 'Enter Country ISO - USA, CZ, HU, SE, RO'
$Filter = '*#GG-'+$CountryList+"-O365-EXCHANGE"

$Group_Name = Get-ADGroup -Filter {Name -like $Filter}