Private/Wissen/X12_Technology_PowerShell.ps1

# ? TITEL PowerShell
# ? DESCRIPTION Weitere Möglichkeiten und Beispiele der PowerShell
# ? TAGS Transcript FormatEnumerationLimit Transaction Jobs Sleep Guid
# ? VERSION 2019.09.20

#region Informationen über den akt. Host ermitteln

$Host
$Host.PrivateData
$Host.Runspace
$PSVersionTable

#endregion

#region Konsolen-Ein-/Ausgabe protokollieren

Start-Transcript -Path C:\Temp\PowerShellConsole.log
Get-Process | Stop-Process -Force -WhatIf
Remove-Item c:\ -Recurse -Force -WhatIf
Stop-Transcript
Get-Content -Path C:\Temp\PowerShellConsole.log

#endregion

#region max. Anzeige von Enumerationen (-1 ohne Grenze, Default: 4)

$FormatEnumerationLimit
Get-Command -Name ForEach-Object | Select-Object -First 1 | Format-List * # Siehe Eigenschaft: Parameters
$FormatEnumerationLimit = 5
Get-Command -Name ForEach-Object | Select-Object -First 1 | Format-List * # Siehe Eigenschaft: Parameters

#endregion

#region PowerShell-Transaktion (*-Transaction)

Get-Command -Noun Transaction -Module Microsoft.PowerShell.*

# Gilt nur für Cmdlets die den Parameter UseTransaction besitzen
Get-Help * -Parameter UseTransaction 

#
# BEISPIELE
#

Set-Location -Path hkcu:\software

Start-Transaction
New-Item _ABC -UseTransaction
New-ItemProperty -Path _ABC -name Heute -value (Get-Date) -UseTransaction
Complete-Transaction

Get-ItemProperty -Path _ABC
Remove-Item -Path _ABC -Force

Start-Transaction
New-Item _ABC -UseTransaction
New-ItemProperty -Path _ABC -name Heute -value (Get-Date) -UseTransaction
Undo-Transaction

Get-ItemProperty -Path _ABC

#endregion

#region Job's

# Lang anhaltende Aufgaben in Jobs auslagern um in der akt. Session weiter arbeiten zu können

Start-Job -Name MyJob -ScriptBlock {[System.Threading.Thread]::Sleep(2000); Get-Process}
Get-Job
Receive-Job -Name MyJob
Stop-Job -Name MyJob
Wait-Job -Name MyJob
Remove-Job -Name MyJob

#endregion

#region Die Anzahl von Gruppen-Elementen bestimmen

Get-ChildItem -Path C:\Windows\System32 -File -Force | 
    Group-Object -Property Extension -NoElement | 
    Where-Object Count -gt 1 | 
    Sort-Object -Property Count -Descending 

#endregion

#region Eine Anzahl an Sekunden warten

Start-Sleep -Seconds 3

#endregion

#region Eine eindeutige Id erstellen

# z.B. für Dateinamen, Primärschlüssel, ...

New-Guid | Select-Object -ExpandProperty Guid

#endregion