1. Windows Powershell
Kurze Einführung in
Windows Powershell
Software: Planen. Entwickeln. Testen.
2. Was ist Windows Powershell?
Eine von Microsoft entwickelte Alternative
zum Kommandozeilenprogramm «cmd» und
Windows Host Script
Sehr mächtig (powerful)
Software: Planen. Entwickeln. Testen.
2
3. Eigenschaften
• Ist Objekt basierend (nicht Text basierend)
• Basiert auf .net
• Hat vollen Zugriff auf .net Framework
• Ab Windows 7 vorinstalliert
• Kommt mit «Entwicklungsumgebung» inkl.
Debugger.
• Ist «Sicherheitsbewusst» (Skripte lassen sich
nicht ohne weiteres ausführen, Skripts
müssen signiert sein, etc.)
Software: Planen. Entwickeln. Testen.
3
4. Eigenschaften
• Cmdlets («Command-lets» ) als kleinste
Funktionseinheit
• Benennungsschema: Verb-Substantiv
– Get-help
– Get-process
• Case insensitiv
• Hat viele Alias
– «dir» für «get-childItem»
– «cd» für «set-location»
• Alias können selber erzeugt werden
• Variable beginnen mit «$»
Software: Planen. Entwickeln. Testen.
4
5. «Piping»
• «Piping» («|») ist wichtig
Get-process | sort-object –property id | more
• “$_”: Als Platzhalter für das aktuelle Objekt.
Offizielle Definition für “$_.” das aktuelle
Piplineobjekt
Get-Service | where {$_.status -eq "Running" }
Software: Planen. Entwickeln. Testen.
5
6. Definierte Variable
Variable Name Description
The current pipeline object; used in script blocks, filters, the process clause of functions, where-object, foreach-object and
$_ switch
$^ contains the first token of the last line input into the shell
$$ contains the last token of last line input into the shell
$? Contains the success/fail status of the last statement
$Args Used in creating functions that require parameters
$Env:Path Environmental Path to files.
$Error If an error occurred, the object is saved in the $error PowerShell variable
$foreach Refers to the enumerator in a foreach loop.
$HOME The user's home directory; set to %HOMEDRIVE%%HOMEPATH%
$Input Input piped to a function or code block
$Match A hash table consisting of items found by the -match operator.
$MyInvocation Information about the currently script or command-line
$Host Information about the currently executing host
$LastExitCode The exit code of the last native application to run
$true Boolean TRUE
$false Boolean FALSE
$null A null object
$OFS Output Field Separator, used when converting an array to a string. By default, this is set to the space character
The identifier for the shell. This value is used by the shell to determine the ExecutionPolicy and what profiles are run at
$ShellID startup.
$StackTrace contains detailed stack trace information about the last error
Software: Planen. Entwickeln. Testen.
6
13. «dot Source» a Script
Statt Scriptaufruf
«c:pfadscript.ps1»
Diesen Aufruf:
«. c:pfadscript.ps1» (Beachte Punkt und Blank)
Die Variablen, die innerhalb des Scirpts
definiert sind, stehen nach Ablauf des
Scripts zur Verfügung. (sie sind global
geworden)
Software: Planen. Entwickeln. Testen.
13