简体   繁体   English

自动删除一段时间内未访问实验室 VM 的用户

[英]Automatically remove users that haven't been accessed a lab VM for a period of time

So let me preface this by saying that I'm still a newbie to Azure, an don't know much PowerShell (yet) .因此,让我先说明一下,我仍然是 Azure 的新手,对 PowerShell (还)了解不多。

I've started working tech support for an IT training company who uses Azure Labs for training VMs, and one of our regular tasks is to go through the labs and remove users who are no longer using a VM, otherwise the lab slots fill up, eventually locking out new users.我已经开始为一家使用 Azure 实验室训练 VM 的 IT 培训公司提供技术支持,我们的常规任务之一是通过实验室到 go 并删除不再使用 VM 的用户,否则实验室插槽会填满,最终将新用户拒之门外。

However, to do this currently, we have to cross reference users emails in Labs backend to our LMS, to see when a user last accessed a course, which is an incredibly manual and tedious process, as there's no way to see when a VM was last accessed in Labs directly, let alone easily check every VM/user in bulk.然而,目前要做到这一点,我们必须将实验室后端中的用户电子邮件交叉引用到我们的 LMS,以查看用户上次访问课程的时间,这是一个令人难以置信的手动和繁琐的过程,因为无法查看虚拟机何时访问最后直接在实验室中访问,更不用说轻松地批量检查每个虚拟机/用户了。

I thought surely this is a common enough occurrence that this is a solved problem, but I've been searching for an answer for the last couple of days, and nothing.我认为这肯定是很常见的事情,这是一个已解决的问题,但过去几天我一直在寻找答案,但一无所获。 I don't know if I'm missing something, or if my company has things setup wrong, or if what I'm asking is simply not possible for some reason?我不知道我是否遗漏了什么,或者我的公司是否设置有误,或者我所问的是否由于某种原因根本不可能?

Before I answer the PowerShell question directly, let me call out a few things.在我直接回答 PowerShell 问题之前,让我说几件事。 The Azure Lab Services Teams integration automatically syncs the Teams group to the user list. Azure 实验室服务团队集成自动将团队组同步到用户列表。 With Azure Lab Services April 2022 Update (preview), there is Canvas LMS integration which will sync the course roster with the lab user list.通过 Azure 实验室服务 2022 年 4 月更新(预览),有Canvas LMS 集成,它将课程名册与实验室用户列表同步。 If your not using one of these LMS, which I'm assuming is the case, then you still need to manually sync the user list if you want to keep using the same lab.如果您没有使用这些 LMS 之一(我假设是这种情况),那么如果您想继续使用同一个实验室,您仍然需要手动同步用户列表。

The other thing I wanted to note, was possible workflows using Azure Lab Services.我想指出的另一件事是使用 Azure 实验室服务的可能工作流程。 It sounds like your company is using the same lab and just adds or removes users from session to session. Another possible workflow that I've seen used by training companies using Azure Lab Services is to create a new lab for every session and then delete it when the session is over.听起来贵公司正在使用同一个实验室,只是在 session 到 session 之间添加或删除用户。我见过使用 Azure 实验室服务的培训公司使用的另一种可能的工作流程是为每个 session 创建一个新实验室,然后将其删除当 session 结束时。 The template image for a lab can be re-used if the lab account has an attached Azure Compute Gallery and the template image has been saved to the gallery .如果实验室帐户附加了 Azure 计算库并且模板图像已保存到库中,则可以重复使用实验室的模板图像。

Okay, now let's get into PowerShell commands in case none of the previous workflows meets your company's requirements.好的,现在让我们进入 PowerShell 命令,以防之前的工作流程都不符合贵公司的要求。 I'm guessing you want to automate to following steps:我猜您想自动执行以下步骤:

  1. Get user information for the labs获取实验室的用户信息
  2. Take information from #1 and create to-be-removed user information list.从#1 中获取信息并创建待删除的用户信息列表。
  3. Remove all users from list in #2.从 #2 中的列表中删除所有用户。

The PowerShell is different depending on which version of Azure Lab Services you are using. PowerShell 因您使用的 Azure 实验室服务版本而异。 Let me start with the GA'ed version of the service (pre-April 2022 Update).让我从服务的 GA'ed 版本开始(2022 年 4 月更新前)。 This using the Az.LabServices available from GitHub, not the built-in version.这使用GitHub提供的 Az.LabServices,而不是内置版本。 The PowerShell code for #1 would look something like the following: #1 的 PowerShell 代码如下所示:

#1. Get all users in all labs for the subscription
$userList = @(Get-AzLabAccount | Get-AzLab | Get-AzLabUser | Select-Object -Property LabAccountName,LabName,Name,@{N="Email";E={$_.properties.email}}} ) 

#2. Create '$toDeleteUserList'
#TODO: Add logic to match userList information with list of student emails that need to be removed
$toDeleteUserList = @() 

#3. Remove Users
$toDeleteUserList | ForEach-Object {Get-AzLabAccount -LabAccountName $_.LabAccountName | Get-AzLab -LabName $_.LabName | Remove-AzLabUser -User @{"name"=$_.name} }

If your using the April 2022 Update (preview), then you'll be using the built-in module.如果您使用 2022 年 4 月更新(预览版),那么您将使用内置模块。 (Import-Module Az.LabServices ). (导入模块Az.LabServices )。 The code changes to:代码更改为:

#1. Get all users in all labs for the subscription
$userList = @(Get-AzLabServicesLabPlan | Get-AzLabServicesLab | Get-AzLabServicesUser | Select -Property Id, Email) 

#2. Create '$toDeleteUserList'
#TODO: Add logic to match userList information with list of student emails that need to be removed
$toDeleteUserList = @() 

#3. Remove Users
$toDeleteUserList | ForEach-Object { Remove-AzLabServicesUser -ResourceId $_.Id}

Hope that helps, Elizabeth希望有帮助,伊丽莎白

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM