├── 02_ActiveDirectory ├── readme.md ├── sheets │ └── ScriptRunner-Webinar-PoSh-Crash-Course-Part-02.pdf └── Crashkurs_AD.PS1 ├── 03_PsModules ├── WriteLogMf │ ├── WriteLogMf.psd1 │ └── WriteLogMf.psm1 ├── sheets │ └── ScriptRunner-Webinar-PoSh-Crash-Course-Part-03.pdf ├── WriteLog.ps1 ├── WriteLog │ └── WriteLog.psm1 ├── WriteLog.psm1 └── MyFirstModule.ps1 ├── 04_PsProvider ├── sheets │ └── ScriptRunner-Webinar-PoSh-Crash-Course-Part-04.pdf ├── slides.md └── codesnippets.ps1 ├── 01_GettingStarted ├── sheets │ └── ScriptRunner-Webinar-PoSh-Crash-Course-Part-01.pdf ├── MyFirstScript.ps1 └── PoshCrashCourse_2016-10-20.ps1 └── README.md /02_ActiveDirectory/readme.md: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /03_PsModules/WriteLogMf/WriteLogMf.psd1: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/scriptrunner/PoShCrashCourse/HEAD/03_PsModules/WriteLogMf/WriteLogMf.psd1 -------------------------------------------------------------------------------- /03_PsModules/sheets/ScriptRunner-Webinar-PoSh-Crash-Course-Part-03.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/scriptrunner/PoShCrashCourse/HEAD/03_PsModules/sheets/ScriptRunner-Webinar-PoSh-Crash-Course-Part-03.pdf -------------------------------------------------------------------------------- /04_PsProvider/sheets/ScriptRunner-Webinar-PoSh-Crash-Course-Part-04.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/scriptrunner/PoShCrashCourse/HEAD/04_PsProvider/sheets/ScriptRunner-Webinar-PoSh-Crash-Course-Part-04.pdf -------------------------------------------------------------------------------- /01_GettingStarted/sheets/ScriptRunner-Webinar-PoSh-Crash-Course-Part-01.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/scriptrunner/PoShCrashCourse/HEAD/01_GettingStarted/sheets/ScriptRunner-Webinar-PoSh-Crash-Course-Part-01.pdf -------------------------------------------------------------------------------- /02_ActiveDirectory/sheets/ScriptRunner-Webinar-PoSh-Crash-Course-Part-02.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/scriptrunner/PoShCrashCourse/HEAD/02_ActiveDirectory/sheets/ScriptRunner-Webinar-PoSh-Crash-Course-Part-02.pdf -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # PowerShell-Crash-Course 2 | 3 | ## [01 A Brief Introduction To PowerShell Scripting](./01_GettingStarted) 4 | 5 | ## [02 How-To Manage ActiveDirectory](./02_ActiveDirectory) 6 | 7 | ## [03 My First PowerShell Module](./03_PsModules) 8 | 9 | ## [04 Using PowerShell Provider](./04_PsProvider) 10 | 11 | ... *to be continued* -------------------------------------------------------------------------------- /01_GettingStarted/MyFirstScript.ps1: -------------------------------------------------------------------------------- 1 | 2 | param( 3 | #[parameter(Mandatory = $true)] 4 | [string]$a, 5 | [int]$b 6 | ) 7 | 8 | "Hello World " + $a 9 | 10 | $b + 1 11 | $a + 1 12 | 13 | 14 | if($a -eq 'a'){ 15 | Write-Output "Weg1 $a" 16 | } 17 | else{ 18 | Write-Output "Weg2 $a" 19 | } 20 | 21 | $ar = @('abc', 'bc', 'cdef') 22 | 23 | foreach($item in $ar) 24 | { 25 | $item.Length 26 | if($item.Length -eq 2){ 27 | break; 28 | } 29 | } 30 | 31 | $i = 1 32 | 33 | do { 34 | $i++ 35 | $i 36 | } 37 | until ($i -gt 5) 38 | 39 | $i = 2 40 | for(;$i -lt 5; $i++ ) 41 | { 42 | $i 43 | } 44 | 45 | -------------------------------------------------------------------------------- /03_PsModules/WriteLog.ps1: -------------------------------------------------------------------------------- 1 | 2 | <# 3 | .SYNOPSIS 4 | Write Log messages to the LogFilePath. 5 | 6 | .DESCRIPTION 7 | Write Log messages to the LogFilePath. Possible seveity levels are 'INFO','WARNING','ERROR','FATAL' and 'DEBUG'. 8 | 9 | .PARAMETER Message 10 | The log message. 11 | 12 | .PARAMETER Level 13 | The severity level of the log message. 14 | 15 | .PARAMETER LogFilePath 16 | Parameter description 17 | 18 | .EXAMPLE 19 | An example 20 | 21 | .NOTES 22 | General notes 23 | #> 24 | 25 | $Global:SeverityLevel = 'INFO' 26 | Function Write-Log { 27 | [CmdletBinding()] 28 | Param( 29 | [Parameter(Mandatory=$True)] 30 | [string]$Message, 31 | [ValidateSet('INFO','WARNING','ERROR','FATAL','DEBUG')] 32 | [String]$Level = "INFO", 33 | [string]$LogFilePath 34 | ) 35 | 36 | $timeStamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss.fff" 37 | $line = "$timeStamp [$Level] $Message" 38 | switch ($Global:SeverityLevel) { 39 | 'DEBUG' { break } 40 | 'INFO' { 41 | if($Level -eq 'DEBUG'){ 42 | Write-Output $line 43 | exit 44 | } 45 | break 46 | } 47 | 'WARNING' { 48 | if(($Level -eq 'DEBUG') -or ($Level -eq 'INFO')){ 49 | Write-Output $line 50 | exit 51 | } 52 | break 53 | } 54 | 'ERROR' { 55 | if(($Level -ne 'ERROR') -or ($Level -ne 'FATAL')){ 56 | Write-Output $line 57 | exit 58 | } 59 | break 60 | } 61 | 'FATAL' { 62 | if($Level -ne 'FATAL'){ 63 | Write-Output $line 64 | exit 65 | } 66 | break 67 | } 68 | } 69 | if($LogFilePath) { 70 | Add-Content -Path $LogFilePath -Value $line -PassThru -Force -ErrorAction SilentlyContinue 71 | } 72 | else { 73 | Write-Output $line 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /03_PsModules/WriteLog/WriteLog.psm1: -------------------------------------------------------------------------------- 1 | 2 | <# 3 | .SYNOPSIS 4 | Write Log messages to the LogFilePath. 5 | 6 | .DESCRIPTION 7 | Write Log messages to the LogFilePath. Possible seveity levels are 'INFO','WARNING','ERROR','FATAL' and 'DEBUG'. 8 | 9 | .PARAMETER Message 10 | The log message. 11 | 12 | .PARAMETER Level 13 | The severity level of the log message. 14 | 15 | .PARAMETER LogFilePath 16 | Parameter description 17 | 18 | .EXAMPLE 19 | An example 20 | 21 | .NOTES 22 | General notes 23 | #> 24 | 25 | 26 | $script:SeverityLevel = 'INFO' 27 | 28 | function Set-SeverityLevel { 29 | param( 30 | [ValidateSet('INFO','WARNING','ERROR','FATAL','DEBUG')] 31 | [string]$Level = 'Info' 32 | ) 33 | 34 | $script:SeverityLevel = $Level 35 | } 36 | 37 | function Get-SeverityLevel { 38 | Get-Variable -Scope Script -Name SeverityLevel -ValueOnly 39 | } 40 | 41 | $script:SeverityLevel = 'INFO' 42 | Function Write-Log { 43 | [CmdletBinding()] 44 | Param( 45 | [Parameter(Mandatory=$True)] 46 | [string]$Message, 47 | [ValidateSet('INFO','WARNING','ERROR','FATAL','DEBUG')] 48 | [String]$Level = "INFO", 49 | [string]$LogFilePath 50 | ) 51 | 52 | $timeStamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss.fff" 53 | $line = "$timeStamp [$Level] $Message" 54 | switch ($script:SeverityLevel) { 55 | 'DEBUG' { break } 56 | 'INFO' { 57 | if($Level -eq 'DEBUG'){ 58 | exit 59 | } 60 | break 61 | } 62 | 'WARNING' { 63 | if(($Level -eq 'DEBUG') -or ($Level -eq 'INFO')){ 64 | exit 65 | } 66 | break 67 | } 68 | 'ERROR' { 69 | if(($Level -ne 'ERROR') -or ($Level -ne 'FATAL')){ 70 | exit 71 | } 72 | break 73 | } 74 | 'FATAL' { 75 | if($Level -ne 'FATAL'){ 76 | exit 77 | } 78 | break 79 | } 80 | } 81 | if($LogFilePath) { 82 | Add-Content -Path $LogFilePath -Value $line -PassThru -Force -ErrorAction SilentlyContinue 83 | } 84 | else { 85 | Write-Output $line 86 | } 87 | } 88 | 89 | 90 | Export-ModuleMember -Function 'Write-Log', 'Set-SeverityLevel', 'Get-SeverityLevel' 91 | 92 | -------------------------------------------------------------------------------- /03_PsModules/WriteLogMf/WriteLogMf.psm1: -------------------------------------------------------------------------------- 1 | 2 | <# 3 | .SYNOPSIS 4 | Write Log messages to the LogFilePath. 5 | 6 | .DESCRIPTION 7 | Write Log messages to the LogFilePath. Possible seveity levels are 'INFO','WARNING','ERROR','FATAL' and 'DEBUG'. 8 | 9 | .PARAMETER Message 10 | The log message. 11 | 12 | .PARAMETER Level 13 | The severity level of the log message. 14 | 15 | .PARAMETER LogFilePath 16 | Parameter description 17 | 18 | .EXAMPLE 19 | An example 20 | 21 | .NOTES 22 | General notes 23 | #> 24 | 25 | 26 | $script:SeverityLevel = 'INFO' 27 | 28 | function Set-SeverityLevel { 29 | param( 30 | [ValidateSet('INFO','WARNING','ERROR','FATAL','DEBUG')] 31 | [string]$Level = 'Info' 32 | ) 33 | 34 | $script:SeverityLevel = $Level 35 | } 36 | 37 | function Get-SeverityLevel { 38 | Get-Variable -Scope Script -Name SeverityLevel -ValueOnly 39 | } 40 | 41 | $script:SeverityLevel = 'INFO' 42 | Function Write-Log { 43 | [CmdletBinding()] 44 | Param( 45 | [Parameter(Mandatory=$True)] 46 | [string]$Message, 47 | [ValidateSet('INFO','WARNING','ERROR','FATAL','DEBUG')] 48 | [String]$Level = "INFO", 49 | [string]$LogFilePath 50 | ) 51 | 52 | $timeStamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss.fff" 53 | $line = "$timeStamp [$Level] $Message" 54 | switch ($script:SeverityLevel) { 55 | 'DEBUG' { break } 56 | 'INFO' { 57 | if($Level -eq 'DEBUG'){ 58 | exit 59 | } 60 | break 61 | } 62 | 'WARNING' { 63 | if(($Level -eq 'DEBUG') -or ($Level -eq 'INFO')){ 64 | exit 65 | } 66 | break 67 | } 68 | 'ERROR' { 69 | if(($Level -ne 'ERROR') -or ($Level -ne 'FATAL')){ 70 | exit 71 | } 72 | break 73 | } 74 | 'FATAL' { 75 | if($Level -ne 'FATAL'){ 76 | exit 77 | } 78 | break 79 | } 80 | } 81 | if($LogFilePath) { 82 | Add-Content -Path $LogFilePath -Value $line -PassThru -Force -ErrorAction SilentlyContinue 83 | } 84 | else { 85 | Write-Output $line 86 | } 87 | } 88 | 89 | 90 | Export-ModuleMember -Function 'Write-Log', 'Set-SeverityLevel', 'Get-SeverityLevel' 91 | 92 | -------------------------------------------------------------------------------- /03_PsModules/WriteLog.psm1: -------------------------------------------------------------------------------- 1 | # WriteLog 2 | # Version: 1.0.0.0 3 | 4 | <# 5 | .SYNOPSIS 6 | Write Log messages to the LogFilePath. 7 | 8 | .DESCRIPTION 9 | Write Log messages to the LogFilePath. Possible seveity levels are 'INFO','WARNING','ERROR','FATAL' and 'DEBUG'. 10 | 11 | .PARAMETER Message 12 | The log message. 13 | 14 | .PARAMETER Level 15 | The severity level of the log message. 16 | 17 | .PARAMETER LogFilePath 18 | The file path of the log file. 19 | 20 | .EXAMPLE 21 | Write-Log -Message 'This is a sample log message.' -LogFilePath 'C:\Temp\log.txt' 22 | 2017-06-21 17:24:49.871 [INFO] This is a sample log message. 23 | 24 | .NOTES 25 | General notes 26 | #> 27 | Function Write-Log { 28 | [CmdletBinding()] 29 | Param( 30 | [Parameter(Mandatory=$True)] 31 | [string]$Message, 32 | [ValidateSet('INFO','WARNING','ERROR','FATAL','DEBUG')] 33 | [String]$Level = "INFO", 34 | [string]$LogFilePath 35 | ) 36 | 37 | $timeStamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss.fff" 38 | $line = "$timeStamp [$Level] $Message" 39 | switch ($script:SeverityLevel:SeverityLevel) { 40 | 'DEBUG' { break } 41 | 'INFO' { 42 | if($Level -eq 'DEBUG'){ 43 | exit 44 | } 45 | break 46 | } 47 | 'WARNING' { 48 | if(($Level -eq 'DEBUG') -or ($Level -eq 'INFO')){ 49 | exit 50 | } 51 | break 52 | } 53 | 'ERROR' { 54 | if(($Level -ne 'ERROR') -or ($Level -ne 'FATAL')){ 55 | exit 56 | } 57 | break 58 | } 59 | 'FATAL' { 60 | if($Level -ne 'FATAL'){ 61 | exit 62 | } 63 | break 64 | } 65 | } 66 | if($LogFilePath) { 67 | Add-Content -Path $LogFilePath -Value $line -PassThru -Force -ErrorAction SilentlyContinue 68 | } 69 | else { 70 | Write-Output $line 71 | } 72 | } 73 | 74 | <# 75 | .SYNOPSIS 76 | Set the severity log level. 77 | 78 | .DESCRIPTION 79 | Set the severity log level. 80 | 81 | .PARAMETER Level 82 | The severity level.Allowed values are 'INFO','WARNING','ERROR','FATAL' and 'DEBUG'. 83 | 84 | .EXAMPLE 85 | Set-SeverityLevel -Level 'Error' 86 | 87 | .NOTES 88 | General notes 89 | #> 90 | function Set-SeverityLevel { 91 | param( 92 | [ValidateSet('INFO','WARNING','ERROR','FATAL','DEBUG')] 93 | [string]$Level = 'INFO' 94 | ) 95 | Set-Variable -Scope Script -Name 'SeverityLevel' -Value $Level 96 | } 97 | 98 | <# 99 | .SYNOPSIS 100 | Get the severity log level. 101 | 102 | .DESCRIPTION 103 | Get the severity log level. 104 | 105 | .EXAMPLE 106 | Get-SeverityLevel 107 | 108 | .NOTES 109 | General notes 110 | #> 111 | function Get-SeverityLevel { 112 | Get-Variable -Scope Script -Name 'SeverityLevel' -ValueOnly 113 | } 114 | 115 | Set-Variable -Scope Script -Name 'SeverityLevel' -Value 'INFO' 116 | Export-ModuleMember -Function 'Write-Log', 'Get-SeverityLevel', 'Set-SeverityLevel' -Variable 'SeverityLevel' 117 | -------------------------------------------------------------------------------- /04_PsProvider/slides.md: -------------------------------------------------------------------------------- 1 | # PsProvider und PsDrives 2 | 3 | PowerShell Provider stellen Daten aus bestimmten Datenspeichern in der Windows PowerShell bereit, 4 | um sie dort anzeigen und bearbeiten zu können. 5 | Provider stellen die Daten in einem Laufwerk bereit. Auf die Daten kann somit über ein Verzeichnispfad 6 | zugegriffen werden, wie dies beispielsweise im Dateisystem möglich ist. 7 | Für die Bearbeitung der Daten können die PowerShell Built-in Cmdlets verwendet werden, 8 | die der jeweilige Provider unterstützt. 9 | 10 | ## BUILT-IN PROVIDERS 11 | 12 | - Alias Alias: Windows PowerShell aliases 13 | - Certificate Cert: x509 certificates for digital signatures 14 | - Environment Env: Windows environment variables 15 | - FileSystem * File system drives, directories, and files 16 | - Function Function: Windows PowerShell functions 17 | - Registry HKLM:, HKCU: Windows registry 18 | - Variable Variable: Windows PowerShell variables 19 | - WSMan WSMan: WS-Management configuration information 20 | 21 | ## Provider CmdLets 22 | 23 | Die folgenden CmdLets sind für den Zugriff und die Bearbeitung der Daten aller Provider gedacht. 24 | Diese können für alle Provider in der gleichen Weise benutzt werden. 25 | Haben Sie gelernt einen Provider zu verwenden, können Sie genauso auch mit allen anderen Provider arbeiten. 26 | 27 | ### CHILDITEM 28 | 29 | - Get-ChildItem 30 | 31 | ### CONTENT CMDLETS 32 | 33 | - Add-Content 34 | - Clear-Content 35 | - Get-Content 36 | - Set-Content 37 | 38 | ### ITEM CMDLETS 39 | 40 | - Clear-Item 41 | - Copy-Item 42 | - Get-Item 43 | - Invoke-Item 44 | - Move-Item 45 | - New-Item 46 | - Remove-Item 47 | - Rename-Item 48 | - Set-Item 49 | 50 | ### ITEMPROPERTY CMDLETS 51 | 52 | - Clear-ItemProperty 53 | - Copy-ItemProperty 54 | - Get-ItemProperty 55 | - Move-ItemProperty 56 | - New-ItemProperty 57 | - Remove-ItemProperty 58 | - Rename-ItemProperty 59 | - Set-ItemProperty 60 | 61 | ### LOCATION CMDLETS 62 | 63 | - Get-Location 64 | - Pop-Location 65 | - Push-Location 66 | - Set-Location 67 | 68 | ### PATH CMDLETS 69 | 70 | - Join-Path 71 | - Convert-Path 72 | - Split-Path 73 | - Resolve-Path 74 | - Test-Path 75 | 76 | ### PSDRIVE CMDLETS 77 | 78 | - Get-PSDrive 79 | - New-PSDrive 80 | - Remove-PSDrive 81 | 82 | ## Provider Typen 83 | 84 | ### 4 BasisTypen 85 | 86 | - Drive-Enabled Providers 87 | Drive CmdLets 88 | 89 | - Item-Enabled Providers 90 | Clear-Item 91 | Get-Item 92 | Set-Item 93 | Invoke-Item 94 | Test-Path 95 | 96 | - Container-Enabled Providers 97 | - ChildItem CmdLets: 98 | Get-ChildItem 99 | - Item CmdLets: 100 | Copy-Item 101 | New-Item 102 | Remove-Item 103 | Rename-Item 104 | Convert-Path 105 | HasChildItem 106 | 107 | - Navigation-Enabled Providers 108 | Join-Path 109 | Get-ParentPath 110 | Move-Item 111 | 112 | ### 2 Provider Interfaces 113 | 114 | - Content-Enabled Providers 115 | Implementiert Conent Cmdlets 116 | 117 | - Property-Enabled Providers 118 | Implementiert PropertyItem Cmdlets 119 | 120 | 121 | ## Links 122 | 123 | [PowerShell Documentation](https://docs.microsoft.com/en-us/powershell/) 124 | [Writing a Windows PowerShell Provider](https://msdn.microsoft.com/en-us/library/ee126192(v=vs.85).aspx) 125 | -------------------------------------------------------------------------------- /04_PsProvider/codesnippets.ps1: -------------------------------------------------------------------------------- 1 | # Code Snippets - PowerShell Grundlagen CrashKurs IV 2 | 3 | # Provider anzeigen 4 | Get-PSProvider 5 | 6 | # Hilfe zu PsProvidern anzeigen 7 | Get-Help * -Category provider 8 | Get-Help Registry 9 | Get-Help about_providers 10 | 11 | # PowerShell Laufwerke anzeigen 12 | Get-PSDrive 13 | 14 | # PowerShell Provider Dateisystem 15 | Get-PSDrive -PSProvider FileSystem 16 | 17 | # PowerShell Provider Registry 18 | Get-PSProvider -PSProvider Registry 19 | 20 | # Provider Typen anzeigen 21 | $prov = Get-PSProvider -PSProvider FileSystem 22 | $prov.Capabilities 23 | $prov.ImplementingType 24 | 25 | 26 | 27 | # PsDrive CmdLets 28 | Get-Command -Noun 'PsDrive' 29 | Get-PSDrive 30 | Get-PSDrive -PSProvider FileSystem 31 | 32 | # Laufwerk verbinden persistent (nur DateiSystem auf Remote machine) 33 | New-PSDrive -Name Z -PSProvider FileSystem -Root \\\ -Persist -Scope Global 34 | 35 | # temp. Laufwerk verbinden zu einem Registry-Pfad 36 | New-PSDrive -Name Wow6432 -PSProvider Registry -Root HKLM:\SOFTWARE\Wow6432Node -ErrorAction SilentlyContinue 37 | 38 | # Laufwerk entfernen 39 | Remove-PsDrive -Name Z -Force 40 | 41 | 42 | 43 | # Item enabled PsProvider CmdLets 44 | 45 | Test-Path -Path .\AppSphere\ScriptRunnerSvc\General -IsValid 46 | 47 | Get-Item -Path .\AppSphere\ScriptRunnerSvc\General 48 | 49 | Get-ChildItem -Path .\AppSphere\ScriptRunnerSvc\General 50 | 51 | # Alias für ein Execuatable anlegen 52 | Set-Item -Path Alias:np -Value C:\Windows\notepad.exe 53 | 54 | # Alias entfernen 55 | Clear-Item -Path Alias:np -Force 56 | 57 | # Datei mit Standardeditor öffnen 58 | Invoke-Item -Path Z:\git\ActionPacks\ActiveDirectory\Users\Add-ADUsersToGroups.ps1 59 | 60 | 61 | 62 | # Container Enabled CmdLets 63 | <# 64 | Get-ChildItem 65 | Copy-Item 66 | New-Item 67 | Remove-Item 68 | Rename-Item 69 | Convert-Path 70 | #> 71 | 72 | # In string konvertieren 73 | Convert-Path -Path 'HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\AppSphere ScriptRunnerSvc' 74 | Convert-Path -Path ~ 75 | Convert-Path -Path . 76 | 77 | # Get-Item vs Convert-Path 78 | (Convert-Path -Path .).GetType() 79 | 80 | (Get-Item -Path .).GetType() 81 | 82 | # Verzeichnis erstellen 83 | New-Item -Path C:\Test -ItemType Directory -Force 84 | 85 | # Verzeichnis löschen 86 | Remove-Item -Path C:\Test -Force 87 | 88 | # Existenz von Verzeichnissen, Datei, etc. prüfen 89 | Test-Path C:\Test 90 | 91 | # Umbenennen von Verzeichnissen, Dateien, etc. 92 | Rename-Item -Path C:\Test -NewName 'Test2' -Force 93 | 94 | # Verzeichnisse rekursiv kopieren 95 | Copy-Item -Path Z:\git -Destination C:\Test -Container -Recurse -Force 96 | Copy-Item -Path Z:\git -Destination C:\Test -Recurse -Force 97 | 98 | # Verzeichnisse rekursiv löschen 99 | Remove-Item -Path C:\Test\ActionPacks -Recurse -Force 100 | 101 | 102 | 103 | # Navigation-Enabled Providers 104 | <# 105 | Join-Path 106 | Move-Item 107 | #> 108 | 109 | # Verzeichnisse verschieben 110 | Move-Item -Path C:\Test\git -Destination C:\Test2 -Force 111 | 112 | # Content cmdlets 113 | <# 114 | Add-Content 115 | Clear-Content 116 | Get-Content 117 | Set-Content 118 | #> 119 | 120 | 121 | Get-Content -Path C:\Git\ScriptRunner\ActionPacks\README.md 122 | 123 | Get-Content -Path C:\Git\ScriptRunner\ActionPacks\README.md | Set-Content -Path C:\readme2.md 124 | 125 | Get-Content -Path C:\readme2.md 126 | 127 | Clear-Content -Path C:\readme2.md 128 | 129 | Set-Content -Path C:\readme2.md -Value 'Test 123' -Force 130 | 131 | 132 | Get-ItemProperty -Path 'HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\AppSphere ScriptRunnerSvc' -Name UninstallString 133 | 134 | 135 | 136 | 137 | 138 | $item = Get-Item -Path 'C:\TEMP\log.txt' 139 | $item | Get-ItemProperty -Name Length 140 | 141 | -------------------------------------------------------------------------------- /01_GettingStarted/PoshCrashCourse_2016-10-20.ps1: -------------------------------------------------------------------------------- 1 | <# 2 | PowerShell Crashkurs 3 | 4 | 20.10.2016 5 | 6 | 7 | Einführung 8 | ------------- 9 | 10 | - PowerShell 1.0 verfügbar ab Windows Server 2003/Windows XP/Windows Vista, erstmals mit Microsoft Exchange Server 2007 11 | 12 | - PowerShell ist Open Source 13 | https://github.com/PowerShell/PowerShell 14 | 15 | - PowerShell ist eine Cross Platform Technologie (PowerShell Core basierend auf .NET Core) 16 | Windows 17 | Client 18 | Server 19 | 20 | Linux 21 | Ubuntu 14.04 \ 16.04 22 | CentOs 7.1 23 | RHEL 7 24 | 25 | MAC OS X 26 | OS X 10.11 27 | 28 | Docker 29 | 30 | https://github.com/PowerShell/PowerShell/blob/master/docs/installation 31 | 32 | - Aktuelle Version 5.0 (Windows Management Framework 5.0), 33 | bereits Preview für 6.0 auf GitHub erhältlich 34 | s 35 | - Langfristig Ersatz für cmd und VBScript/Windows Scripting Host 36 | 37 | - Kombiniert den aus den UNIX-Shells bekannten Ansatz von Pipes und Filtern 38 | mit dem Paradigma der objektorientierten Programmierung 39 | 40 | - Verwendung unterschiedlichster Technologien 41 | - .NET Framework 42 | - ADSI 43 | - COM 44 | - WMI 45 | 46 | Dateiendungen 47 | -------------- 48 | .ps1 – Windows PowerShell Shell-Skript 49 | .ps1xml – Windows PowerShell Format- und Typdefinitionen 50 | .psc1 – Windows PowerShell Konsolendatei (exportierte Shell-Konfiguration) 51 | .psd1 – Windows PowerShell Datendatei 52 | .psm1 – Windows PowerShell Moduldatei 53 | 54 | 55 | #> 56 | 57 | # Powershell Version 58 | $PSVersionTable 59 | 60 | # PowerShell Host 61 | Get-Host 62 | 63 | # Vordefinierte Variablen 64 | Get-Variable 65 | 66 | 67 | <# 68 | Variablen 69 | ---------- 70 | 71 | implizite Deklaration -> Datentyp muss nicht explizit angegeben werden 72 | 73 | $i = '1' 74 | $i = $i + 1 75 | 76 | $j = 1 77 | $j = $j + 1 78 | 79 | $a = Get-Process 80 | $b 81 | 82 | Invoke-Command $b | Select ProcessName -Unique 83 | 84 | #> 85 | 86 | 87 | <# 88 | CmdLets und Module 89 | 90 | - CmdlLets 91 | Konvention für CmdLet Namen: Verb-Noun 92 | 93 | -Module 94 | Zusammenfassung von Cmdlets 95 | - DLL Module 96 | - Manifest Module 97 | - Script Module 98 | 99 | -Aliase 100 | 101 | - What you see is NOT, what you get! 102 | #> 103 | 104 | Get-Verb 105 | Get-Verb | Group-Object -Property Group 106 | Get-Verb | Where-Object -Property Group -EQ -Value Common 107 | 108 | Get-Alias 109 | 110 | 111 | Get-Module 112 | 113 | $env:PSModulePath 114 | 115 | 116 | <# 117 | Wie finde ich Hilfe zu Befehlen, Datentypen, etc 118 | 119 | Get-Help about_Core_Commands 120 | Get-Help about_Logical_Operators 121 | 122 | Get-Help about_Comparison_operators -ShowWindow 123 | Get-Help about_Operators -ShowWindow 124 | #> 125 | 126 | Get-Help 127 | Get-Member 128 | Get-Module 129 | Get-Command 130 | 131 | 132 | 133 | <# 134 | Pipelines 135 | ---------- 136 | 137 | Selektieren; Messen, Sortieren 138 | 139 | Get-Service DPS 140 | 141 | Select-Object 142 | Where-Object 143 | Sort-Object 144 | Measure-Object 145 | 146 | get-service | Where-Object { $_.Status -eq 'Stopped' } 147 | 148 | #> 149 | 150 | 151 | <# 152 | Strukturen 153 | ----------- 154 | Arrays 155 | Hashtables 156 | 157 | Methoden, Properties an Objekten 158 | 159 | #> 160 | 161 | <# 162 | 163 | Scripting, Funktionen, Verzweigungen, Schleifen 164 | 165 | if 166 | foreach 167 | while 168 | do 169 | for 170 | switch 171 | 172 | #> 173 | 174 | 175 | 176 | 177 | <# 178 | 179 | Find-Module 180 | 181 | Install-Module 182 | #> 183 | 184 | 185 | <# 186 | $rssUrl = "http://blogs.msdn.com/b/powershell/rss.aspx" 187 | $blog = [xml](New-Object System.Net.WebClient).DownloadString($rssUrl) 188 | $blog.rss.channel.item | select title -First 8 189 | #> -------------------------------------------------------------------------------- /02_ActiveDirectory/Crashkurs_AD.PS1: -------------------------------------------------------------------------------- 1 | # PowerShell Modul ActiveDirectory installieren 2 | 3 | 4 | # Client Betriebssysteme Windows 7 .. 10 5 | 6 | # Download remote server administration tools (Remoteserver-Verwaltungstools) 7 | 8 | # Windows 7 https://www.microsoft.com/de-de/download/details.aspx?id=7887 9 | # Windows 8 https://www.microsoft.com/de-de/download/details.aspx?id=28972 10 | # Windows 8.1 https://www.microsoft.com/de-de/download/details.aspx?id=39296 11 | # Windows 10 https://www.microsoft.com/de-DE/download/details.aspx?id=45520 12 | 13 | # Installieren der Remoteserver-Verwaltungstools 14 | # Systemsteuerung -> Programme und Features -> Windows Feature aktivieren 15 | # -> Aktivieren des Remoteserver-Verwaltungstools -> Rollenverwaltungstools -> AD DS- und AD LDS-Tools -> Active Directory-Modul für Windows PowerShell 16 | 17 | 18 | 19 | # Server Betriebssyteme Windows 2012 .. 2016 20 | 21 | Get-WindowsFeature | where -Property Name -Value *AD* -like 22 | 23 | Install-WindowsFeature -Name RSAT-AD-Powershell -Verbose 24 | 25 | # Test if available 26 | Get-Module -ListAvailable | Where-Object -Property Name -Value ActiveDirectory -EQ 27 | 28 | 29 | 30 | Get-Command -Module ActiveDirectory 31 | 32 | 33 | 34 | Get-ADDomain -Current LocalComputer 35 | Get-ADDomain -Current LoggedOnUser 36 | 37 | 38 | # Domain Controller abfragen 39 | Get-ADDomainController 40 | Get-ADDomainController -Discover -DomainName 'mydomain' 41 | Get-ADDomainController -Discover -DomainName 'mydomain' 42 | 43 | 44 | Get-ADUser -Filter '*' | Measure-Object 45 | 46 | Get-ADComputer -Filter '*' 47 | 48 | Get-ADGroup -Filter '*' | Measure-Object 49 | 50 | Get-ADGroup -SearchBase 'OU=ETT,OU=DE,DC=xxx,DC=xxx,DC=com' -Filter '(CN -like *User*)' 51 | 52 | 53 | 54 | 55 | # AD Filter HowTo 56 | Update-Help 57 | 58 | Get-Help about_ActiveDirectory_Filter 59 | 60 | 61 | Get-ADObject -Filter 'CN -like "*admin*"' 62 | 63 | Get-ADUser -Filter 'CN -like "*admin*"' 64 | 65 | Get-ADGroup -Filter 'CN -like "*admin*"' 66 | 67 | $adminGroup = Get-ADGroup -Filter 'CN -eq "TestGroup"' 68 | 69 | Get-ADGroupMember -Identity $adminGroup -Recursive | measure 70 | 71 | # Test AD Group Membership 72 | Function Test-ADGroupMember 73 | { 74 | 75 | [CmdletBinding()] 76 | Param ( 77 | [parameter(Mandatory = $true)] 78 | [string]$User, 79 | [parameter(Mandatory = $true)] 80 | [string]$Group 81 | ) 82 | 83 | Trap {Return "error"} 84 | 85 | if ( 86 | Get-ADUser -Filter "memberOf -RecursiveMatch '$((Get-ADGroup -Identity $Group).DistinguishedName)'" -SearchBase $((Get-ADUser -Identity $User).DistinguishedName) 87 | 88 | ) {Return 'true'} 89 | 90 | else {Return 'false'} 91 | 92 | } 93 | 94 | 95 | Test-ADGroupMember -User 'testuser' -Group 'testGroup' 96 | 97 | 98 | 99 | Function Unlock-MyADAccount 100 | { 101 | Param 102 | ( 103 | [parameter(Mandatory=$true)][string]$SAMAccount 104 | ) 105 | 106 | 107 | $Testees = @(Search-ADAccount -LockedOut) 108 | foreach($Test in $Testees) 109 | { 110 | If ($Test.SamAccountName -eq $SAMAccount) 111 | { 112 | Write-Host "Account is locked, unlock ongoing" 113 | Unlock-ADAccount -Identity $SAMAccount 114 | $Test2 = Search-ADAccount -LockedOut 115 | If ($Test2.SamAccountName -eq $SAMAccount) 116 | { 117 | Write-Host "Account is still locked, something went wrong!" 118 | } 119 | else 120 | { 121 | Write-Host "Account unlock successfully." 122 | } 123 | } 124 | } 125 | } 126 | 127 | 128 | $Firstname = 'Hans' 129 | $Lastname = 'Maier' 130 | 131 | $City = 'Ettlingen' 132 | $Company = 'AppSphere AG' 133 | $Department = 'DevOps' 134 | $StreetAddress = 'Ludwig-Erhard-Staße 1' 135 | $maildomain = '@appsphere.com' 136 | 137 | New-ADUser -Name "$Firstname $Lastname" -AccountPassword (ConvertTo-SecureString “1qay!QAY” -AsPlainText -Force) -AllowReversiblePasswordEncryption $FALSE -ChangePasswordAtLogon $TRUE -City $City -Company "$Company" -Department "$Department" -EmailAddress "$Firstname.$Lastname$maildomain" -Enabled $TRUE -GivenName "$Firstname" -PasswordNotRequired $FALSE -SamAccountName "$Firstname.$Lastname" -StreetAddress "$StreetAddress" -Surname "$Lastname" -userprincipalname (“$Firstname.$Lastname$userdomain") 138 | 139 | 140 | $user = Get-ADUser -Filter 'CN -EQ "Hans Maier"' 141 | Set-ADUser -Identity $user 142 | -------------------------------------------------------------------------------- /03_PsModules/MyFirstModule.ps1: -------------------------------------------------------------------------------- 1 | <# PowerShell Modules 2 | 3 | · Was ist ein PowerShell Modul? 4 | · Warum werden PowerShell Module verwendet? 5 | · Arten von PowerShell Modulen 6 | o Skript Module 7 | o Manifest Module 8 | o Binäre Module 9 | o Dynamic Module 10 | · Die PowerShellGallery und PowerShellGet 11 | · Eigene Module schreiben und verwenden 12 | · Live Coding-Beispiele 13 | 14 | #> 15 | <# What is a PowerShell module? 16 | A module is a package that contains Windows PowerShell commands, such as cmdlets, providers, 17 | functions, workflows, variables, and aliases. 18 | 19 | #> 20 | <# Why should I use PowerShell modules? 21 | You can easily decide whether to create a module by answering the following questions while writing a script: 22 | 23 | - Will the code I'm writing need to be used more than once? 24 | - Does this code essentially manage a single object or entity? 25 | - As I write this code, do I find that I'm breaking it apart into functions because it's getting too complex to be in a single script? 26 | - Do I need to share the code with others? 27 | 28 | If you answered yes to at least three of these four questions, it's a good bet you should be writing a module instead of a PS1 script. 29 | - See more at: http://www.tomsitpro.com/articles/powershell-modules,2-846.html#sthash.WfFAB85t.dpuf 30 | 31 | #> 32 | <# Module auto-loading 33 | Module Auto-Loading Beginning in Windows PowerShell 3.0, Windows PowerShell imports modules 34 | automatically the first time that you run any command in an installed module. You can now use 35 | the commands in a module without any set-up or profile configuration, so there's no need to 36 | manage modules after you install them on your computer. 37 | 38 | #> 39 | <# Explicite module loading 40 | - 41 | #> 42 | <# Different kinds of PowerShell modules 43 | 44 | - Script Modules -- PSM1 files that typically contain mostly functions, but can contain any valid PowerShell code. 45 | - Manifest Modules -- Script modules that contain a manifest. 46 | - Binary Modules -- Compiled DLL files typically not created by IT pros; these are usually left up to developers. 47 | - Dynamic Modules -- Modules that are never written to disk and are only available in memory. 48 | 49 | Each module serves specific purposes, but the module type you'll be creating will most likely be script modules. 50 | Script modules don't require knowledge of C# or the compilation process, and they are the most common, 51 | especially among IT pros. 52 | - See more at: http://www.tomsitpro.com/articles/powershell-modules,2-846.html#sthash.WfFAB85t.dpuf 53 | 54 | #> 55 | <# Get installed modules 56 | - Modules loaded to the current session 57 | 58 | Get-Module 59 | 60 | - List of all installed modules 61 | 62 | Get-Module -ListAvailable 63 | 64 | - PsModulePath 65 | 66 | $env:PsModulePath -split ';' 67 | 68 | #> 69 | <# How to install a module 70 | - Create a Modules directory for the current user if one does not exist. 71 | 72 | New-Item -Type Directory -Path $home\Documents\WindowsPowerShell\Modules -Force 73 | 74 | - Copy the entire module folder into the Modules directory. 75 | e.g. 76 | Copy-Item -Path C:\Git\ScriptRunner\Internal\PoShCrashCourse\03_PsModules\WriteLog -Destination $home\Documents\WindowsPowerShell\Modules 77 | 78 | Get-ChildItem -Path $home\Documents\WindowsPowerShell\Modules 79 | 80 | #> 81 | <# Install module from PsGallery 82 | 83 | Get-Command -Module PowerShellGet 84 | 85 | Find-Module -Name PSScriptAnalyzer 86 | 87 | Install-Module -Name PSScriptAnalyzer -Force 88 | 89 | Update-Module -Name Psscriptanalyzer 90 | 91 | 92 | #> 93 | <# How to import a module 94 | You might have to import a module or import a module file. Importing is required when a module is not installed 95 | in the locations specified by the PSModulePath environment variable ($env:PSModulePath), or the module 96 | consists of file, such as a .dll or .psm1 file, instead of typical module that is delivered as a folder. 97 | 98 | You might also choose to import a module so that you can use the parameters of the Import-Module command, 99 | such as the Prefix parameter, which adds a distinctive prefix to the noun names of all imported commands, 100 | or the NoClobber parameter, which prevents the module from adding commands that would hide or replace 101 | existing commands in the session. 102 | 103 | Import-Module C:\Git\ScriptRunner\Internal\PoShCrashCourse\03_PsModules\WriteLog\WriteLog.psm1 104 | 105 | #> 106 | <# How to remove a module from the current session 107 | 108 | Remove-Module -Name WriteLog 109 | 110 | #> 111 | <# How to import a module into every session 112 | 113 | Description | Path 114 | --- | --- 115 | Current user, Current Host | $Home\Documents\WindowsPowerShell\Profile.ps1 116 | Current User, All Hosts | $Home\Documents\Profile.ps1 117 | All Users, Current Host | $PsHome\Microsoft.PowerShell_profile.ps1 118 | All Users, All Hosts | $PsHome\Profile.ps1 119 | 120 | 121 | Get-Content -Path $profile 122 | 123 | Add-Content -Value 'Import-Module C:\Git\ScriptRunner\Internal\PoShCrashCourse\03_PsModules\WriteLog\WriteLog.psm1' -Path $profile 124 | 125 | #> 126 | <# How to create a script module based on a script file 127 | 128 | Code sample 129 | 130 | #> 131 | <# Manifest module 132 | 133 | New-ModuleManifest -Path C:\Git\ScriptRunner\Internal\PoShCrashCourse\03_PsModules\WriteLogMf\WriteLogMf.psd1 134 | 135 | Test-ModuleManifest -Path C:\Git\ScriptRunner\Internal\PoShCrashCourse\03_PsModules\WriteLogMf\WriteLogMf.psd1 136 | 137 | Code sample 138 | 139 | #> --------------------------------------------------------------------------------