So sieht das aus…
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
param( [String]$WorkingDir, [Switch]$DoNotDisplayConsole ) #region Script-Header mit Funktionserklärung und Versionsverlauf <###################################################################### # Firma : Sam # # Kunde : Kundenname # # Powershell-Script # # ##################################################################### # Name : demoscript.ps1 # # Funktion: Demo-Script für Sam's Blog # ####################################################################### # Versionsverlauf: # ####################################################################### # Ver. | Autor | Änderungen | Datum # ####################################################################### # 0.1 | . | Erst-Erstellung | 09.09.2016 # ######################################################################> #endregion #region Voreinstellungen !!! Nicht ändern !!! #region Startzeit $global:starttime = get-date #endregion #region Wechsle in Arbeitsverzeichnis, wenn gesetzt if($WorkingDir){ set-location $WorkingDir } #endregion #region Globale Variablen #region Erfassen des Scriptnamens zur Erstellung von Log- und Statusfile $global:ScriptName = ($MyInvocation.MyCommand.Name -replace ".ps1","") $global:ThisScript = ($MyInvocation.MyCommand.Definition) #endregion # ComputerName $global:computerName = ($env:COMPUTERNAME) #region Verzeichnispfade $global:PathWorkingDir = Get-Location $global:StringWorkingDir = $PathWorkingDir.ToString() + "\" $global:dirLog = $StringWorkingDir + "Log\" $global:dirRep = $StringWorkingDir + "Reports\" $ModuleToLoad = "SPE.Common" $dirModule = $StringWorkingDir + $ModuleToLoad + ".psd1" #endregion #endregion #region Laden des SPEModule Import-Module -Name "SPE.Common" #nur entkommentieren, wenn SPE.SharePoint installiert ist und mitgeladen werden soll. #Import-Module -Name "SPE.SharePoint" #endregion #region Laden der Config Get-SPEConfig -ScriptName $ScriptName #endregion #region ConsoleTitle mit Scriptnamen versehen $oldConsoleTitle = Set-SPEConsoleTitle -newTitle "Aktuelles Script: $ScriptName" #endregion #region Add SharePoint PowerShell Snapin if($global:UsingSharePoint){ if ( (Get-PSSnapin -Name Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue) -eq $null ) { Add-PSSnapin Microsoft.SharePoint.Powershell -ErrorAction SilentlyContinue } } #endregion #region ScriptStatus $scriptCorrId = $global:DefaultCorrID $global:CorrelationId = $scriptCorrId Write-SPELogMessage -message "Script has started." -level "High" Write-SPEReportMessage -level "High" -area "Script" -category "Started" -message "Script has started." -CorrelationId $scriptCorrId $global:scriptaborted = $false $global:foundErrors = $false #endregion #region Warnmeldungen #region Warnung, falls TestModus aktiviert ist if($TestModus -eq $true){ Write-SPELogMessage -message "!!!Achtung!!! TestModus aktiv !!! Es werden keine Daten gelöscht oder geschrieben !!!" Write-SPELogMessage -message "!!! Dient nur zum reinen Funktionstest !!!" } #endregion #region Warnung, falls Logging auf Console deaktiviert ist if(!$LogToConsole){ Write-Host "Logging auf Console ist deaktiviert." -ForegroundColor DarkYellow if($LogToLogFile){ Write-Host "Logging erfolgt in Logfile. `nLogfile wird am Ende des Scripts geöffnet.`n" -ForegroundColor DarkYellow } if($LogToULSFile){ Write-Host "Logging erfolgt in ULSfile. `nULSfile Bitte mit dem ULSViewer prüfen.`n" -ForegroundColor DarkYellow } } #endregion #endregion #region Prüfe Console auf Ausführung "als Administrator" if($global:RunAsAdmin) { $currentPrincipal = New-Object Security.Principal.WindowsPrincipal([Security.Principal.windowsIdentity]::GetCurrent()) if(!($currentPrincipal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator))){ $argumentList = "$ThisScript -workingDir $StringWorkingDir" start-process powershell -ArgumentList $argumentList -Verb RunAs Wait-SPELoop -text "Das Script muss mit Administrator-Berechtigungen ausgeführt werden und wurde daher in einem neuen Konsolen-Fenster neu gestartet. Dieses Fenster wird geschlossen." -time 10 Stop-Process $PID } } #endregion #endregion Exit-SPEOnCtrlC while($true) { #region Hauptprogramm !!! Hier kann gearbeitet werden !!! #endregion break } Trap [ExecutionEngineException]{ Write-SPELogMessage -level High -CorrelationId $scriptCorrId -message "Script terminated by Ctrl-C." $global:scriptaborted = $true #region Auszuführender Code nach manuellem Abbruch durch Ctrl-C if(!$DoNotDisplayConsole){ Show-SPETextLine -text "Script wurde durch Ctrl-C abgebrochen!" -fgColor Red -bgColor White $resetConsoleTitle = Set-SPEConsoleTitle -newTitle $oldConsoleTitle Wait-SPEForKey } continue #endregion } #region End of Script and opening of the script's logfile if($global:scriptaborted) { Write-SPEReportMessage -level "Critical" -area "Script" -category "Aborted" -message "Script has been aborted. Check Log(s)" -CorrelationId $scriptCorrId Write-SPELogMessage -level "Critical" -area "Script" -category "Aborted" -message "Script has been aborted. Check Log(s)" -CorrelationId $scriptCorrId } elseif($global:foundErrors){ Write-SPEReportMessage -level "High" -area "Script" -category "Stopped" -message "Script has finished with errors. Check Log(s)" -CorrelationId $scriptCorrId Write-SPELogMessage -level "High" -area "Script" -category "Stopped" -message "Script has finished with errors. Check Log(s)" -CorrelationId $scriptCorrId } else { Write-SPEReportMessage -message "Script has successfully finished without any error." -level "High" -area "Script" -category "Stopped" -CorrelationId $scriptCorrId Write-SPELogMessage -message "Script has successfully finished without any error." -level "High" -area "Script" -category "Stopped" -CorrelationId $scriptCorrId } if($TestMode){ Wait-SPEForKey } if($LogToLogFile){ notepad.exe $PathToLogfile } #endregion #EndOfFile |