powershell_cheat_sheet
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| powershell_cheat_sheet [2019/09/16 02:14] – stephen | powershell_cheat_sheet [2021/04/13 21:53] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 9: | Line 9: | ||
| if (!(Test-Path $path)) { | if (!(Test-Path $path)) { | ||
| Write-Host "File does not exist." | Write-Host "File does not exist." | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | ===== Check previous command was successful ===== | ||
| + | |||
| + | <code powershell> | ||
| + | if ($?) { | ||
| + | Write-Host ' | ||
| + | } else { | ||
| + | Write-Host ' | ||
| + | throw ' | ||
| } | } | ||
| </ | </ | ||
| Line 22: | Line 33: | ||
| <code powershell> | <code powershell> | ||
| (Get-Content $filename) -replace ' | (Get-Content $filename) -replace ' | ||
| + | </ | ||
| + | |||
| + | ===== Create a timestamp ===== | ||
| + | |||
| + | <code powershell> | ||
| + | $timestamp = (Get-Date).ToString(" | ||
| </ | </ | ||
| Line 28: | Line 45: | ||
| <code powershell> | <code powershell> | ||
| param ( | param ( | ||
| - | [parameter(Mandatory=$true)] [string]$targetServer, | + | [parameter(Mandatory=$true, |
| - | [parameter(Mandatory=$true)] [string]$targetWebConfig | + | [parameter(Mandatory=$true)] [string]$targetWebConfig, |
| + | [parameter(Mandatory=$false)] [string]$build = ' | ||
| ) | ) | ||
| </ | </ | ||
| - | ===== Search installed certs for thumbprint | + | ===== Set current directory to script location |
| + | |||
| + | <code powershell> | ||
| + | Push-Location " | ||
| + | #... | ||
| + | Pop-Location | ||
| + | </ | ||
| + | |||
| + | ===== Certificates ===== | ||
| + | |||
| + | Search by thumbprint: | ||
| <code powershell> | <code powershell> | ||
| Get-ChildItem -Path Cert: -Recurse | Where-Object { $_.Thumbprint -and $_.Thumbprint.ToLower().StartsWith(' | Get-ChildItem -Path Cert: -Recurse | Where-Object { $_.Thumbprint -and $_.Thumbprint.ToLower().StartsWith(' | ||
| + | </ | ||
| + | |||
| + | To check if the cert is valid, pipe it the '' | ||
| + | |||
| + | <code powershell> | ||
| + | Get-ChildItem -Path Cert: -Recurse | Where-Object { $_.Thumbprint -and $_.Thumbprint.ToLower().StartsWith(' | ||
| + | </ | ||
| + | |||
| + | To check for expired / revoked certs: | ||
| + | |||
| + | <code powershell> | ||
| + | $certs = Get-ChildItem -Path Cert: | ||
| + | foreach ($cert in $certs) { | ||
| + | Write-Host " | ||
| + | |||
| + | if ($certs.NotAfter -lt (Get-Date)) { | ||
| + | Write-Host " | ||
| + | } | ||
| + | elseif ($cert | Test-Certificate) { | ||
| + | Write-Host " | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | ===== Get Install-Module working behind a webproxy ===== | ||
| + | |||
| + | - Run '' | ||
| + | - Add this to the top:< | ||
| + | # Uncomment this to use PowerShellGet | ||
| + | # | ||
| + | # | ||
| + | # | ||
| + | # | ||
| + | # | ||
| + | - Uncomment, fill out the details, and save. | ||
| + | - Start a //new// Powershell session. | ||
| + | - Run '' | ||
| + | - Run '' | ||
| + | - Once installation is complete, comment out commands and change password. | ||
| + | |||
| + | If this doesn' | ||
| + | |||
| + | ===== No Sleep ===== | ||
| + | |||
| + | May only work inside Windows Powershell ISE. | ||
| + | |||
| + | <code powershell> | ||
| + | $intervalSeconds = 60 | ||
| + | $mouseDistance = 1 | ||
| + | |||
| + | $myshell = New-Object -com " | ||
| + | while ($true) { | ||
| + | Start-Sleep -Seconds $intervalSeconds | ||
| + | $Pos = [System.Windows.Forms.Cursor]:: | ||
| + | [System.Windows.Forms.Cursor]:: | ||
| + | [System.Windows.Forms.SendKeys]:: | ||
| + | Write-Host 'A shuffle to the right.' | ||
| + | Start-Sleep -Seconds $intervalSeconds | ||
| + | $Pos = [System.Windows.Forms.Cursor]:: | ||
| + | [System.Windows.Forms.Cursor]:: | ||
| + | [System.Windows.Forms.SendKeys]:: | ||
| + | Write-Host 'A shuffle to the left.' | ||
| + | } | ||
| </ | </ | ||
| Line 63: | Line 154: | ||
| ==== Reformatting by using regex groups ==== | ==== Reformatting by using regex groups ==== | ||
| + | |||
| <code powershell> | <code powershell> | ||
| # Filter lines by regex. | # Filter lines by regex. | ||
| Line 77: | Line 169: | ||
| #left:30 - right:30 | #left:30 - right:30 | ||
| #left:40 - right:40 | #left:40 - right:40 | ||
| + | </ | ||
| + | |||
| + | ==== Regex groups ==== | ||
| + | |||
| + | <code powershell> | ||
| + | $regex=[regex] (' | ||
| + | $input=' | ||
| + | $matches = $regex.matches($input) | ||
| + | if (!($matches.Success)) { | ||
| + | Write-Host "Regex failed to match: $input" | ||
| + | Exit(-1) | ||
| + | } | ||
| + | Write-Host " | ||
| + | Write-Host " | ||
| + | Write-Host " | ||
| </ | </ | ||
powershell_cheat_sheet.1568600041.txt.gz · Last modified: 2019/09/17 03:14 (external edit)
