Workspace-Guru

New-WVD-Enviroment

May 31st, 2019
851
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ### Creat complete Microsoft Windows Virtual Desktops Enviroment
  2. ### Before running this script autorize your tenant by going to: https://rdweb.wvd.microsoft.com/
  3.  
  4. ### User Parameters
  5. $TenantAdminName = "[email protected]" ## MFA is not supported for Tenant Admn
  6. $TenantName = "newtenant01" ## New WVD Tenant Name
  7. $tenantAdminPassword = read-host "Please enter Tenant Admin password" -AsSecureString
  8. $SubscriptionID = "aaaaaa-aaaaaaaa-aaaaaaa" ## Azure Subscription ID
  9. $AzureADID = "bbbbbbb-bbbbbbb-bbbbbb" ## Azure Active Directory ID, can be found in properties in Azure Active Directory
  10. $resourceGroupName = "WVD-Pool" ## Name for new or empty resource group for Host Pool
  11. $resourceGroupLocation = "westeurope" ## Location for resource group for Host Pool
  12. $rdshNamePrefix = "WVDMachine" ## Prefix of the VDI pool machines that will be created
  13. $rdshNumberOfInstances = "1" ## Number of VM's to be created in the Pool
  14. $rdshVMDiskType = "Premium_LRS" ## Disk type
  15. $rdshVmSize = "Standard_D4s_v3" ## VM size
  16. $domainToJoin = "contoso.com" ## Domain to join the VM's
  17. $existingDomainUPN = "[email protected]" ## UPN of domain admin
  18. $existingDomainPassword = read-host "Enter domain admin password" -AsSecureString  ## Password of domain admin
  19. $ouPath = "" ## Path to OU where VM will be created. Leave emptye and they will default go in to computer OU
  20. $existingVnetName = "Azure-VNet-01" ## Vnet that is connect to Active Directory
  21. $existingSubnetName = "default" ## Name of the subnate in the VNet
  22. $virtualNetworkResourceGroupName  = "Azure-Resource-Group-01" ## Name of resource group where VNet is located"
  23. $existingTenantGroupName = "Default Tenant Group" ## Tenant group name default is Default Tenant Group
  24. $hostPoolName = "NewPool" ## Name of the new host pool
  25. $defaultDesktopUsers = "[email protected],[email protected]" ## User who get acces to the new WVD desktop
  26.  
  27. ### Default Parameters
  28. $rdshImageSource = "Gallery"
  29. $vmImageVhdUri = ""
  30. $rdshGalleryImageSKU = "Windows-10-Enterprise-multi-session-with-Office-365-ProPlus"
  31. $rdshCustomImageSourceName = ""
  32. $rdshCustomImageSourceResourceGroup = ""
  33. $enableAcceleratedNetworking = $false
  34. $rdshUseManagedDisks = $true
  35. $storageAccountResourceGroupName = ""
  36. $newOrExistingVnet = "existing"
  37. $existingTenantName = $TenantName
  38. $enablePersistentDesktop = $false
  39. $tenantAdminUpnOrApplicationId = $TenantAdminName
  40. $isServicePrincipal = $false
  41. $location = $resourceGroupLocation
  42.  
  43.  
  44. ### Importing and Installing modules
  45. Write-host -foreground Green "Installing and Importing PowerShell Modules"
  46.  
  47. # Azure Active Directory Module
  48. if (Get-Module -ListAvailable -Name AzureAD)
  49. {
  50.     Import-Module AzureAD | Out-Null
  51. }
  52. else
  53. {
  54.     Install-Module -Name AzureAD -scope AllUsers -Confirm:$false -force
  55.     Import-Module AzureAD | Out-Null
  56. }
  57.  
  58. # Azure RM Module
  59. if (Get-Module -ListAvailable -Name AzureRM)
  60. {
  61.     Import-Module AzureRM | Out-Null
  62. }
  63. else
  64. {
  65.     Install-Module -Name AzureRM -scope AllUsers -Confirm:$false -force
  66.     Import-Module AzureRM | Out-Null
  67. }
  68.  
  69. # RD Infra Module
  70. if (Get-Module -ListAvailable -Name Microsoft.RDInfra.RDPowerShell)
  71. {
  72.     Import-Module Microsoft.RDInfra.RDPowerShell | Out-Null
  73. }
  74. else
  75. {
  76.     Install-Module -Name Microsoft.RDInfra.RDPowerShell -scope AllUsers -Confirm:$false -force
  77.     Import-Module Microsoft.RDInfra.RDPowerShell | Out-Null
  78. }
  79.  
  80. ### Connect to Azure AD
  81. write-host -ForegroundColor Yellow "Enter your Azure Active Directory Crendentials"
  82. Connect-AzureAD
  83.  
  84. ### Assign admin RDS TenantCreator Role
  85. $username = $TenantAdminName
  86. $app_name = "Windows Virtual Desktop"
  87. $app_role_name = "TenantCreator"
  88.  
  89. # Get the user to assign, and the service principal for the app to assign to
  90. $user = Get-AzureADUser -ObjectId "$username"
  91. $sp = Get-AzureADServicePrincipal -Filter "displayName eq '$app_name'"
  92. $appRole = $sp.AppRoles | Where-Object { $_.DisplayName -eq $app_role_name }
  93.  
  94. # Assign the user to the app role
  95. New-AzureADUserAppRoleAssignment -ObjectId $user.ObjectId -PrincipalId $user.ObjectId -ResourceId $sp.ObjectId -Id $appRole.Id | out-null
  96. sleep 5
  97.  
  98. ### Creating new WVD Tenant
  99.  
  100. # Sign into WVD Environment
  101. Write-Host -ForegroundColor yellow "Enter your Tenant Admin Credentials"
  102. Add-RdsAccount -DeploymentUrl "https://rdbroker.wvd.microsoft.com"
  103.  
  104. # Creating new WVD Tenant
  105. New-RdsTenant -Name $TenantName -AadTenantId $AzureADID -AzureSubscriptionId $SubscriptionID
  106.  
  107. ### Deploying new host Pool with AzureRM
  108.  
  109. #Sign into Azure
  110. Write-Host -ForegroundColor yellow "Enter your Azure Admin Credentials"
  111. Login-AzureRmAccount
  112.  
  113. # Register RPs
  114. Function RegisterRP {
  115.     Param(
  116.         [string]$ResourceProviderNamespace
  117.     )
  118.  
  119.     Write-Host "Registering resource provider '$ResourceProviderNamespace'";
  120.     Register-AzureRmResourceProvider -ProviderNamespace $ResourceProviderNamespace;
  121. }
  122.  
  123. $resourceProviders = @("microsoft.resources","microsoft.compute");
  124. if($resourceProviders.length) {
  125.     Write-Host -ForegroundColor Green "Registering resource providers"
  126.     foreach($resourceProvider in $resourceProviders) {
  127.         RegisterRP($resourceProvider);
  128.     }
  129. }
  130.  
  131. # Create or check for existing resource group
  132. $resourceGroup = Get-AzureRmResourceGroup -Name $resourceGroupName -ErrorAction SilentlyContinue
  133. if(!$resourceGroup)
  134. {
  135.     Write-Host -ForegroundColor Green "Creating resource group '$resourceGroupName' in location '$resourceGroupLocation'";
  136.     New-AzureRmResourceGroup -Name $resourceGroupName -Location $resourceGroupLocation
  137. }
  138. else{
  139.     Write-Host -foreground Yellow "Using existing resource group '$resourceGroupName'";
  140. }
  141.  
  142. # Start the deployment
  143. Write-Host -ForegroundColor Green "Starting Host Pool deployment this can take some time (~15min)..."
  144. $templatefile = "https://raw.githubusercontent.com/Azure/RDS-Templates/master/wvd-templates/Create%20and%20provision%20WVD%20host%20pool/mainTemplate.json"
  145. New-AzureRmResourceGroupDeployment -ResourceGroupName $resourceGroupName -Name "New-WVD-HostPool" -TemplateUri $templatefile `
  146. -tenantAdminPassword $tenantAdminPassword `
  147. -rdshNamePrefix $rdshNamePrefix `
  148. -rdshNumberOfInstances $rdshNumberOfInstances `
  149. -rdshVMDiskType $rdshVMDiskType `
  150. -rdshVmSize $rdshVmSize `
  151. -domainToJoin $domainToJoin `
  152. -existingDomainUPN $existingDomainUPN `
  153. -existingDomainPassword $existingDomainPassword `
  154. -ouPath $ouPath `
  155. -existingVnetName $existingVnetName `
  156. -existingSubnetName $existingSubnetName `
  157. -virtualNetworkResourceGroupName $virtualNetworkResourceGroupName `
  158. -existingTenantGroupName $existingTenantGroupName `
  159. -hostPoolName $hostPoolName `
  160. -defaultDesktopUsers $defaultDesktopUsers `
  161. -rdshImageSource $rdshImageSource `
  162. -vmImageVhdUri $vmImageVhdUri `
  163. -rdshGalleryImageSKU $rdshGalleryImageSKU `
  164. -rdshCustomImageSourceName $rdshCustomImageSourceName `
  165. -rdshCustomImageSourceResourceGroup $rdshCustomImageSourceResourceGroup `
  166. -enableAcceleratedNetworking $enableAcceleratedNetworking `
  167. -rdshUseManagedDisks $rdshUseManagedDisks `
  168. -storageAccountResourceGroupName $storageAccountResourceGroupName `
  169. -newOrExistingVnet $newOrExistingVnet `
  170. -existingTenantName $existingTenantName `
  171. -enablePersistentDesktop $enablePersistentDesktop `
  172. -tenantAdminUpnOrApplicationId $tenantAdminUpnOrApplicationId `
  173. -isServicePrincipal $isServicePrincipal `
  174. -location $location
  175.  
  176. ### Checking Host Pool
  177. $hostPool = Get-RdsHostPool -TenantName $tenantName -Name $HostPoolName
  178. if(!$hostpool){
  179.  write-host -ForegroundColor red "Something went wrong check te deployment in the resource group"
  180. }else{
  181.  write-host -ForegroundColor green "WVD Tenant is created and users can now sign in to https://rdweb.wvd.microsoft.com/webclient/index.html"
  182. }
Advertisement
Add Comment
Please, Sign In to add comment