Start Azure VM and Open Bastion

The following snippet will check if a VM is turned on, and if not start it, then launch the Bastion connection window in Edge.

$VM = Get-AzVM -Name 'LN-TCTester-01' -Status
if($VM.PowerState -eq 'VM deallocated'){
    $VM | Start-AzVM
}
Start-Process -Path msedge -ArgumentList "https://portal.azure.com/#/resource$($VM.Id)/bastionHost"
  |  
Reboot remote computer and wait

This script that will send a reboot command to a remote computer. It will then monitor for it to go offline and then come back online. Optionally, you can have it monitor for a service to return to running.

# The remote computer to reboot
$computer = 'YourComputer'
# the name of a service to check for after reboot
$Service  = 'WinRM'

# send reboot command to remote computer
Restart-Computer -ComputerName $computer -Force -ErrorAction Stop

# ping computer every second until it goes offline
do{
    $test = Test-Connection -ComputerName $Computer -Count 1 -Quiet
    Start-Sleep -s 1
}while($test -ne $false)

# ping computer every 10 seconds until it comes back online
while($test -eq $false){
    $test = Test-Connection -ComputerName $Computer -Count 1 -Quiet
    Start-Sleep -s 10
}

# wait for service to show running
if(-not [string]::IsNullOrEmpty($Service)){
    do{
        $test = Get-Service -Name  -ComputerName $Computer -ErrorAction SilentlyContinue
        Start-Sleep -s 10
    }while($test.Status -ne 'Running')
}
  |  
Run PSExec From PowerShell

PowerShell remoting help in a lot of areas, but there are times when you need to use PSExec. For those instances, I’ve created a function that you can use to run a command on a remote machine using PSExec.

Function ExecutePsExec($computer, $command){
    $ping = Test-Connection $computer -Count 1 -Quiet
    if($ping){
        $StdOutput = (Join-path $env:temp "$($computer).txt")
        Start-Process -FilePath $psexec -ArgumentList "-s \\$computer $command" -Wait -RedirectStandardOutput $StdOutput -WindowStyle Hidden
        $Results = Get-Content $StdOutput -raw
        Remove-Item $StdOutput -Force
    } else {
        $Results = "Not online"
    }
    [pscustomobject]@{
        Computer = $computer
        Results = $Results
    }
}
# path to PsExec on your local machine
$script:psexec = "C:\Tools\PsExec.exe"

# the command to run
$command = 'cmd /c "powershell.exe -ExecutionPolicy ByPass \\SHARE01\Scripts\Demo.ps1"'

# execute the command on the remote computer
ExecutePsExec -computer 'MYPC01' -command $command
  
Query Remote Registry for Key Value
# Open the specified Registry Hive on a remote machine specified. LocalMachine = HKLM / CurrentUser = HKCU
$computer = 'localhost'
$w32reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine',$computer)

# Open the specific registry key (exclude hive from path)
$keypath = 'SOFTWARE\Microsoft\Windows\CurrentVersion\Authentication\LogonUI'
$SubKey = $w32reg.OpenSubKey($keypath)

# Return data for a specific value
$SubKey.GetValue('LastLoggedOnUser')

# List all values
$SubKey.GetValueNames()

# Return data for all values to hashtable
$AllValues = @{}
$SubKey.GetValueNames() | ForEach-Object{
    $AllValues.Add($_,$SubKey.GetValue($_))
}
$AllValues
Details
This command will create a connection to a remote machine and return the specified registry values. It also shows how you can return all values and data for a key.

  |  
Run Multiple Commands on Remote Machine
# Create a persistent connection to remote machine
$Session = New-PSSession -ComputerName $Computer -Credential $Credential

# Runs on remote machine
Invoke-Command -Session $Session -ScriptBlock {Stop-Service -Name Bits}

# Run on local machine
Get-Service

# Runs on remote machine again
Invoke-Command -Session $Session -ScriptBlock {Start-Service -Name Bits}
Details
Use the New-PSSession to creae a persistent connection to a remote machine. This allows you to call the remote machine multiple times within a single script, without the need to reinitialize your session.
  
Run Command on Remote Machine
$Credential = Get-Credential
Invoke-Command -ComputerName $Computer -ScriptBlock {Stop-Service -Name Bits}
Details
Use to run a single command or script on a remote machine.