其中对象基于可变内容

时间:2014-10-10 09:39:13

标签: powershell

我的Office365 Powershell脚本存在问题

$expireUser = Get-MsolUser | Where-Object {$_.PasswordNeverExpires -eq $false}

$expireUser | ft DisplayName, UserPrincipalName, PasswordNeverExpires

$userinput = Read-Host 'Users in the table have the parameter PasswordNeverExpires set on $False. Set to $True? (Y/N)'
If ($userinput -eq "Y"){
    Get-MsolUser | Where-Object {$_.PasswordNeverExpires -eq $false} | Set-MsolUser -PasswordNeverExpires $True
    write-host "Down are visible the parameters of the users modified." 
    $expireUserCheck =  Get-MsolUser | Where-Object {$_.UserPrincipalName -eq $expireUser.UserPrincipalName}
    $expireUserCheck | ft DisplayName, UserPrincipalName, PasswordNeverExpires
    }
Elseif ($userinput -eq "N")
    {write-host "No change are made"}
Else    
    {write-host "Input not managed"}

每次更改参数的过程都是正确的,因此脚本的基本功能都可以,但问题是只有当1个用户拥有参数集时,检查才能显示带有DisplayName,UserPrincipalName和$ True的表。在PasswordNeverExpire下。 如果2个或更多用户在$ expireUser变量中,则必须显示第二个表时没有任何内容。

有什么想法吗?

非常感谢, 马可

1 个答案:

答案 0 :(得分:0)

使用以下命令创建UserPrincipalNames数组:

$arrayOfuserPrincipalnames = $expireUser | Select -ExpandProperty UserPrincipalName

然后改变这一行:

$expireUserCheck =  Get-MsolUser | Where-Object {$_.UserPrincipalName -eq $expireUser.UserPrincipalName}

为:

$expireUserCheck =  Get-MsolUser | Where-Object {$_.UserPrincipalName -in $arrayOfUserPrincipalnames}
相关问题