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...
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...
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...
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}
Run Command on Remote Machine with Credentials
$Credential = Get-Credential Invoke-Command -ComputerName $Computer -ScriptBlock {Stop-Service -Name Bits} -Credential $Credential
Run Command on Remote Machine
$Credential = Get-Credential Invoke-Command -ComputerName $Computer -ScriptBlock {Stop-Service -Name Bits}