Command Line Interface (CLI)

PowerShell

PowerShell

Pipe Cmdlets

Format-Table

# Long syntax
Get-AzSubscription | Format-Table
Get-AzSubscription | Format-Table -Property Name, Id

# Short syntax
Get-AzSubscription | ft
Get-AzSubscription | ft Name, Id

Format-List

# Long syntax
Get-AzSubscription | Format-List
Get-AzSubscription | Format-List -Property Name, Id

# Short syntax
Get-AzSubscription | fl
Get-AzSubscription | fl Name, Id

Where-Object

# Long syntax
Get-AzSubscription | Where-Object { $_.Id -eq 'xxxx' }

# Short syntax
Get-AzSubscription | ? { $_.Id -eq 'xxxx' }

ConvertTo-Json

Get-AzSubscription | ConvertTo-Json

Out-File

Get-AzSubscription | ConvertTo-Json | Out-File "subscription.json"
PowerShell

Stop Multiple VMs in Multiple Resource Groups

List All VMs

Get-AzVM -ResourceGroupName 'az104-06*' -Status
ResourceGroupName         Name      Location       VmSize  OsType           NIC Provisioning Zone PowerState MaintenanceAllowed
-----------------         ----      --------       ------  ------           --- ------------ ---- ---------- ------------------
AZ104-06-RG1      az104-06-vm0 southeastasia Standard_B1s Windows az104-06-nic0    Succeeded      VM running
AZ104-06-RG1      az104-06-vm1 southeastasia Standard_B1s Windows az104-06-nic1    Succeeded      VM running
AZ104-06-RG2      az104-06-vm2 southeastasia Standard_B1s Windows az104-06-nic2    Succeeded      VM running
AZ104-06-RG3      az104-06-vm3 southeastasia Standard_B1s Windows az104-06-nic3    Succeeded      VM running

Stop All VMs

Get-AzVM -ResourceGroupName 'az104-06*' | Stop-AzVM -Force -AsJob

Get job statuses

Get-Job
Id     Name            PSJobTypeName   State         HasMoreData     Location             Command
--     ----            -------------   -----         -----------     --------             -------
1      PowerShell.OnI…                 Stopped       False                                …
2      Long Running O… AzureLongRunni… Running       True            localhost            Stop-AzVM
3      Long Running O… AzureLongRunni… Running       True            localhost            Stop-AzVM
4      Long Running O… AzureLongRunni… Running       True            localhost            Stop-AzVM
5      Long Running O… AzureLongRunni… Running       True            localhost            Stop-AzVM

Wait until all jobs complete.

Id     Name            PSJobTypeName   State         HasMoreData     Location             Command
--     ----            -------------   -----         -----------     --------             -------
1      PowerShell.OnI…                 Stopped       False                                …
2      Long Running O… AzureLongRunni… Completed     True            localhost            Stop-AzVM
3      Long Running O… AzureLongRunni… Completed     True            localhost            Stop-AzVM
4      Long Running O… AzureLongRunni… Completed     True            localhost            Stop-AzVM
5      Long Running O… AzureLongRunni… Completed     True            localhost            Stop-AzVM

Check status again

ResourceGroupName         Name      Location       VmSize  OsType           NIC Provisioning Zone     PowerState MaintenanceAllowed
-----------------         ----      --------       ------  ------           --- ------------ ----     ---------- ------------------
AZ104-06-RG1      az104-06-vm0 southeastasia Standard_B1s Windows az104-06-nic0    Succeeded      VM deallocated
AZ104-06-RG1      az104-06-vm1 southeastasia Standard_B1s Windows az104-06-nic1    Succeeded      VM deallocated
AZ104-06-RG2      az104-06-vm2 southeastasia Standard_B1s Windows az104-06-nic2    Succeeded      VM deallocated
AZ104-06-RG3      az104-06-vm3 southeastasia Standard_B1s Windows az104-06-nic3    Succeeded      VM deallocated

References

PowerShell

Powershell Script for iCloud Photos

Trigger downloading photos from iCloud to keep local copies for offline use.

$items = Get-ChildItem 'C:\Users\pacroy\Pictures\iCloud Photos\Photos'
$count = $items.length
$counter = 1
ForEach($item in $items) {
    Write-Host -NoNewline "[$counter/$count] $($item.Name) = "
    $stopwatch = [system.diagnostics.stopwatch]::StartNew()
    $duration = Measure-Command { Get-Content $item.FullName -Encoding byte -TotalCount 1 }
    $stopwatch.Stop()
    Write-Host "$($duration.Milliseconds)/$([Math]::Round($stopwatch.Elapsed.TotalMilliseconds))"
    $counter++
}

Copy iCloud photos into OneDrive Camera Roll according to creation year and month

$items = Get-ChildItem 'C:\Users\nap\Pictures\iCloud Photos\Photos'
$destination = 'C:\Users\nap\OneDrive\Pictures\Camera Roll'
$count = $items.Length
$counter = 1
foreach ($item in $items) {
    $year = "{0:d4}" -f $item.CreationTime.Year
    $month = "{0:d2}" -f $item.CreationTime.Month
    $target = "$destination\$year"
    if (-not (Test-Path -LiteralPath $target)) {
        New-Item -Path $target -ItemType Directory -ErrorAction Stop | Out-Null
    }
    $target = "$destination\$year\$month"
    if (-not (Test-Path -LiteralPath $target)) {
        New-Item -Path $target -ItemType Directory -ErrorAction Stop | Out-Null
    }

    Write-Host "[$counter/$count] $($item.Name) -> $target"
    Copy-Item $item.FullName -Destination "$target\$($item.Name)"
    $counter++
}

 

PowerShell

Start-up Script

 

Write-Host "Hi, welcome back!"
Remove-Variable HOMEDRIVE 2> $null
Remove-Variable HOMEPATH 2> $null
Set-Variable HOMEDRIVE "C:"
Set-Variable HOMEPATH "\Users\LANID\"
if ( $(pwd).Path -like 'H:*')
{
    cd $HOME
}

 

Bash