SKRIPTE PRÜFEN MIT PESTER BeforeAll { function Get-Letters ([string]$Name = '*') { $letters = @( @{ Name = 'D' } @{ Name = 'C' } @{ Name = 'B' } @{ Name = 'A' } ) | ForEach-Object { [PSCustomObject] $_ } $letters | Where-Object { $_.Name -like $Name } } } POWERSHELL-ERWEITERUNGEN Listing 1: Audiogeräte steuern Import-Module AudioDeviceCmdlets, Graphical -Verbose $Device = Get-AudioDevice -Playback [int[]]$datapoints =@(0)*50 do { $PeakValue = $Device.Device.AudioMeterIn formation.MasterPeakValue*100 $datapoints += [int]$PeakValue $datapoints = $datapoints | Select-Object -last 50 Clear-Host Show-Graph -datapoints $datapoints -GraphTitle AudioLevels Show-Graph -datapoints $datapoints -GraphTitle AudioLevels -Type Line Show-Graph -datapoints $datapoints -GraphTitle AudioLevels -Type Scatter Start-Sleep -Milliseconds 1000 } while ($true) Listing 2: Erstellen eines Auswahlmenüs Import-Module PS-Menu $result = menu @("Fauchen", "Wefzen", "Zenzen") -ReturnIndex Write-Output $result if($result -eq 0) { Write-Output "Lasset uns Fauchen" } elseif($result -eq 1) { Write-Output "Lasset uns Wefzen" } elseif($result -eq 2) { Write-Output "Lasset uns Zenzen" } NACHRICHTEN IN MS TEAMS Listing 1: Nachricht per ReST aus der PowerShell $DockerCmd = "Kommando, das Status von Docker-Abbild prüft" $JSONBody = [PSCustomObject][Ordered]@{ "@type" = "MessageCard" "@context" = "" "summary" = "Created: $($DockerCmd.name)" "themeColor" = '0078D7' "title" = "DockerImage Ready" "text" = "`Image Name: $($DockerCmd.name)" + "`Build Time: $($DockerCmd.buildTime)" + "`Build Duration: $($DockerCmd.buildDuration)" + "`Image path: $($DockerCmd.imagePath)" + "`nTimestamp: $($DockerCmd.timestamp.ToString())" } $TeamMessageBody = ConvertTo-Json $JSONBody $parameters = @{ "URI" = "https://..." "Method" = 'POST' "Body" = $TeamMessageBody "ContentType" = 'application/json' } Invoke-RestMethod @parameters Listing 2: Zugriffstoken für Graph $clientId = Application ID $tenantId = Directory ID $graphScopes = "user.read mail.read mail.send" Connect-MgGraph -ClientId $clientId -TenantId $tenantId -Scopes $graphScopes -UseDeviceAuthentication Get-MgContext Listing 3: Chat zwischen zwei Teilnehmern erstellen Import-Module Microsoft.Graph.Teams $params = @{ ChatType = "oneOnOne" Members = @( @{ "@odata.type" = "#microsoft.graph. aadUserConversationMember" Roles = @( "owner" ) "User@odata.bind" = "https://graph.microsoft.com/v1.0/users ('xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxx')" } @{ "@odata.type" = "#microsoft.graph. aadUserConversationMember" Roles = @( "owner" ) "User@odata.bind" = "https://graph.microsoft.com/v1.0/users ('yyyyyyyy-yyyy-yyyy-yyyy-yyyyyyyyyy')" } ) } $myChatSession=New-MgChat -BodyParameter $params Listing 4: PowerShell-Nachricht $Body = @{ ContentType = 'html' Content = @'Dies ist eine beispielhafte Nachricht.'@ } New-MgChatMessage -ChatId $myChatSession.id -Body $Body TAB-VERVOLLSTÄNDIGUNG Listing 1: Beispiel für einfache Parameter Function Tolle-Funktion { param( $Name, $Anzahl) For($i = 1 ; $i -le $Anzahl; $i++){ "Anzeige: $Name, Durchlauf $i von $Anzahl" } } Listing 2: Restart-Remote-Funktion ohne dynamische Vervollständigung Function Restart-RemoteService{ Param($computername=$env:COMPUTERNAME,$srv ="BITS") ForEach($machine in $computername){ write-host "Stopping service $srv..." -NoNewline Get-WmiObject -ClassName Win32\_Service -ComputerName $machine | Where Name -eq $srv | % StopService | Out-Null write-host "\[OK\]" -Foreground Color Cyan Write-Host "Starting Service $srv..." -NoNewline Get-WmiObject -ClassName Win32\_Service -ComputerName $machine | Where Name -eq $srv | % StartService | Out-Null write-host "\[OK\]" -ForegroundColor Cyan } } POWERSHELL MIT KI AUFPEPPEN Listing 1: Fibonacci-Ausgabe mit ChatGPT4 function Get-Fibonacci { param ( [int]$n, [int]$first = 0, [int]$second = 1 ) if ($n -le 0) { return } Write-Host $first Get-Fibonacci -n ($n - 1) -first $second -second ($first + $second) } Get-Fibonacci -n 10 ## Ausgabe: 0 1 1 2 3 5 8 13 21 34 Listing 2: Fibonacci-Ausgabe mit Bing Chat function Get-Fibonacci($n) { if ($n -lt 2) { return $n } else { return (Get-Fibonacci ($n - 1)) + (Get-Fibonacci ($n - 2)) } } Get-Fibonacci -n 10 ## Ausgabe: 55 Listing 3: Antwort von ChatGPT auf Aufgabe 3 (RegEx) ^(?=(?:[^0-9]*[0-9]){2})(?=(?:[^A-Za-z]* [A-Za-z]){2}).{6}$ Dieser Ausdruck bedeutet: ^ und $: Anfang und Ende der Zeichenkette. (?=(?:[^0-9]*[0-9]){2}): Ein Lookahead, der sicherstellt, dass mindestens zwei Zahlen vorhanden sind. (?=(?:[^A-Za-z]*[A-Za-z]){2}): Ein Lookahead, der sicherstellt, dass mindestens zwei Buchstaben vorhanden sind. .{6}: Genau sechs beliebige Zeichen. Listing 4: Übersetzen mit DeepL ## DeepL-Free API ## https://www.deepl.com/docsapi/documents/translate-document $API_KEY = 'YOUR-API-KEY' $uri = 'https://api-free.deepl.com/v2/ translate' $text = @' Space, the final frontier These are the voyages of the Starship Enterprise Its five year mission To explore strange new worlds To seek out new life And new civilizations To boldly go where no man has gone before '@ $headers = @{ 'Authorization' = "DeepL-Auth-Key $API_KEY" } $body = @{ 'text'=$text 'target_lang'='DE' } $request = Invoke-RestMethod -Method 'Post' -Headers $headers -Uri $uri -Body $body $request.translations.text ## AUSGABE Der Weltraum, die letzte Grenze Dies sind die Reisen des Raumschiffs Enterprise Seine fünfjährige Mission Um fremde neue Welten zu erforschen Auf der Suche nach neuem Leben Und neue Zivilisationen Um kühn dorthin zu gehen, wo noch kein Mensch zuvor war