Creasoft - Windows powershell

14
Software: Planen. Entwickeln. Testen. Windows Powershell Kurze Einführung in Windows Powershell

description

 

Transcript of Creasoft - Windows powershell

Page 1: Creasoft - Windows powershell

Software: Planen. Entwickeln. Testen.

Windows Powershell

Kurze Einführung in Windows Powershell

Page 2: Creasoft - Windows powershell

Software: Planen. Entwickeln. Testen.

Was ist Windows Powershell?

Eine von Microsoft entwickelte Alternative zum Kommandozeilenprogramm «cmd» und Windows Host Script

Sehr mächtig (powerful)

2

Page 3: Creasoft - Windows powershell

Software: Planen. Entwickeln. Testen.

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.)

3

Page 4: Creasoft - Windows powershell

Software: Planen. Entwickeln. Testen.

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 «$»

4

Page 5: Creasoft - Windows powershell

Software: Planen. Entwickeln. Testen.

«Piping»

• «Piping» («|») ist wichtigGet-process | sort-object –property id | more

• “$_”: Als Platzhalter für das aktuelle Objekt. Offizielle Definition für “$_.” das aktuelle PiplineobjektGet-Service | where {$_.status -eq "Running" }

5

Page 6: Creasoft - Windows powershell

Software: Planen. Entwickeln. Testen.

Definierte VariableVariable 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

$ShellID The identifier for the shell. This value is used by the shell to determine the ExecutionPolicy and what profiles are run at startup.

$StackTrace contains detailed stack trace information about the last error

6

Page 7: Creasoft - Windows powershell

Software: Planen. Entwickeln. Testen.

Get-Help

• Get-help• Get-help set-location• Get-help set-location –full• Get-help set-location –example

7

Page 8: Creasoft - Windows powershell

Software: Planen. Entwickeln. Testen.

Get-Command

• Get-command• Get-command | more• Get-command | where-object {$_.CommandType -eq «Alias»}

(get-alias macht dasselbe)

8

Page 9: Creasoft - Windows powershell

Software: Planen. Entwickeln. Testen.

Weitere Befehle

• Variable Zuweisung: $a = get-command• Get-member (get-command | get-member)• Get-random

9

Page 10: Creasoft - Windows powershell

Software: Planen. Entwickeln. Testen.

Aufpassen

• Set-ExecutePolicy remoteSigned• Programm Start wenn es Leerzeichen im

Pfad hat: «&» voranstellen&«Pfad mit Blank\Prog.exe»

• Es braucht immer einen Pfadnamen:Myprogram inputfile.txt Myprogram .\inputfile.txt

• Vergleiche: eq, ne, gt, etc. (NICHT =!, >=, etc)

10

Page 11: Creasoft - Windows powershell

Software: Planen. Entwickeln. Testen.

Kleine Anwendung

• 100 Zufallszahlen zwischen 0 und 50 erzeugen:

for ($i=0; $i –le 99; $i++){get-random –maximum 50}

• In Variable schreiben$a = for ($i=0; $i –le 99; $i++){get-random –maximum 50}

• Ausgabe auf Datei:$a | Out-file –filepath «c:\work\random.txt»

11

Page 12: Creasoft - Windows powershell

Software: Planen. Entwickeln. Testen.

Powershell IDE

• Script erzeugen• Script debuggen (break points etc)• Befehle manuell eingeben• Powershell Fenster starten

12

Page 13: Creasoft - Windows powershell

Software: Planen. Entwickeln. Testen.

«dot Source» a Script

Statt Scriptaufruf «c:\pfad\script.ps1»

Diesen Aufruf:«. c:\pfad\script.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)

13

Page 14: Creasoft - Windows powershell

Software: Planen. Entwickeln. Testen.

Webseiten

• http://de.wikipedia.org/wiki/Windows_PowerShell• http://technet.microsoft.com/en-us/scriptcenter/dd742419• http://technet.microsoft.com/en-us/library/ee177003.aspx• http://www.computerperformance.co.uk/powershell/index.htm

• Quick Referenz:– http://www.microsoft.com/download/en/details.aspx?displaylang=en&id=7097

• http://www.powershellpro.com/

14