%3CLINGO-SUB%20id%3D%22lingo-sub-1565688%22%20slang%3D%22en-US%22%3EUsing%20Powershell%20to%20domain%20join%20Windows%2010%20Azure%20Lab%20Service%20VMs%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1565688%22%20slang%3D%22en-US%22%3E%3CP%3EThere%20are%20many%20benefits%20of%20having%20domain%20joined%20lab%20VMs%20in%20Azure%20Lab%20Services%2C%20including%20allowing%20the%20students%20to%20connect%20to%20the%20VM%20using%20their%20domain%20credentials.%26nbsp%3B%20Each%20VM%20can%20be%20joined%20to%20the%20domain%20manually%2C%20but%20this%20is%20tedious%20and%20doesn%E2%80%99t%20scale%20when%20domain%20joining%20multiple%20VMs.%26nbsp%3B%20To%20help%20with%20this%2C%20we%20have%20put%20together%20some%20Windows%2010%20Powershell%20scripts%20that%20a%20lab%20owner%20can%20execute%20on%20the%20so%20that%20every%20VM%20started%20in%20the%20lab%20is%20automatically%20joined%20to%20the%20domain%20and%20the%20student%20is%20added%20as%20a%20Remote%20Desktop%20user.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThe%20scripts%20use%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fpowershell%2Fmodule%2Fscheduledtasks%2Fregister-scheduledtask%3Fview%3Dwin10-ps%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%22%3EWindows%E2%80%99%20task%20scheduler%3C%2FA%3E%20to%20automatically%20run%20a%20script%20when%20the%20student%20VM%20starts%20up.%20The%20first%20script%2C%20which%20the%20lab%20owner%20executes%20on%20the%20template%20VM%2C%20registers%20a%20scheduled%20task%20that%20will%20run%20another%20script%20at%20VM%20start%20up.%20The%20script%20then%20publishes%20the%20template%20VM%20to%20create%20the%20students%E2%80%99%20lab%20VMs.%20When%20a%20student%E2%80%99s%20lab%20VM%20is%20started%20the%20first%20time%2C%20the%20script%20that%20executes%20the%20domain%20join%20will%20automatically%20run.%20I%20would%20recommend%20that%20the%20VM%20start%20and%20domain%20join%20occur%20before%20the%20students%20need%20to%20login%20as%20this%20may%20take%20several%20minutes.%20If%20you%20increase%20the%20lab%20capacity%20later%2C%20those%20VMs%20will%20use%20the%20same%20configuration%20from%20the%20template%20VM%20and%20will%20be%20domain%20joined%20when%20they%20are%20started%20the%20first%20time.%20However%2C%20if%20you%20change%20the%20template%20VM%2C%20such%20as%20by%20adding%20more%20software%20or%20changing%20the%20configuration%2C%20the%20first%20script%20will%20need%20to%20be%20run%20again%20to%20set%20up%20the%20scheduled%20tasks.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThe%20script%20will%20need%20a%20user%2C%20with%20password%2C%20that%20has%20permissions%20to%20join%20the%20domain%2C%20you%20may%20need%20to%20work%20with%20your%20IT%20department%20to%20get%20the%20necessary%20information.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3EThe%20Azure%20Lab%20Service%20team%20will%20be%20building%20this%20functionality%20directly%20into%20the%20product%2C%20in%20the%20meantime%20these%20scripts%20will%20allow%20you%20to%20move%20forward%20with%20Lab%20Services.%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH1%20id%3D%22toc-hId-500698453%22%20id%3D%22toc-hId-500698453%22%20id%3D%22toc-hId-500698453%22%3EEnvironments%3C%2FH1%3E%0A%3CP%3EThese%20scripts%20work%20on%20the%20following%20configurations%3A%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3EOn-premise%20Active%20Directory%20domain%3C%2FLI%3E%0A%3CLI%3EHybrid%20Active%20Directory%20domain%20-%20An%20on-premise%20AD%20which%20is%20connected%20to%20an%20Azure%20Active%20Directory%20through%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Factive-directory%2Fhybrid%2Fhow-to-connect-install-prerequisites%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%22%3EAzure%20AD%20Connect%3C%2FA%3E.%26nbsp%3B%20AD%20Domain%20Services%20is%20installed%20on%20an%20on-premise%20server%2C%20see%20the%20diagram%20below.%20Applies%20also%20to%20federated%20domains.%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22azure-ad-hybrid-joined-als-device.png%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F210262i4F13D85C22D8D0C2%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20title%3D%22azure-ad-hybrid-joined-als-device.png%22%20alt%3D%22azure-ad-hybrid-joined-als-device.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FLI%3E%0A%3CLI%3EAzure%20AD%20DS%20Domains%20-%20For%20full-cloud%20AD%20(Azure%20AD%20%2B%20Azure%20AD%20DS)%20or%20Hybrid%20AD%20with%20secondary%20Domain%20Services%20on%20Azure.%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CH1%20id%3D%22toc-hId--1306756010%22%20id%3D%22toc-hId--1306756010%22%20id%3D%22toc-hId--1306756010%22%3ELab%20account%20and%20Lab%20setup%3C%2FH1%3E%0A%3CP%3EThis%20section%20focuses%20on%20configuring%20your%20lab%20account%20and%20lab%20so%20that%20it%20is%20connected%20to%20your%20on-premise%20domain%20controller.%26nbsp%3B%20You%20may%20need%20to%20work%20with%20your%20IT%20department%20to%20get%20the%20necessary%20information%20and%20permissions%20to%20get%20the%20configuration%20properly%20setup.%3C%2FP%3E%0A%3CP%3E1.)%20Either%20option%20will%20work%3A%3C%2FP%3E%0A%3CP%20class%3D%22lia-indent-padding-left-30px%22%3E-%20Wire%20up%20your%20on-prem%20Domain%20Controller%20on-prem%20network%20to%20an%20Azure%20VNet%2C%20either%20with%20a%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fvpn-gateway%2Fvpn-gateway-about-vpngateways%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%22%3Esite-to-site%20VPN%20gateway%3C%2FA%3E%20or%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fexpressroute%2Fexpressroute-introduction%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%22%3EExpressRoute%3C%2FA%3E.%3C%2FP%3E%0A%3CP%20class%3D%22lia-indent-padding-left-30px%22%3E-%20Create%20a%20secondary%20managed%20domain%20on%20top%20of%20your%20on-prem%20one%20with%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Factive-directory-domain-services%2Ftutorial-create-instance%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%22%3EAzure%20AD%20DS%20(PaaS)%3C%2FA%3E.%3C%2FP%3E%0A%3CP%3E2.)%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Flab-services%2Fclassroom-labs%2Fhow-to-connect-peer-virtual-network%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%22%3EPeer%20the%20Lab%20Account%3C%2FA%3E%20with%20the%20connected%20Virtual%20Network%20(VNet).%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%26nbsp%3B%20%3CSTRONG%3EWARNING%3C%2FSTRONG%3E%3A%20The%20lab%20account%20must%20be%20peered%20to%20a%20virtual%20network%20%3CSTRONG%3Ebefore%3C%2FSTRONG%3E%20the%20lab%20is%20created.%3C%2FP%3E%0A%3CP%3E3.)%20Create%20a%20new%20lab%2C%20with%20the%20option%20enabled%20to%20use%20the%20same%20password%20for%20all%20virtual%20machines.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH1%20id%3D%22toc-hId-1180756823%22%20id%3D%22toc-hId-1180756823%22%20id%3D%22toc-hId-1180756823%22%3EWhere%20are%20the%20scripts%3C%2FH1%3E%0A%3CP%3EThe%20scripts%20are%20available%20on%20%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2FAzure%2Fazure-devtestlab%2Ftree%2Fmaster%2Fsamples%2FClassroomLabs%2FScripts%2FActiveDirectoryJoin%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%22%3EGitHub%3C%2FA%3E%20along%20with%20a%20%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2FAzure%2Fazure-devtestlab%2Fblob%2Fmaster%2Fsamples%2FClassroomLabs%2FScripts%2FActiveDirectoryJoin%2FREADME.md%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ereadme%3C%2FA%3E%20that%20has%20all%20the%20details%20about%20running%20the%20scripts.%26nbsp%3B%20The%20scripts%20require%20a%20domain%20user%20that%20can%20add%20VMs%20to%20the%20domain%2C%20you%20may%20need%20to%20contact%20your%20IT%20department%20to%20get%20the%20necessary%20information.%3C%2FP%3E%0A%3CP%3EThe%20scripts%20are%20designed%20to%20be%20modular.%26nbsp%3B%20The%20first%20script%20that%20is%20run%20on%20the%20template%20VM%20is%20the%20Join-AzLabADTemplate.%26nbsp%3B%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3EJoin-AzLabADStudent_RenameVm%20which%20renames%20the%20VM%20to%20a%20unique%20name.%3C%2FLI%3E%0A%3CLI%3EJoin-AzLabADStudent_JoinVM%20which%20joins%20the%20VM%20to%20the%20appropriate%20domain%20to%20an%20optional%20organizational%20unit.%3C%2FLI%3E%0A%3CLI%3EJoin-AzLabADStudent_AddStudent%20which%20adds%20the%20student%20that%20the%20VM%20is%20registered%20to%2C%20to%20the%20Remote%20User%20group%20so%20they%20can%20login.%26nbsp%3B%20If%20the%20VM%20isn%E2%80%99t%20registered%20to%20a%20user%20the%20task%20is%20skipped.%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EHere%20are%20two%20additional%20scripts%20that%20aren%E2%80%99t%20part%20of%20the%20domain-join%20process%20that%20will%20help%20manage%20the%20VMs.%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3ESet-AzLabCapacity%2C%20which%20allows%20you%20to%20change%20the%20capacity%20of%20the%20lab%20from%20the%20template%20VM.%3C%2FLI%3E%0A%3CLI%3ESet-AzLabADVms%2C%20which%20starts%20all%20the%20VMs%20from%20the%20template%20VM.%26nbsp%3B%20This%20script%20can%20be%20run%20to%20get%20all%20the%20VMs%20domain-joined%20instead%20of%20having%20the%20domain-join%20occur%20when%20the%20students%20start%20the%20VM.%26nbsp%3B%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%3EIf%20you%20have%20any%20questions%2C%20feel%20free%20to%20post%20them%20at%20the%20-ERR%3AREF-NOT-FOUND-community%20forum.%26nbsp%3B%20For%20issues%20with%20the%20scripts%2C%20add%20an%20%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fazure-lab-services%2Fbd-p%2FAzureLabServices%22%20target%3D%22_blank%22%20rel%3D%22noopener%22%3Eissue%20to%20the%20GitHub%20repository%3C%2FA%3E.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThanks%3C%2FP%3E%0A%3CP%3ERoger%20Best%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-1565688%22%20slang%3D%22en-US%22%3E%3CP%3EUsing%20Powershell%20scripts%20to%20automatically%20domain%20join%20Windows%2010%20Azure%20Lab%20Service%20VMs.%3C%2FP%3E%3C%2FLINGO-TEASER%3E%3CLINGO-LABS%20id%3D%22lingo-labs-1565688%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3ELab%20Services%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1566656%22%20slang%3D%22en-US%22%3ERe%3A%20Using%20Powershell%20to%20domain%20join%20Windows%2010%20Azure%20Lab%20Service%20VMs%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1566656%22%20slang%3D%22en-US%22%3E%3CP%3EThank%20you%20for%20Sharing%20with%20the%20Community%2C%20Awesome%20for%20Education%26nbsp%3B%3CIMG%20class%3D%22lia-deferred-image%20lia-image-emoji%22%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Fhtml%2Fimages%2Femoticons%2Fcool_40x40.gif%22%20alt%3D%22%3Acool%3A%22%20title%3D%22%3Acool%3A%22%20%2F%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E
Microsoft

There are many benefits of having domain joined lab VMs in Azure Lab Services, including allowing the students to connect to the VM using their domain credentials.  Each VM can be joined to the domain manually, but this is tedious and doesn’t scale when domain joining multiple VMs.  To help with this, we have put together some Windows 10 Powershell scripts that a lab owner can execute on the so that every VM started in the lab is automatically joined to the domain and the student is added as a Remote Desktop user.

 

The scripts use Windows’ task scheduler to automatically run a script when the student VM starts up. The first script, which the lab owner executes on the template VM, registers a scheduled task that will run another script at VM start up. The script then publishes the template VM to create the students’ lab VMs. When a student’s lab VM is started the first time, the script that executes the domain join will automatically run. I would recommend that the VM start and domain join occur before the students need to login as this may take several minutes. If you increase the lab capacity later, those VMs will use the same configuration from the template VM and will be domain joined when they are started the first time. However, if you change the template VM, such as by adding more software or changing the configuration, the first script will need to be run again to set up the scheduled tasks.

 

The script will need a user, with password, that has permissions to join the domain, you may need to work with your IT department to get the necessary information.

 

The Azure Lab Service team will be building this functionality directly into the product, in the meantime these scripts will allow you to move forward with Lab Services.

 

Environments

These scripts work on the following configurations:

  • On-premise Active Directory domain
  • Hybrid Active Directory domain - An on-premise AD which is connected to an Azure Active Directory through Azure AD Connect.  AD Domain Services is installed on an on-premise server, see the diagram below. Applies also to federated domains.azure-ad-hybrid-joined-als-device.png
  • Azure AD DS Domains - For full-cloud AD (Azure AD + Azure AD DS) or Hybrid AD with secondary Domain Services on Azure.

