This solely ensures that the most compute intensive task (caching sizes/performance) is not repeated between jobs. You’ll still have to handle running multiple jobs using your own preferred method, e.g. foreach -parallel, runspaces or start-job. Example:
I wanted to put this out there as it felt like a nifty way to pipeline AVD MSIX files into AVD without any user interaction (other than a pipeline kicking off the script).
Basically, above will grab the MSIX file from a known Azure Fileshare (after mounting). It’ll read the MSIX’s primary CIM file for meta data, use the Azure Rest API to add it to the hostpool and then updates a param file of an ARM template which can be used to e.g. update the appgroup in Azure.
You’ll need some background knowledge to re-use above in your specific situation 🙂
Code example:
#create the MSIX package object in the hostpool. Ensure the lastUpdated value is always unique otherwise it will fail to overwrite an existing package with the same value$apiPostData=@{"properties"=@{"displayName"=if($packageMeta-match"(?<=<DisplayName>)(.*?)(?=<\/DisplayName>)"){$matches[1]}else{Throw"No display name found in AppManifest"}"imagePath"=$imagePath"isActive"=$True"isRegularRegistration"=$False"lastUpdated"=(get-itemproperty$packageFolder.FullName).LastWriteTimeUtc.AddSeconds((Get-Random-Minimum "-150"-Maximum 150)).ToString("yyyy-MM-ddThh:mm:ss")"packageApplications"=$packageApplications"packageDependencies"=@()"packageFamilyName"="$($packageShortName)_$($packageFamily)""packageName"=$packageShortName"packageRelativePath"="\MSIXPackages\$($packageFolder.Name)""version"=$packageVersion}}#send the actual API request to register the package in the hostpool using the pipeline serviceprincipaltry{$context=[Microsoft.Azure.Commands.Common.Authentication.Abstractions.AzureRmProfileProvider]::Instance.Profile.DefaultContext$token=[Microsoft.Azure.Commands.Common.Authentication.AzureSession]::Instance.AuthenticationFactory.Authenticate($context.Account,$context.Environment,$context.Tenant.Id.ToString(),$null,[Microsoft.Azure.Commands.Common.Authentication.ShowDialog]::Never,$null,"https://management.azure.com")Invoke-RestMethod-Uri "https://management.azure.com/subscriptions/$((get-azcontext).Subscription.id)/resourcegroups/rg-common-$($environment)-weeu-01/providers/Microsoft.DesktopVirtualization/hostPools/vdhp-common-$($environment)-weeu-01/msixPackages/$($packageFolder.Name)?api-version=2021-07-12"-Method PUT -UseBasicParsing -ContentType "application/json"-Body ($apiPostData|convertto-json-Depth 15)-Headers @{"Authorization"="Bearer $($token.AccessToken)"}-ErrorAction Stop}catch{Write-Output$_ closeCIMSessionThrow}
When creating a CIM image for MSIX app attach, make sure that you’re using a lower or equal OS version than your target OS in e.g. Azure Virtual Desktop. If you use Windows 11 to create a CIM file and try to mount this on Windows 10, the result will be one of the following.
Azure Portal:
ActivityId: 1ef4d1ad-e4af-42d1-b6fa-139c45775efb Error: The MSIX Application metadata expand request failed on all Session Hosts that it was sent to. Session Host: xxxx1, Error: Native error when mounting CIM, HResult -2147024809, ErrorCode 87.. (Code: 400)
Using the CimDiskImage PS module:
Mount-CimDiskimage : Mounting xxxxx.cim to volume failed with Error:'The operation completed successfully Errorcode:0'
Mount-CimDiskimage : Mounting xxxx.cim to volume failed with Error:'Too many posts were made to a semaphore ErrorCode:298'
Above happens because W10 using V2 CIM disks, while W11 uses V3 CIM disks.
For Intune / Microsoft Endpoint Manager, no solution was known yet. So I base64 encoded Bernd’s solution and wrapped it into a SYSTEM wide scheduled task that is triggered by a security eventlog logoff entry.
Deploy this to your VM’s in Intune (either through a user or a machine group) and it’ll ensure users’ VM’s get deallocated when they log off.
This also works on shared VM’s, as it will only deallocate if it is the last user logging off.
4. Accept further defaults and when created click Go to resource:
5. Under the Identity option, click Azure Role Assignments:
6. Be sure to select the same subscription your Virtual Machines are in:
7. If you wish to scope permissions more specifically (always recommended), at minimum you’ll need to assign Virtual Machine Contributor to the resource group(s) containing your VM’s and Log Analytics Reader on your log analytics workspace.
8. Now create a runbook in your new automation account:
11. Go to modules and click browse gallery, search for ADDRS:
12. Wait for the import to complete. If it fails, check if you have installed the 7.1 versions of these requires modules, install if needed and try again:
Az.Compute
Az.OperationalInsights
Az.Resources
Az.Accounts
13. Run the runbook to test, or link it to a schedule:
14. if you want to use a schedule different from weekly, make sure to also add the -measurePeriodHours parameter to match, and if you use maintenance windows, include those as well as described in the module manual.