Skip to content

Instantly share code, notes, and snippets.

@amirrajan
Created May 1, 2018 22:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amirrajan/cb4e822c8fb4cf4284dc4afc4958efb7 to your computer and use it in GitHub Desktop.
Save amirrajan/cb4e822c8fb4cf4284dc4afc4958efb7 to your computer and use it in GitHub Desktop.

Here is a thingy.

function PingVM($hash, $vmname) {
    $vmname = ParseVMName $vmname $false
    if (!$vmname) { $vmname = ParseHash $hash $true }
    $vm = FindVM $hash $vmname
    $ipAddress = $vm.virtualMachine.network.publicIpAddresses[0].ipAddress
    $credential = GetSecureVMCredentials $vmname
    $sessionOptions = New-PSSessionOption -SkipCACheck -SkipCNCheck
    $fancyMessage = "PowerShell says hello!"
    Invoke-Command -ComputerName $ipAddress -Credential $credential -UseSSL -SessionOption $sessionOptions -ScriptBlock {
        $hostname = hostname
        Write-Output "Ping successful. Hostname: $hostname"
        Write-Output $using:fancyMessage
    }
}

I need a thingy that lets me write this instead:

function PingVM($hash, $vmname) {
    $fancyMessage = "PowerShell says hello!"
    RemotelyExecute $hash $vmname -ScriptBlock {
        $hostname = hostname
        Write-Output "Ping successful. Hostname: $hostname"
        Write-Output $using:fancyMessage
    }
}

What does the RemotelyExecute function look like?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment