# Import Az module Import-Module Az # Variables for Azure Virtual Desktop Environment $subscriptionId = "xxxxx-xxxx-xxx-xxx" $resourceGroupName = "MyRG" $hostPoolName = "MyHostpool" $sessionHostUser = "testuser" # FSLogix user profile to test $password = "password" | ConvertTo-SecureString -AsPlainText -Force $domain = "abc_company.onmicrosoft.com" # Domain where the user is authenticated $sessionCount = 4 # Number of concurrent sessions to simulate $monitorInterval = 30 # Monitoring interval in seconds #Get Access token # Set up the variables $tenantId = "aaaa-aaaa-aaaa" $clientId = "bbbb-bbbb-bbbb-bbb" $clientSecret = "1gebdjsadkbdjqwduqwid2b" #client secret value from app reg $resource = "https://rdweb.wvd.microsoft.com" # e.g., "https://graph.microsoft.com" for Microsoft Graph # Create the request body for the token request $body = @{ client_id = $clientId client_secret = $clientSecret scope = "$resource/.default" grant_type = "client_credentials" } # Request the token $uri = "https://login.microsoftonline.com/$tenantId/oauth2/v2.0/token" $response = Invoke-RestMethod -Method Post -Uri $uri -ContentType "application/x-www-form-urlencoded" -Body $body # Extract the access token $accessToken = $response.access_token # Display the access token $accessToken # Azure Login Write-Output "Logging into Azure..." Connect-AzAccount -UseDeviceAuthentication # Get Session Hosts in the Host Pool Write-Output "Retrieving session hosts in the Host Pool..." $sessionHosts = Get-AzWvdSessionHost -ResourceGroupName $resourceGroupName -HostPoolName $hostPoolName if (-not $sessionHosts) { Write-Output "No session hosts found in the specified host pool." exit } # Simulate User Logins via Azure Virtual Desktop API Write-Output "Starting user login simulation..." for ($i = 1; $i -le $sessionCount; $i++) { $host1 = $sessionHosts[$i % $sessionHosts.Count] # Round-robin session host selection # Example: Interact with AVD API for user sessions $apiUrl = "https://management.azure.com/subscriptions/$subscriptionId/resourceGroups/$resourceGroupName/providers/Microsoft.DesktopVirtualization/hostPools/$hostPoolName/userSessions?api-version=2022-02-10" # Construct the payload or parameters as needed $body = @{ username = "$domain\$sessionHostUser" } # API call for session interaction $response = Invoke-RestMethod -Method Post -Uri $apiUrl -Headers @{ Authorization = "Bearer $token" } -Body ($body | ConvertTo-Json -Depth 10) Write-Output "Simulated user login for session host $($host1.SessionHostName): $response.status" Start-Sleep -Seconds 2 # Delay to avoid overwhelming session hosts } # Monitor Performance Metrics Write-Output "Monitoring session host performance..." while ($true) { foreach ($host2 in $sessionHosts) { #$performance = Get-AzWvdSessionHost -ResourceGroupName $resourceGroupName -HostPoolName $hostPoolName -SessionHostName $host2.SessionHostName #Write-Output "Session Host: $($host2.SessionHostName)" Write-Output " CPU Usage: $($performance.CpuUsagePercent)%" Write-Output " Memory Usage: $($performance.MemoryUsageInMB) MB" Write-Output " Sessions Active: $($performance.SessionsActive)" } Start-Sleep -Seconds $monitorInterval }