This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Corrected Azure Site Recovery PowerShell Script with Azure CLI Commands | |
# Set variables (modify these as needed) | |
$ResourceGroup = "MyASRTestRG" | |
$Location = "eastus" | |
$RecoveryLocation = "westus" | |
$VaultName = "MyASRTestVault" | |
$PrimaryVNet = "PrimaryTestVNet" | |
$RecoveryVNet = "RecoveryTestVNet" | |
$StorageAccount = "myasrteststorage" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"Smith, John" -replace "(\w+),\s*(\w+)", '$2 $1' | |
## John Smith |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Write-CustomLog { | |
param([string]$Message) | |
$callerInfo = (Get-PSCallStack)[1] | |
$scriptName = Split-Path -Leaf $callerInfo.ScriptName | |
$lineNumber = $callerInfo.ScriptLineNumber | |
$functionName = $callerInfo.FunctionName | |
$logEntry = "{0:yyyy-MM-dd HH:mm:ss} - {1}:{2} - {3} - {4}" -f ` | |
(Get-Date), $scriptName, $lineNumber, $functionName, $Message |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$logEntries = @( | |
"2024-06-09", | |
"Error: Disk full", | |
404, | |
"2024-06-08", | |
"Error: Network unreachable", | |
500 | |
) | |
foreach ($entry in $logEntries) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Remove-ItemSafely { | |
[CmdletBinding(SupportsShouldProcess, ConfirmImpact = 'High')] | |
param([string]$path) | |
if ($PSCmdlet.ShouldProcess($path, "Remove")) { | |
Remove-Item -Path $path | |
} | |
} | |
PS> Remove-ItemSafely -Path '\oh\god\wrong\file.txt' -WhatIf |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Get-ListOfFiles { | |
[OutputType('System.IO.FileInfo')] | |
[CmdletBinding()] | |
param( | |
[string]$Path | |
) | |
Get-ChildItem -File -Path $Path | |
} | |
Get-ListOfFiles -Path "C:\Temp" | ForEach-Object { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Get-DatabaseData { | |
[CmdletBinding()] | |
param ( | |
[Parameter(Mandatory)] | |
[string]$DatabaseType | |
) | |
dynamicparam { | |
$paramDictionary = New-Object System.Management.Automation.RuntimeDefinedParameterDictionary | |
$attributes = New-Object System.Collections.ObjectModel.Collection[System.Attribute] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Set-LogLevel { | |
param ( | |
[ValidateSet("Debug", "Info", "Warning", "Error")] | |
[string]$Level | |
) | |
Write-Output "Log level set to $Level" | |
} | |
# Example usage: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## Instead of this | |
$group = 'some group name' | |
## Do this | |
$groupName = 'some group name' | |
## Because a group could be an object with properties like id, name, displayname, etc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Define a custom object for a server or whatever object you want | |
$server = [pscustomobject]@{ | |
Name = "Server1" | |
IPAddress = "192.168.0.1" | |
} | |
# Add a ScriptMethod to check if the server is online | |
## This can run any code you may like to associated with an object | |
$server | Add-Member -MemberType ScriptMethod -Name "IsOnline" -Value { | |
param ($Timeout = 1000) |
NewerOlder