gpt4 book ai didi

azure - 在 Azure Function App 中使用 PowerShell 连接到不同的 Azure AD 租户

转载 作者:行者123 更新时间:2023-12-03 01:41:37 25 4
gpt4 key购买 nike

我正在尝试创建一个系统,其中 PowerShell 从多个租户收集数据并在报告中显示。需要检查的数据点之一是管理员是否启用了 MFA。为了提取这些数据,我使用以下内容

$credentials = <credentials>;    
Connect-MSOLService -Credential $credentials;

foreach ($role in Get-MsolRole) {
foreach ($adminUser in (Get-MsolRoleMember -All -RoleObjectId $role.ObjectId -MemberObjectTypes @("User"))) {
$isMFA = ($adminUser.StrongAuthenticationRequirements -match 'Microsoft.Online.Administration.StrongAuthenticationRequirement').Count -gt 0;
#Do stuff
}
}

这有效。问题是,该脚本正在队列触发的 azure 函数中运行。它是在计时器上触发的,这意味着所有触发器将同时运行。建立第一个连接后,所有其他数据请求都会从同一租户提取数据。

有什么方法可以确保每个请求都有自己的连接,或者限制 msol 连接的范围?

我能想到的唯一解决方案是同步运行脚本,但这会导致性能非常差。

最佳答案

事实上,当由队列触发时,同一函数的多次运行似乎根本不是孤立的。

您可以采取的一种方法是使用 Start-Job 将应独立运行的 PowerShell 代码包装到其自己的 PowerShell 作业中。这是我测试成功的示例。

# Receive queue message
$input = Get-Content $queueItem -Raw

# Pass the input queue message as a parameter to a new job.
$job = Start-Job -ArgumentList $input -ScriptBlock {

param($queueMessage)

# Load the MSOnline PowerShell module
Import-Module $env:CONTOSO_PathToMSOnline

# Retrieve the credentials from where they're securely stored
$credentials = ... # e.g. get from Key Vault

# Connect to Azure AD. This connection is only used by this job.
Connect-MsolService -Credential $credentials

# Do something with MSOnline...
}

# Wait for the job to complete, receive results, then clean up.
Receive-Job -Wait -Job $job -AutoRemoveJob

根据我的测试,这应该可以满足您的隔离需求。但是,请记住,您为此启动了一个全新的 PowerShell 主机实例,这可能会产生意想不到的后果(例如,内存使用量更大、加载时间更长)。

在此期间,我想建议对您的流程进行调整,以识别启用了每用户 MFA 的管理员(假设您不想重复计算属于多个角色的成员的管理员) :

# Iterate over all admins of all roles, and check if they have per-user MFA enabled.

$admins = @{} # To keep track of which admins we've already seen

foreach ($role in Get-MsolRole) {

$roleMembers = Get-MsolRoleMember -All -RoleObjectId $role.ObjectId `#`
-MemberObjectTypes @("User")

foreach ($user in $roleMembers) {

if ($admins.ContainsKey($user.ObjectId)) {
# We've already seen this user, skip it.
} else {
$admins[$user.ObjectId] = $true # Mark as admin we've seen

# Determine if per-user MFA is enabled or enforced
$isMfaEnabledOrEnforced = $user.StrongAuthenticationRequirements.Count -gt 0

# Do something...
}
}
}

关于azure - 在 Azure Function App 中使用 PowerShell 连接到不同的 Azure AD 租户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52778063/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com