Export/Private/Submit-ScriptToVMAndExecute.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
function Global:Submit-ScriptToVmAndExecute {
    [CmdletBinding()]
    <#
    .SYNOPSIS
        ...
    .DESCRIPTION
        ...
        Do not pass any secure-objects in $RunParameter (like SecureString for Passwords); these will cause to run indefinitely
    #>

    param(
        [Parameter(Mandatory = $true)]
        [string]
        $ResourceGroupName,        
        [Parameter(Mandatory = $true)]
        [string]
        $ResourceLocation,
        [Parameter(Mandatory = $true)]
        [string]
        $VMName,
        [Parameter(Mandatory = $true)]        
        $ScriptBlock,
        [Parameter(Mandatory = $false)]
        $RunParameter,
        [Parameter(Mandatory = $false)]
        [string]
        $MsgBeforeExecuting,
        [Parameter(Mandatory = $false)]
        [switch]
        $ScaleSetExecution = $false
    )
    process {
        if ($MsgBeforeExecuting) {
            Write-Verbose $MsgBeforeExecuting
        }
        # Temporary save script-block as file
        $fullscriptpath = New-TemporaryFile | Rename-Item -NewName { $_ -replace 'tmp$', 'ps1' } -PassThru | Select-Object -ExpandProperty FullName
        Set-Content -Path $fullscriptpath -Value $ScriptBlock
        Write-Verbose "Running command on $VMName..."
        if (-not($ScaleSetExecution)) {
            Invoke-AzVMRunCommand -ResourceGroupName $ResourceGroupName -VMName $VMName -CommandId 'RunPowerShellScript' -ScriptPath $fullscriptpath -Parameter $RunParameter | Out-Null
        }
        else {
            foreach ($instance in Get-AzVmssVM -ResourceGroupName $ResourceGroupName -VMScaleSetName $VMName) {
                $instanceId = $instance.InstanceId
            
                Write-Verbose "on Instance $instanceId..."
                Invoke-AzVmssVMRunCommand -ResourceGroupName $ResourceGroupName -VMScaleSetName $VMName -InstanceId $instanceId -CommandId 'RunPowerShellScript' -ScriptPath $fullscriptpath -Parameter $RunParameter | Out-Null
            }
        }
        Write-Verbose "Command completed."
        Remove-Item $fullscriptpath -Force
    }
}