如何使用IF语句检查AD用户的订阅

时间:2018-01-19 21:01:37

标签: powershell azure

我一直在尝试获取一个脚本来检查特定订阅中的用户的Azure AD,如果是,那么继续,如果没有则终止脚本。

我假设选择订阅将指定要使用的Azure AD

Select-AzureRmSubscription -SubscriptionName "The Main Sub"

然后我会使用

Get-AzureRmADUser | fl

但是我被卡住了,我得到显示名称值和upn

将UPN分配给变量

    $x = user01@outlook.com
    if ($x -eq $null) {
"User not valid" Exit 
}
    Else
{
Continue
}

后台是用户启动此脚本,它必须验证用户属于正确的AD \ Subscription,然后脚本将继续,但随后用User01@outlook.com登录

请帮助:)

1 个答案:

答案 0 :(得分:1)

$upn = "user01@mydomain.com"
$user = Get-AzureRmADUser -UserPrincipalName $upn 
if ($user -ne $null) 
{
"User exists in Azure AD"
} Else {
"User not found in Azure AD"
}