Write-Host 没有正确返回我的变量数据
Write-Host is not returning my variable data properly
我正在尝试使用 CSV 文件将用户列表添加到我在租户中设置的 AzureAD 组。该脚本似乎正在运行,但由于某种原因,我的一个 If 语句中的一个特定写入主机没有按预期显示我的变量数据。我在所有其他写入主机中使用相同的变量,它们都可以工作,所以我不确定我在这里遗漏了什么。
如果重要的话,我的 CSV 看起来像这样:
Name,InvitedUserEmailAddress
Test User01,testuser01@gmail.com
Test User02,testuser02@gmail.com
这是我简化的 PS 片段。
$users = import-csv "D:\UserListTest1.csv"
$groupID = Get-AzureADGroup -SearchString "TestGroup" | Select-Object ObjectId, displayname
foreach ($email in $users) {
# Pull usersAAD email list from AzureAD
$usersAAD = Get-AzureADUser -SearchString $($email.InvitedUserEmailAddress) | Select-Object ObjectId, displayname, mail
# Users from CSV not in AzureAD
if ($usersAAD.mail -eq $null) {
Write-Host "User $($usersAAD.displayname) does not exist in AzureAD" -ForegroundColor Red
}
else {
# Pull AzureAD user group membership from users that exist in AzureAD
$ExistingGroups = Get-AzureADUserMembership -ObjectId $usersAAD.ObjectId | Select-Object displayname, objectid
# Users that are already members of the AzureAD group
if ($ExistingGroups.ObjectId -eq $groupID.objectId) {
Write-Host "$($usersAAD.displayname) already exists in $($groupID.displayname)" -ForeGroundColor Yellow
}
else {
# Add users to AzureAD group if they are not already part of AzureAD group
Add-AzureADGroupMember -ObjectId $groupID.ObjectId -RefObjectId $usersAAD.ObjectId
Write-Host "Added $($usersAAD.displayname) to $($GroupID.displayname)" -ForeGroundColor Green
}
}
}
问题出在以下 If 语句的写入主机结果,当用户已经在组中时会发生这种情况。
# Users from CSV not in AzureAD
if ($usersAAD.mail -eq $null) {
Write-Host "User $($usersAAD.displayname) does not exist in AzureAD" -ForegroundColor Red
}
在我的示例中,我的 AzureAD 租户中不存在 testuser02@gmail.com,因此我期待一个红色文本显示“AzureAD 中不存在用户 testuser02@gmail.com”用户。相反,我看到了以下输出。测试 User01 工作正常,但我的测试 User02 却不行。抱歉格式错误。
Test User01 already exists in TestGroup
User does not exist in AzureAD
为什么对于已经属于该组的用户它会有一个空值?它甚至在输出中添加了一个 space 。我也尝试删除 .displayname 对象,但没有任何帮助。
可能与它有关的一件奇怪的事情是我的 $usersAAD 变量在我 运行 整个事情之后似乎是空的。如果我在整个 运行 之后执行 write-host $usersAAD(即使它正确地邀请了用户),它也不会 return 任何结果。
如评论中所述,当条件 $usersAAD.mail -eq $null
为 $true
时,"User $($usersAAD.displayname) does not...
指的是不存在的 object ($null
),这就是为什么在你的输出中你得到 User does not exist in AzureAD
。要解决此问题,您可以参考 collection (Csv) 中的项目 ($email
)。
这是我对您的代码的看法,其中包括修复程序以及一些内联注释,以帮助您进行思考。
$groupID = Get-AzureADGroup -SearchString "TestGroup"
foreach ($email in Import-Csv "D:\UserListTest1.csv") {
# if this user exists in Azure AD
if ($usersAAD = Get-AzureADUser -SearchString $email.InvitedUserEmailAddress) {
# get the membership
$ExistingGroups = Get-AzureADUserMembership -ObjectId $usersAAD.ObjectId
# and check if the test group is part of the user membership
# (notice `-contains` here is faster than `-eq` !!!)
if ($ExistingGroups.ObjectId -contains $groupID.objectId) {
Write-Host "$($usersAAD.displayname) already exists in $($groupID.displayname)" -ForeGroundColor Yellow
# if this condition was `$true` just go to the next item in our loop
continue
}
# if we're here above condition was `$false`, so add this user to the test group
Add-AzureADGroupMember -ObjectId $groupID.ObjectId -RefObjectId $usersAAD.ObjectId
Write-Host "Added $($usersAAD.displayname) to $($GroupID.displayname)" -ForeGroundColor Green
# and go to next item in loop
continue
}
# if we're here we can assume the user did not exist in Azure AD, hence:
Write-Host "User $($email.Name) does not exist in AzureAD" -ForegroundColor Red
}
我正在尝试使用 CSV 文件将用户列表添加到我在租户中设置的 AzureAD 组。该脚本似乎正在运行,但由于某种原因,我的一个 If 语句中的一个特定写入主机没有按预期显示我的变量数据。我在所有其他写入主机中使用相同的变量,它们都可以工作,所以我不确定我在这里遗漏了什么。
如果重要的话,我的 CSV 看起来像这样:
Name,InvitedUserEmailAddress
Test User01,testuser01@gmail.com
Test User02,testuser02@gmail.com
这是我简化的 PS 片段。
$users = import-csv "D:\UserListTest1.csv"
$groupID = Get-AzureADGroup -SearchString "TestGroup" | Select-Object ObjectId, displayname
foreach ($email in $users) {
# Pull usersAAD email list from AzureAD
$usersAAD = Get-AzureADUser -SearchString $($email.InvitedUserEmailAddress) | Select-Object ObjectId, displayname, mail
# Users from CSV not in AzureAD
if ($usersAAD.mail -eq $null) {
Write-Host "User $($usersAAD.displayname) does not exist in AzureAD" -ForegroundColor Red
}
else {
# Pull AzureAD user group membership from users that exist in AzureAD
$ExistingGroups = Get-AzureADUserMembership -ObjectId $usersAAD.ObjectId | Select-Object displayname, objectid
# Users that are already members of the AzureAD group
if ($ExistingGroups.ObjectId -eq $groupID.objectId) {
Write-Host "$($usersAAD.displayname) already exists in $($groupID.displayname)" -ForeGroundColor Yellow
}
else {
# Add users to AzureAD group if they are not already part of AzureAD group
Add-AzureADGroupMember -ObjectId $groupID.ObjectId -RefObjectId $usersAAD.ObjectId
Write-Host "Added $($usersAAD.displayname) to $($GroupID.displayname)" -ForeGroundColor Green
}
}
}
问题出在以下 If 语句的写入主机结果,当用户已经在组中时会发生这种情况。
# Users from CSV not in AzureAD
if ($usersAAD.mail -eq $null) {
Write-Host "User $($usersAAD.displayname) does not exist in AzureAD" -ForegroundColor Red
}
在我的示例中,我的 AzureAD 租户中不存在 testuser02@gmail.com,因此我期待一个红色文本显示“AzureAD 中不存在用户 testuser02@gmail.com”用户。相反,我看到了以下输出。测试 User01 工作正常,但我的测试 User02 却不行。抱歉格式错误。
Test User01 already exists in TestGroup
User does not exist in AzureAD
为什么对于已经属于该组的用户它会有一个空值?它甚至在输出中添加了一个 space 。我也尝试删除 .displayname 对象,但没有任何帮助。
可能与它有关的一件奇怪的事情是我的 $usersAAD 变量在我 运行 整个事情之后似乎是空的。如果我在整个 运行 之后执行 write-host $usersAAD(即使它正确地邀请了用户),它也不会 return 任何结果。
如评论中所述,当条件 $usersAAD.mail -eq $null
为 $true
时,"User $($usersAAD.displayname) does not...
指的是不存在的 object ($null
),这就是为什么在你的输出中你得到 User does not exist in AzureAD
。要解决此问题,您可以参考 collection (Csv) 中的项目 ($email
)。
这是我对您的代码的看法,其中包括修复程序以及一些内联注释,以帮助您进行思考。
$groupID = Get-AzureADGroup -SearchString "TestGroup"
foreach ($email in Import-Csv "D:\UserListTest1.csv") {
# if this user exists in Azure AD
if ($usersAAD = Get-AzureADUser -SearchString $email.InvitedUserEmailAddress) {
# get the membership
$ExistingGroups = Get-AzureADUserMembership -ObjectId $usersAAD.ObjectId
# and check if the test group is part of the user membership
# (notice `-contains` here is faster than `-eq` !!!)
if ($ExistingGroups.ObjectId -contains $groupID.objectId) {
Write-Host "$($usersAAD.displayname) already exists in $($groupID.displayname)" -ForeGroundColor Yellow
# if this condition was `$true` just go to the next item in our loop
continue
}
# if we're here above condition was `$false`, so add this user to the test group
Add-AzureADGroupMember -ObjectId $groupID.ObjectId -RefObjectId $usersAAD.ObjectId
Write-Host "Added $($usersAAD.displayname) to $($GroupID.displayname)" -ForeGroundColor Green
# and go to next item in loop
continue
}
# if we're here we can assume the user did not exist in Azure AD, hence:
Write-Host "User $($email.Name) does not exist in AzureAD" -ForegroundColor Red
}