User Tools

Site Tools


powershell_cheat_sheet

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
powershell_cheat_sheet [2019/09/14 21:56] stephenpowershell_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."
 +}
 +</code>
 +
 +===== Check previous command was successful =====
 +
 +<code powershell>
 +if ($?) {
 + Write-Host 'Success' -ForegroundColor Green
 +} else {
 + Write-Host 'FAILED' -ForegroundColor Red
 + throw 'Failed.'
 } }
 </code> </code>
Line 22: Line 33:
 <code powershell> <code powershell>
 (Get-Content $filename) -replace 'heise', 'Heise' | Set-Content $filename (Get-Content $filename) -replace 'heise', 'Heise' | Set-Content $filename
 +</code>
 +
 +===== Create a timestamp =====
 +
 +<code powershell>
 +$timestamp = (Get-Date).ToString("yyyyMMdd HHmmssff")
 </code> </code>
  
Line 28: Line 45:
 <code powershell> <code powershell>
 param ( param (
-    [parameter(Mandatory=$true)] [string]$targetServer, +    [parameter(Mandatory=$true,HelpMessage='The target server')] [string]$targetServer, 
-    [parameter(Mandatory=$true)] [string]$targetWebConfig+    [parameter(Mandatory=$true)] [string]$targetWebConfig
 +    [parameter(Mandatory=$false)] [string]$build = 'debug'
 ) )
 </code> </code>
  
-===== Search installed certs for thumbprint =====+===== Set current directory to script location ===== 
 + 
 +<code powershell> 
 +Push-Location "$($MyInvocation.MyCommand.Path)\..\" 
 +#... 
 +Pop-Location 
 +</code> 
 + 
 +===== Certificates ===== 
 + 
 +Search by thumbprint:
  
 <code powershell> <code powershell>
 Get-ChildItem -Path Cert: -Recurse | Where-Object { $_.Thumbprint -and $_.Thumbprint.ToLower().StartsWith('c4') } Get-ChildItem -Path Cert: -Recurse | Where-Object { $_.Thumbprint -and $_.Thumbprint.ToLower().StartsWith('c4') }
 +</code>
 +
 +To check if the cert is valid, pipe it the ''Test-Certificate'' command:
 +
 +<code powershell>
 +Get-ChildItem -Path Cert: -Recurse | Where-Object { $_.Thumbprint -and $_.Thumbprint.ToLower().StartsWith('c4') } | Test-Certificate
 +</code>
 +
 +To check for expired / revoked certs:
 +
 +<code powershell>
 +$certs = Get-ChildItem -Path Cert:\LocalMachine\My -Recurse
 +foreach ($cert in $certs) {
 +    Write-Host "$($cert.Thumbprint) $($cert.Subject)"
 + 
 +    if ($certs.NotAfter -lt (Get-Date)) {
 +        Write-Host "    Expired on $($certs.NotAfter)" -ForegroundColor Red
 +    }
 +    elseif ($cert | Test-Certificate) {
 +        Write-Host "    Tested ok." -ForegroundColor Green
 +    }
 +}
 +</code>
 +
 +===== Get Install-Module working behind a webproxy =====
 +
 +  - Run ''notepad $PROFILE''.
 +  - Add this to the top:<code powershell>
 +# Uncomment this to use PowerShellGet
 +#$Username="global\heises"
 +#$Password="password"
 +#[system.net.webrequest]::defaultwebproxy = new-object system.net.webproxy('http://10.104.66.70:80')
 +#[system.net.webrequest]::defaultwebproxy.credentials = New-Object System.Net.NetworkCredential($Username, $Password)
 +#[system.net.webrequest]::defaultwebproxy.BypassProxyOnLocal = $true</code>
 +  - Uncomment, fill out the details, and save.
 +  - Start a //new// Powershell session.
 +  - Run ''Register-PSRepository -Default''
 +  - Run ''Get-PSRepository'' again to verify.
 +  - Once installation is complete, comment out commands and change password.
 +
 +If this doesn't work, and it won't for an ANZ Dev VM, try the [[powershell_manual_module_installation|PowerShell Manual Module Installation]].
 +
 +===== No Sleep =====
 +
 +May only work inside Windows Powershell ISE.
 +
 +<code powershell>
 +$intervalSeconds = 60
 +$mouseDistance = 1
 +
 +$myshell = New-Object -com "Wscript.Shell"
 +while ($true) {
 +    Start-Sleep -Seconds $intervalSeconds
 +    $Pos = [System.Windows.Forms.Cursor]::Position
 +    [System.Windows.Forms.Cursor]::Position = New-Object System.Drawing.Point((($Pos.X) + $mouseDistance) , $Pos.Y)
 +    [System.Windows.Forms.SendKeys]::SendWait("{f15}")
 +    Write-Host 'A shuffle to the right.'
 +    Start-Sleep -Seconds $intervalSeconds
 +    $Pos = [System.Windows.Forms.Cursor]::Position
 +    [System.Windows.Forms.Cursor]::Position = New-Object System.Drawing.Point((($Pos.X) - $mouseDistance) , $Pos.Y)
 +    [System.Windows.Forms.SendKeys]::SendWait("{f15}")
 +    Write-Host 'A shuffle to the left.'
 +}
 </code> </code>
  
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
 +</code>
 +
 +==== Regex groups ====
 +
 +<code powershell>
 +$regex=[regex] ('-rw-rw---- 1 root sdcard_rw (?<length>\d+) (?<date>\d\d\d\d-\d\d-\d\d \d\d:\d\d) (?<filename>.*)$')
 +$input='-rw-rw---- 1 root sdcard_rw 358 2019-09-01 19:50 /storage/emulated/0/Podcasts/Sync Test/New folder/rua.txt'
 +$matches = $regex.matches($input)
 +if (!($matches.Success)) {
 + Write-Host "Regex failed to match: $input" -ForegroundColor Red
 + Exit(-1)
 +}
 +Write-Host "length   = $($matches.Captures.Groups["length"].value)"
 +Write-Host "date     = $($matches.Captures.Groups["date"].value)"
 +Write-Host "filename = $($matches.Captures.Groups["filename"].value)"
 </code> </code>
  
Line 86: Line 193:
 </code> </code>
  
 +==== Pipeline function ====
  
 +<code powershell>
 +function Replace-String {
 +    [cmdletbinding()]
 +    Param (
 +        [parameter(ValueFromPipeline)]
 +        [string]$Input,
 +        [string]$OldValue,
 +        [string]$NewValue
 +    )
 +
 +    Process {
 +        $Input -replace $OldValue, $NewValue
 +    }
 +}
 +
 +'djkslajklfdsjkl' | Replace-String -OldValue 'd' -NewValue 'x'
 +</code>
powershell_cheat_sheet.1568498217.txt.gz · Last modified: 2019/09/15 22:56 (external edit)

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki