Private/Submit-AndRunScriptToVm.ps1
function Global:Submit-AndRunScriptToVm { [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')] [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 } } Set-Content -Path $fullscriptpath -Value $content Write-Host "Running command '$ScriptBlockName' on VM $VMName..." Invoke-AzVMRunCommand -ResourceGroupName $ResourceGroupName -VMName $VMName -CommandId 'RunPowerShellScript' -ScriptPath $fullscriptpath -Parameter $RunParameter Write-Host "Command completed." -ForegroundColor Green Remove-Item $fullscriptpath -Force } } |