Private/Submit-ScriptToVmAndExecute.ps1
function Global:Submit-ScriptToVmAndExecute { [CmdletBinding()] <# .SYNOPSIS Initializes the newly created VM .DESCRIPTION ... #> param( [Parameter(Mandatory = $true)] [string] $ResourceGroupName, [Parameter(Mandatory = $true)] [string] $ResourceLocation, [Parameter(Mandatory = $true)] [string] $VMName, [Parameter(Mandatory = $true)] [ValidateSet('InstallD365Module','InitVM','DownloadBC','InstallBC','GeneralizeVM','WriteProperties','CreateUpdateScheduledTask')] [string] $ScriptBlockName, [Parameter(Mandatory = $false)] $RunParameter ) process { . $PSScriptRoot\Scriptblocks.ps1 $fullscriptpath = New-TemporaryFile | Rename-Item -NewName { $_ -replace 'tmp$', 'ps1' } -PassThru | Select-Object -ExpandProperty FullName $content = "" switch ($ScriptBlockName) { 'InstallD365Module' { $content = $installD365Module } 'InitVM' { $content = $initializeVm } 'DownloadBC' { $content = $downloadBCDVD } 'InstallBC' { $content = $installBC } 'GeneralizeVM' { $content = $generalizeVM } 'WriteProperties' { $content = $writeProperties } 'CreateUpdateScheduledTask' { $content = $createUpdateScheduledTask } } Set-Content -Path $fullscriptpath -Value $content Write-CustomHost -Message "Running command '$ScriptBlockName' on VM $VMName..." Invoke-AzVMRunCommand -ResourceGroupName $ResourceGroupName -VMName $VMName -CommandId 'RunPowerShellScript' -ScriptPath $fullscriptpath -Parameter $RunParameter | Out-Null Write-CustomHost -Message "Command completed." Remove-Item $fullscriptpath -Force } } |