EINSATZ FREMDER SKRIPTE UND MODULE Listing 1: Code mit AST prüfen $file = "" $AST = [System.Management.Automation. Language.Parser]::ParseFile( $File, [ref]$null, [ref]$null ) $commandsFound = @() $AST.FindAll({$args[0].GetType().Name -like 'CommandAst'}, $true) | ForEach-Object { $commandsFound += [pscustomobject]@{ Cmdlet = $_.CommandElements[0].Value Parameters = $_.CommandElements. ParameterName } } $commandsFound | Sort-Object Cmdlet, Parameters -Unique PERFORMANTE UND BENUTZERFREUNDLICHE CMDLETS Listing 1: Get-WeatherReport-Cmdlet function Get-WeatherReport { [CmdletBinding()] param ( [string] $Apikey = [Parameter(Mandatory,ValueFromPipeline)] [string[]] $City, [string] $Units = 'metric' ) ## Main process { foreach ($item in $City) { $Uri = "http://api.openweathermap.org/data/2.5/weather?units=$Units&q=$Item&appid=$Apikey" Invoke-RestMethod -Uri $Uri | Select-Object -ExpandProperty Main } } } Get-WeatherReport -City 'Munic','Berlin','Hamburg' | Format-Table 'Köln','Frankfurt','Stuttgart' | Get-WeatherReport | Format-Table Listing 2: Verbesserte Version des Get-WeatherReport-Cmdlet function Get-WeatherReport { [CmdletBinding()] [Alias('gwr')] param ( [string] $Apikey = , [Parameter(Mandatory, ValueFromPipeline)] [string[]] $City, [string] $Units = 'metric' ) begin { switch ($Units) { 'metric' { $unit = 'Celsius' } 'imperial' { $unit = 'Fahrenheit' } 'standard' { $unit = 'Kelvin' } } $stopwatch = [System.Diagnostics.Stopwatch]::StartNew() } process { foreach ($item in $City) { $uri = "http://api.openweathermap.org/data/2.5/weather?units=$Units&q=$item&appid=$Apikey" try { $request = Invoke-RestMethod -UseBasicParsing -Uri $uri -Verbose:$false -ErrorAction Stop [pscustomobject] @{ 'City' = $request.name 'Description' = $request.weather.DESCRIPTION 'AirPressure' = $request.main.pressure 'AirHumidity' = $request.main.humidity 'Temperature' = $request.main.temp 'Unit' = $unit } } catch { Write-Warning -Message "Webrequest failed! City: $City, API-Key: $Apikey, Units: $Units" } } } end { 'Runtime(ms): ' + $stopwatch.Elapsed.TotalMilliseconds | Write-Verbose } } Get-WeatherReport -City 'Munic', 'Berlin', 'Hamburg' -Units imperial -Verbose | Format-Table 'Cologne', 'Frankfurt', 'Stuttgart' | Get-WeatherReport -Verbose | Format-Table Listing 3: Invoke-Command geschickt einsetzen function Get-HardwareInformation { [CmdletBinding()] [Alias('ghwi')] Param ( [Parameter(Mandatory,ValueFromPipeline)] [string[]] $Computername ) Begin { $stopwatch = [System.Diagnostics.Stopwatch]::StartNew() $code = { } ## Ihr Code zur Hardwareinventarisierung } Process { ## Wir sammeln die übergebenen Computernamen in einem neuen Array [string[]] $Computernames += $Computername } End { ## MAIN-Code $psSessions = New-PSSession -ComputerName $Computernames -ErrorAction SilentlyContinue Invoke-Command -Session $psSessions -ScriptBlock $code $psSessions | Remove-PSSession ## Laufzeit ermitteln Write-Verbose -message "Runtime(ms): $($stopwatch.ElapsedMilliseconds)" } } NATIVEN CODE NUTZEN Listing 1: C#-Code in einer Variablen speichern $dotnetclass = @" public class Dog { public Dog() { Name = "Snoopy"; } public Dog (string name) { Name = name; } public string Name { get; set; } public override string ToString() { return "Woof! I am " + Name; } public static string Bark () { return "Woof! Woof!"; } } "@ Add-Type -TypeDefinition $dotnetclass Listing 2: P/Invoke für CopyFile aus der Kernel32.dll [DllImport("kernel32.dll", SetLastError=true, CharSet=CharSet.Auto)] [return: MarshalAs(UnmanagedType.Bool)] static extern bool CopyFileEx(string lpExistingFileName, string lpNewFileName, CopyProgressRoutine lpProgressRoutine, IntPtr lpData, ref Int32 pbCancel, CopyFileFlags dwCopyFlags);