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 [2020/10/16 20:13] – [Search installed certs for thumbprint] 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 42: Line 59:
 </code> </code>
  
-===== Search installed certs for thumbprint =====+===== Certificates ===== 
 + 
 +Search by thumbprint:
  
 <code powershell> <code powershell>
Line 52: Line 71:
 <code powershell> <code powershell>
 Get-ChildItem -Path Cert: -Recurse | Where-Object { $_.Thumbprint -and $_.Thumbprint.ToLower().StartsWith('c4') } | Test-Certificate 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> </code>
  
Line 70: Line 105:
   - Once installation is complete, comment out commands and change password.   - 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>
  
 ===== Text Processing ===== ===== Text Processing =====
powershell_cheat_sheet.1602879221.txt.gz · Last modified: 2020/10/17 21:13 (external edit)

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki