Created
June 3, 2021 12:41
-
-
Save techdecline/022d14c4d2750f6bbdbbfe7adb0b5e40 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #Requires -Modules LogStream,Az.Storage,Microsoft.Graph.Intune | |
| [CmdletBinding()] | |
| param ( | |
| # Image File Path | |
| [Parameter(Mandatory)] | |
| [ValidateScript({Test-Path -Path $_})] | |
| [String] | |
| $ImagePath, | |
| # Select Wallpaper or Lockscreen | |
| [Parameter(Mandatory)] | |
| [ValidateSet("Wallpaper","Lockscreen")] | |
| [string] | |
| $Purpose, | |
| # Policy Id to Update | |
| [Parameter(Mandatory=$false)] | |
| [string] | |
| $MEMPolicyId, | |
| # Storage Account Name | |
| [Parameter(Mandatory)] | |
| [string] | |
| $StorageAccountName, | |
| # Storage Account Resource Group | |
| [Parameter(Mandatory)] | |
| [String] | |
| $StorageAccountResourceGroupName, | |
| # Storage Account Container Name | |
| [Parameter(Mandatory)] | |
| [string] | |
| $StorageAccountContainerName, | |
| # Log File Directory | |
| [Parameter(Mandatory=$false)] | |
| [ValidateScript({Test-Path -Path $_})] | |
| [string] | |
| $LogFilePath = $PSScriptRoot, | |
| # Switch to randomize file name before uploading | |
| [Parameter(Mandatory=$false)] | |
| [switch] | |
| $RandomizeFileName | |
| ) | |
| function Get-RandomString { | |
| param ($Length = 32) | |
| $str = (new-Guid).Guid -replace "-" | |
| return ($str.substring(0,$Length)) | |
| } | |
| # Initialize Logging | |
| Import-Module LogStream | |
| $LogFile = Join-Path $LogFilePath -ChildPath "Set-MEMImage_$(get-date -Format yyyyMMdd).log" | |
| Start-Log -LogFilePath $LogFile | |
| try { | |
| # Create Storage Context | |
| Write-VerboseLog -LogFilePath $LogFile -Message "Target is: $Purpose" | |
| Write-VerboseLog -LogFilePath $LogFile "Setting Storage Context to Storage Account: $StorageAccountName" | |
| $storageContext = (Get-AzStorageAccount -ResourceGroupName $StorageAccountResourceGroupName -Name $StorageAccountName).Context | |
| # Upload File to blob | |
| if ($RandomizeFileName) { | |
| Write-VerboseLog -LogFilePath $LogFile -Message "Randomizing File Name" | |
| $tmpArr = (get-item $ImagePath).Name -split "\." | |
| $newName = "$($tmpArr[0])_$(Get-RandomString -Length 8).$($tmpArr[1])" | |
| $newImageFileObj = Copy-Item -Path $ImagePath -Destination (Join-Path -Path (Split-Path $ImagePath -Parent) -ChildPath $newName) -PassThru | |
| $ImagePath = $newImageFileObj.FullName | |
| } | |
| Write-VerboseLog -LogFilePath $LogFile -Message "Uploading Image File to Storage Container: $StorageAccountContainerName" | |
| $uploadParam = @{ | |
| File = $ImagePath | |
| Container = $StorageAccountContainerName | |
| Blob = Split-Path -Path $ImagePath -Leaf | |
| Context = $storageContext | |
| ErrorAction = "Stop" | |
| Force = $true | |
| } | |
| $blobObj = Set-AzStorageBlobContent @uploadParam | |
| Write-VerboseLog -LogFilePath $LogFile -Message "Image Url is: $($blobObj.BlobClient.Uri.AbsoluteUri)" | |
| # Update MEM Policy | |
| $deviceCfgPolicyObj = Get-DeviceManagement_DeviceConfigurations -deviceConfigurationId $MEMPolicyId | |
| Write-VerboseLog -LogFilePath $LogFile -Message "Updating Policy with Name: $($deviceCfgPolicyObj.displayName)" | |
| switch ($Purpose) { | |
| "Wallpaper" { | |
| $deviceCfgPolicyObj | Update-DeviceManagement_DeviceConfigurations -personalizationDesktopImageUrl $blobObj.BlobClient.Uri.AbsoluteUri | |
| } | |
| "Lockscreen" { | |
| $deviceCfgPolicyObj | Update-DeviceManagement_DeviceConfigurations -personalizationLockScreenImageUrl $blobObj.BlobClient.Uri.AbsoluteUri | |
| } | |
| } | |
| Write-VerboseLog -LogFilePath $logFile -Message "Successfully updated MEM Policy with Id: $MEMPolicyId" | |
| } | |
| catch [Microsoft.Rest.Azure.CloudException]{ | |
| Write-ErrorLog -LogFilePath $LogFile -Message $error[0].Exception.Message | |
| } | |
| catch [Microsoft.Azure.Storage.StorageException] { | |
| Write-ErrorLog -LogFilePath $LogFile -Message ($error[0].Exception.Message -split "`n")[0] | |
| } | |
| catch [System.Net.Http.HttpRequestException] { | |
| Write-ErrorLog -LogFilePath $LogFile -Message "Could not find MEM Policy: $(($error[0].Exception.Message -split "`n")[0])" | |
| } | |
| catch [System.InvalidOperationException] { | |
| Write-ErrorLog -LogFilePath $LogFile -Message $error[0].Exception.message | |
| } | |
| finally { | |
| if ($newImageFileObj) { | |
| Write-VerboseLog -LogFilePath $LogFile -Message "Removing temporary file: $($newImageFileObj.Fullname)" | |
| Remove-Item $newImageFileObj.Fullname -Force -Confirm:$false | |
| } | |
| Stop-Log -LogFilePath $LogFile | Out-Null | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment