Files
oam/knowledge base/powershell.md

4.2 KiB

Windows PowerShell

  1. TL;DR
  2. Further readings
  3. Sources

TL;DR

# Calculate the hash of a file.
CertUtil -hashfile path/to/file sha256

# Get super user privileges.
Start-Process powershell -Verb runAs

# List available features.
Get-WindowsCapability -Online
Get-WindowsCapability -Online | Where-Object Name -like 'OpenSSH*'

# Install a feature.
Add-WindowsCapability -Online -Name OpenSSH.Server
Add-WindowsCapability -Online -Name OpenSSH.Client~~~~0.0.1.0

# Test a network connection.
Test-NetConnection -Port 443 -ComputerName 192.168.0.1 -InformationLevel Detailed

# Assign values to variables.
$variableName = 'value'
$response = Invoke-WebRequest -Uri 'https://jsonplaceholder.typicode.com/users'
$env:PATH += ';C:\foo'

# Print the value of the PATH environment variable.
$env:PATH
Write-Output $env:PATH
Write-Host $env:PATH

# Pipe the output of a command into another.
$users = $response | ConvertFrom-Json
$response | ConvertFrom-Json | Select-Object -Property username,email

# Access Objects' properties via dot-notation.
$users.id
(Invoke-WebRequest -Uri 'https://jsonplaceholder.typicode.com/users').Content

# Show selected Objects' properties.
$users | Select-Object -Property id,username,email
$users | select -Property id,username,email

# Show selected Objects' properties (expanded).
# Dot-notation automatically expands the output.
$users | Select-Object -Expand id,username,email
$users | select -Expand id,username,email

# Filter Objects' values.
$users | Where-Object -Property id -EQ 10
$users | where {$_.id -eq 10}
$users | where {($_.id -eq 10) -or ($_.id -lt 3)}

# Split a command on multiple lines.
Invoke-WebRequest `
  -Uri 'https://jsonplaceholder.typicode.com/users' `
  -UseBasicParsing `
| select -Expand Content `
| ConvertFrom-Json `
| Select-Object -Property id,username,email

# Filter out nodes name and their issues from K8S nodes' command output.
# Both contructions do the same operations and have the same output.
kubectl get nodes -o json `
| ConvertFrom-Json `
| Select-Object -ExpandProperty items `
| Select-Object -Property `
    @{l="Node";e={$_.metadata.name}},`
    @{l="Issues";e={$_.status.conditions `
      | Where-Object { ($_.status -ne "False") -and ($_.type -ne "Ready") } `
      | Select-Object -ExpandProperty type}}
(kubectl get nodes -o json | ConvertFrom-Json).items `
| select @{l="Node";e={$_.metadata.name}},@{l="Issues";e={`
    ($_.status.conditions | where {($_.status -ne "False") -and ($_.type -ne "Ready")}).type`
  }}

Further readings

Sources