PowerShell: Automation and Configuration Management

PowerShell is a cross-platform task automation and configuration management framework, consisting of a command-line shell and scripting language. Built on .NET, it’s particularly powerful in Windows environments for system administration, but its cross-platform nature allows it to be used efficiently on Linux and macOS for managing various services and cloud resources (like Azure).

1. Creating Custom Objects (PSCustomObject)

PSCustomObject is a versatile way to create custom objects in PowerShell. It allows you to define objects with arbitrary properties and values, which can be easily manipulated, passed through the pipeline, or exported.

# Create a new custom object named $Rule
$Rule = [PSCustomObject] @{
  'Name'        = $nsgname           # Assigns a value to the 'Name' property
  'Description' = 'My custom rule'   # Add another property
  'Priority'    = 100
}
 
# Access a property of the custom object
Write-Host "Rule Name: $($Rule.Name)" # Output: Rule Name: <value of $nsgname>
 
# Access all properties
$Rule | Format-List
  • [PSCustomObject] @{ ... }: Defines a hash table that is cast into a custom object. Each key-value pair in the hash table becomes a property and its value in the object.
  • Write-Host($Rule.name): Accesses the Name property of the $Rule object. The $() is for subexpression.

2. Installing PowerShell Modules (e.g., Azure Az Module)

PowerShell’s functionality is extended through modules. The Az module is a collection of cmdlets for managing Azure resources.

# Set execution policy to allow running scripts
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
 
# Install the Az module from PSGallery
Install-Module -Name Az -Repository PSGallery -Force -Scope CurrentUser

Explanation:

  • Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser:

    • Set-ExecutionPolicy: Changes the PowerShell execution policy, which is a security feature that controls the conditions under which PowerShell loads configuration files and runs scripts.
    • RemoteSigned: Requires that all scripts and configuration files downloaded from the Internet be signed by a trusted publisher. Scripts written on the local computer do not require a digital signature.
    • Scope CurrentUser: Applies the policy only to the current user.
    • Security Note: Be aware of the implications of changing execution policy. RemoteSigned is a common choice for development environments but ensure you understand the risks.
  • Install-Module -Name Az -Repository PSGallery -Force -Scope CurrentUser:

    • Install-Module: Installs a PowerShell module from a registered repository.
    • -Name Az: Specifies the name of the module to install (the Azure module).
    • -Repository PSGallery: Specifies that the module should be installed from the PowerShell Gallery, which is a central repository for PowerShell content.
    • -Force: Overwrites any existing module files and suppresses confirmation prompts.
    • Scope CurrentUser: Installs the module only for the current user, typically in C:\Users\<username>\Documents\PowerShell\Modules.

3. Other Common PowerShell Snippets

Working with Variables

$MyString = "Hello, PowerShell!"
$MyNumber = 123
Write-Host "String: $MyString, Number: $MyNumber"

Getting Process Information

Get-Process | Select-Object -First 5
Get-Process -Name "chrome" | Stop-Process

Working with Files and Paths

Get-ChildItem -Path C:\Windows\System32\Drivers\etc | Select-Object Name, Length
Test-Path -Path C:\DoesNotExist.txt

Basic Loop

$services = Get-Service | Where-Object Status -eq 'Running'
foreach ($service in $services) {
    Write-Host "Running Service: $($service.DisplayName)"
}