Lab account and Lab setup

This section focuses on configuring your lab account and lab so that it is connected to your on-premise domain controller.  You may need to work with your IT department to get the necessary information and permissions to get the configuration properly setup.

1.) Either option will work:

- Wire up your on-prem Domain Controller on-prem network to an Azure VNet, either with a site-to-site VPN gateway or ExpressRoute.

- Create a secondary managed domain on top of your on-prem one with Azure AD DS (PaaS).

2.) Peer the Lab Account with the connected Virtual Network (VNet).

   WARNING: The lab account must be peered to a virtual network before the lab is created.

3.) Create a new lab, with the option enabled to use the same password for all virtual machines.

 

Where are the scripts

The scripts are available on GitHub along with a readme that has all the details about running the scripts.  The scripts require a domain user that can add VMs to the domain, you may need to contact your IT department to get the necessary information.

The scripts are designed to be modular.  The first script that is run on the template VM is the Join-AzLabADTemplate. 

  • Join-AzLabADStudent_RenameVm which renames the VM to a unique name.
  • Join-AzLabADStudent_JoinVM which joins the VM to the appropriate domain to an optional organizational unit.
  • Join-AzLabADStudent_AddStudent which adds the student that the VM is registered to, to the Remote User group so they can login.  If the VM isn’t registered to a user the task is skipped.

 

Here are two additional scripts that aren’t part of the domain-join process that will help manage the VMs.

  • Set-AzLabCapacity, which allows you to change the capacity of the lab from the template VM.
  • Set-AzLabADVms, which starts all the VMs from the template VM.  This script can be run to get all the VMs domain-joined instead of having the domain-join occur when the students start the VM. 

If you have any questions, feel free to post them at the community forum.  For issues with the scripts, add an issue to the GitHub repository.

 

Thanks

Roger Best

1 Comment

Thank you for Sharing with the Community, Awesome for Education :cool: