-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The size and complexity becoming increasingly harder to manage so I decided to move the the Pester framework (https://github.com/pester/Pester/wiki) This will allow me to make aoutmated tests and make sure that I have a working version when building the module. you might wanna check it out yourself :)
- Loading branch information
Ioan Popovici
committed
Feb 1, 2018
1 parent
5acb6f6
commit 7a40dcb
Showing
37 changed files
with
3,842 additions
and
3,714 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Output/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# PSWmiToolkit Help |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,265 @@ | ||
#region Function Write-Log | ||
Function Write-Log { | ||
<# | ||
.SYNOPSIS | ||
Write messages to a log file in CMTrace.exe compatible format or Legacy text file format. | ||
.DESCRIPTION | ||
Write messages to a log file in CMTrace.exe compatible format or Legacy text file format and optionally display in the console. | ||
.PARAMETER Message | ||
The message to write to the log file or output to the console. | ||
.PARAMETER Severity | ||
Defines message type. When writing to console or CMTrace.exe log format, it allows highlighting of message type. | ||
Options: 1 = Information (default), 2 = Warning (highlighted in yellow), 3 = Error (highlighted in red) | ||
.PARAMETER Source | ||
The source of the message being logged. | ||
.PARAMETER ScriptSection | ||
The heading for the portion of the script that is being executed. Default is: $script:installPhase. | ||
.PARAMETER LogType | ||
Choose whether to write a CMTrace.exe compatible log file or a Legacy text log file. | ||
.PARAMETER LogFileDirectory | ||
Set the directory where the log file will be saved. | ||
Default is %WINDIR%\Logs\WmiToolkit. | ||
.PARAMETER LogFileName | ||
Set the name of the log file. | ||
.PARAMETER MaxLogFileSizeMB | ||
Maximum file size limit for log file in megabytes (MB). Default is 10 MB. | ||
.PARAMETER WriteHost | ||
Write the log message to the console. | ||
.PARAMETER ContinueOnError | ||
Suppress writing log message to console on failure to write message to log file. Default is: $true. | ||
.PARAMETER PassThru | ||
Return the message that was passed to the function | ||
.PARAMETER DebugMessage | ||
Specifies that the message is a debug message. Debug messages only get logged if -LogDebugMessage is set to $true. | ||
.PARAMETER LogDebugMessage | ||
Debug messages only get logged if this parameter is set to $true in the config XML file. | ||
.EXAMPLE | ||
Write-Log -Message "Installing patch MS15-031" -Source 'Add-Patch' -LogType 'CMTrace' | ||
.EXAMPLE | ||
Write-Log -Message "Script is running on Windows 8" -Source 'Test-ValidOS' -LogType 'Legacy' | ||
.NOTES | ||
.LINK | ||
https://psappdeploytoolkit.com | ||
#> | ||
[CmdletBinding()] | ||
Param ( | ||
[Parameter(Mandatory=$true,Position=0,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)] | ||
[AllowEmptyCollection()] | ||
[Alias('Text')] | ||
[string[]]$Message, | ||
[Parameter(Mandatory=$false,Position=1)] | ||
[ValidateRange(1,3)] | ||
[int16]$Severity = 1, | ||
[Parameter(Mandatory=$false,Position=2)] | ||
[ValidateNotNull()] | ||
[string]$Source = '', | ||
[Parameter(Mandatory=$false,Position=3)] | ||
[ValidateNotNullorEmpty()] | ||
[string]$ScriptSection = '', | ||
[Parameter(Mandatory=$false,Position=4)] | ||
[ValidateSet('CMTrace','Legacy')] | ||
[string]$LogType = 'Legacy', | ||
[Parameter(Mandatory=$false,Position=5)] | ||
[ValidateNotNullorEmpty()] | ||
[string]$LogFileDirectory = $(Join-Path -Path $Env:windir -ChildPath "\Logs\WmiToolkit"), | ||
[Parameter(Mandatory=$false,Position=6)] | ||
[ValidateNotNullorEmpty()] | ||
[string]$LogFileName = 'WmiTool.log', | ||
[Parameter(Mandatory=$false,Position=7)] | ||
[ValidateNotNullorEmpty()] | ||
[decimal]$MaxLogFileSizeMB = '5', | ||
[Parameter(Mandatory=$false,Position=8)] | ||
[ValidateNotNullorEmpty()] | ||
[boolean]$WriteHost = $true, | ||
[Parameter(Mandatory=$false,Position=9)] | ||
[ValidateNotNullorEmpty()] | ||
[boolean]$ContinueOnError = $true, | ||
[Parameter(Mandatory=$false,Position=10)] | ||
[switch]$PassThru = $false, | ||
[Parameter(Mandatory=$false,Position=11)] | ||
[switch]$DebugMessage = $false, | ||
[Parameter(Mandatory=$false,Position=12)] | ||
[boolean]$LogDebugMessage = $true | ||
) | ||
|
||
Begin { | ||
## Get the name of this function | ||
[string]${CmdletName} = $PSCmdlet.MyInvocation.MyCommand.Name | ||
|
||
## Logging Variables | ||
# Log file date/time | ||
[string]$LogTime = (Get-Date -Format 'HH:mm:ss.fff').ToString() | ||
[string]$LogDate = (Get-Date -Format 'MM-dd-yyyy').ToString() | ||
If (-not (Test-Path -LiteralPath 'variable:LogTimeZoneBias')) { [int32]$script:LogTimeZoneBias = [timezone]::CurrentTimeZone.GetUtcOffset([datetime]::Now).TotalMinutes } | ||
[string]$LogTimePlusBias = $LogTime + $script:LogTimeZoneBias | ||
# Initialize variables | ||
[boolean]$ExitLoggingFunction = $false | ||
If (-not (Test-Path -LiteralPath 'variable:DisableLogging')) { $DisableLogging = $false } | ||
# Check if the script section is defined | ||
[boolean]$ScriptSectionDefined = [boolean](-not [string]::IsNullOrEmpty($ScriptSection)) | ||
# Get the file name of the source script | ||
Try { | ||
If ($script:MyInvocation.Value.ScriptName) { | ||
[string]$ScriptSource = Split-Path -Path $script:MyInvocation.Value.ScriptName -Leaf -ErrorAction 'Stop' | ||
} | ||
Else { | ||
[string]$ScriptSource = Split-Path -Path $script:MyInvocation.MyCommand.Definition -Leaf -ErrorAction 'Stop' | ||
} | ||
} | ||
Catch { | ||
$ScriptSource = '' | ||
} | ||
|
||
## Create script block for generating CMTrace.exe compatible log entry | ||
[scriptblock]$CMTraceLogString = { | ||
Param ( | ||
[string]$lMessage, | ||
[string]$lSource, | ||
[int16]$lSeverity | ||
) | ||
"<![LOG[$lMessage]LOG]!>" + "<time=`"$LogTimePlusBias`" " + "date=`"$LogDate`" " + "component=`"$lSource`" " + "context=`"$([Security.Principal.WindowsIdentity]::GetCurrent().Name)`" " + "type=`"$lSeverity`" " + "thread=`"$PID`" " + "file=`"$ScriptSource`">" | ||
} | ||
|
||
## Create script block for writing log entry to the console | ||
[scriptblock]$WriteLogLineToHost = { | ||
Param ( | ||
[string]$lTextLogLine, | ||
[int16]$lSeverity | ||
) | ||
If ($WriteHost) { | ||
# Only output using color options if running in a host which supports colors. | ||
If ($Host.UI.RawUI.ForegroundColor) { | ||
Switch ($lSeverity) { | ||
3 { Write-Host -Object $lTextLogLine -ForegroundColor 'Red' -BackgroundColor 'Black' } | ||
2 { Write-Host -Object $lTextLogLine -ForegroundColor 'Yellow' -BackgroundColor 'Black' } | ||
1 { Write-Host -Object $lTextLogLine } | ||
} | ||
} | ||
# If executing "powershell.exe -File <filename>.ps1 > log.txt", then all the Write-Host calls are converted to Write-Output calls so that they are included in the text log. | ||
Else { | ||
Write-Output -InputObject $lTextLogLine | ||
} | ||
} | ||
} | ||
|
||
## Exit function if it is a debug message and logging debug messages is not enabled in the config XML file | ||
If (($DebugMessage) -and (-not $LogDebugMessage)) { [boolean]$ExitLoggingFunction = $true; Return } | ||
## Exit function if logging to file is disabled and logging to console host is disabled | ||
If (($DisableLogging) -and (-not $WriteHost)) { [boolean]$ExitLoggingFunction = $true; Return } | ||
## Exit Begin block if logging is disabled | ||
If ($DisableLogging) { Return } | ||
## Exit function function if it is an [Initialization] message and the toolkit has been relaunched | ||
If ($ScriptSection -eq 'Initialization') { [boolean]$ExitLoggingFunction = $true; Return } | ||
|
||
## Create the directory where the log file will be saved | ||
If (-not (Test-Path -LiteralPath $LogFileDirectory -PathType 'Container')) { | ||
Try { | ||
$null = New-Item -Path $LogFileDirectory -Type 'Directory' -Force -ErrorAction 'Stop' | ||
} | ||
Catch { | ||
[boolean]$ExitLoggingFunction = $true | ||
# If error creating directory, write message to console | ||
If (-not $ContinueOnError) { | ||
Write-Host -Object "[$LogDate $LogTime] [${CmdletName}] $ScriptSection :: Failed to create the log directory [$LogFileDirectory]. `n$(Resolve-Error)" -ForegroundColor 'Red' | ||
} | ||
Return | ||
} | ||
} | ||
|
||
## Assemble the fully qualified path to the log file | ||
[string]$LogFilePath = Join-Path -Path $LogFileDirectory -ChildPath $LogFileName | ||
} | ||
Process { | ||
## Exit function if logging is disabled | ||
If ($ExitLoggingFunction) { Return } | ||
|
||
ForEach ($Msg in $Message) { | ||
## If the message is not $null or empty, create the log entry for the different logging methods | ||
[string]$CMTraceMsg = '' | ||
[string]$ConsoleLogLine = '' | ||
[string]$LegacyTextLogLine = '' | ||
If ($Msg) { | ||
# Create the CMTrace log message | ||
If ($ScriptSectionDefined) { [string]$CMTraceMsg = "[$ScriptSection] :: $Msg" } | ||
|
||
# Create a Console and Legacy "text" log entry | ||
[string]$LegacyMsg = "[$LogDate $LogTime]" | ||
If ($ScriptSectionDefined) { [string]$LegacyMsg += " [$ScriptSection]" } | ||
If ($Source) { | ||
[string]$ConsoleLogLine = "$LegacyMsg [$Source] :: $Msg" | ||
Switch ($Severity) { | ||
3 { [string]$LegacyTextLogLine = "$LegacyMsg [$Source] [Error] :: $Msg" } | ||
2 { [string]$LegacyTextLogLine = "$LegacyMsg [$Source] [Warning] :: $Msg" } | ||
1 { [string]$LegacyTextLogLine = "$LegacyMsg [$Source] [Info] :: $Msg" } | ||
} | ||
} | ||
Else { | ||
[string]$ConsoleLogLine = "$LegacyMsg :: $Msg" | ||
Switch ($Severity) { | ||
3 { [string]$LegacyTextLogLine = "$LegacyMsg [Error] :: $Msg" } | ||
2 { [string]$LegacyTextLogLine = "$LegacyMsg [Warning] :: $Msg" } | ||
1 { [string]$LegacyTextLogLine = "$LegacyMsg [Info] :: $Msg" } | ||
} | ||
} | ||
} | ||
|
||
## Execute script block to create the CMTrace.exe compatible log entry | ||
[string]$CMTraceLogLine = & $CMTraceLogString -lMessage $CMTraceMsg -lSource $Source -lSeverity $Severity | ||
|
||
## Choose which log type to write to file | ||
If ($LogType -ieq 'CMTrace') { | ||
[string]$LogLine = $CMTraceLogLine | ||
} | ||
Else { | ||
[string]$LogLine = $LegacyTextLogLine | ||
} | ||
|
||
## Write the log entry to the log file if logging is not currently disabled | ||
If (-not $DisableLogging) { | ||
Try { | ||
$LogLine | Out-File -FilePath $LogFilePath -Append -NoClobber -Force -Encoding 'UTF8' -ErrorAction 'Stop' | ||
} | ||
Catch { | ||
If (-not $ContinueOnError) { | ||
Write-Host -Object "[$LogDate $LogTime] [$ScriptSection] [${CmdletName}] :: Failed to write message [$Msg] to the log file [$LogFilePath]. `n$(Resolve-Error)" -ForegroundColor 'Red' | ||
} | ||
} | ||
} | ||
|
||
## Execute script block to write the log entry to the console if $WriteHost is $true | ||
& $WriteLogLineToHost -lTextLogLine $ConsoleLogLine -lSeverity $Severity | ||
} | ||
} | ||
End { | ||
## Archive log file if size is greater than $MaxLogFileSizeMB and $MaxLogFileSizeMB > 0 | ||
Try { | ||
If ((-not $ExitLoggingFunction) -and (-not $DisableLogging)) { | ||
[IO.FileInfo]$LogFile = Get-ChildItem -LiteralPath $LogFilePath -ErrorAction 'Stop' | ||
[decimal]$LogFileSizeMB = $LogFile.Length/1MB | ||
If (($LogFileSizeMB -gt $MaxLogFileSizeMB) -and ($MaxLogFileSizeMB -gt 0)) { | ||
## Change the file extension to "lo_" | ||
[string]$ArchivedOutLogFile = [IO.Path]::ChangeExtension($LogFilePath, 'lo_') | ||
[hashtable]$ArchiveLogParams = @{ ScriptSection = $ScriptSection; Source = ${CmdletName}; Severity = 2; LogFileDirectory = $LogFileDirectory; LogFileName = $LogFileName; LogType = $LogType; MaxLogFileSizeMB = 0; WriteHost = $WriteHost; ContinueOnError = $ContinueOnError; PassThru = $false } | ||
|
||
## Log message about archiving the log file | ||
$ArchiveLogMessage = "Maximum log file size [$MaxLogFileSizeMB MB] reached. Rename log file to [$ArchivedOutLogFile]." | ||
Write-Log -Message $ArchiveLogMessage @ArchiveLogParams | ||
|
||
## Archive existing log file from <filename>.log to <filename>.lo_. Overwrites any existing <filename>.lo_ file. This is the same method SCCM uses for log files. | ||
Move-Item -LiteralPath $LogFilePath -Destination $ArchivedOutLogFile -Force -ErrorAction 'Stop' | ||
|
||
## Start new log file and Log message about archiving the old log file | ||
$NewLogMessage = "Previous log file was renamed to [$ArchivedOutLogFile] because maximum log file size of [$MaxLogFileSizeMB MB] was reached." | ||
Write-Log -Message $NewLogMessage @ArchiveLogParams | ||
} | ||
} | ||
} | ||
Catch { | ||
## If renaming of file fails, script will continue writing to log file even if size goes over the max file size | ||
} | ||
Finally { | ||
If ($PassThru) { Write-Output -InputObject $Message } | ||
} | ||
} | ||
} | ||
#endregion |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
#region Function Get-WmiNameSpaceRecursive | ||
Function Get-WmiNamespaceRecursive { | ||
<# | ||
.SYNOPSIS | ||
This function is used to get wmi namespaces recursively. | ||
.DESCRIPTION | ||
This function is used to get wmi namespaces recursively and return a custom object. | ||
As this is a recursive function it will run multiple times so you might want to assign it to a variable for sorting. | ||
You also might want to disable logging when running this function. | ||
.PARAMETER NamespaceRoot | ||
Specifies the root namespace path from which to start searching. | ||
.EXAMPLE | ||
Get-WmiNamespaceRecursive -NameSpace 'ROOT\SCCM' | ||
.INPUTS | ||
None. | ||
.OUTPUTS | ||
None. | ||
.NOTES | ||
This is a private module function and should not typically be called directly. | ||
.LINK | ||
https://sccm-zone.com | ||
.LINK | ||
https://github.com/JhonnyTerminus/SCCM | ||
.COMPONENT | ||
WMI | ||
.FUNCTIONALITY | ||
WMI Management | ||
#> | ||
[CmdletBinding()] | ||
Param ( | ||
[Parameter(Mandatory=$true,Position=0)] | ||
[ValidateNotNullorEmpty()] | ||
[string]$NamespaceRoot | ||
) | ||
|
||
Begin { | ||
## Initialize/Reset resutl object | ||
[PSCustomObject]$GetNamespaceRecursive = @() | ||
} | ||
Process { | ||
Try { | ||
|
||
## Get all namespaces in the current root namespace | ||
$Namespaces = Get-WmiNameSpace -Namespace "$NamespaceRoot" -List -ErrorAction 'SilentlyContinue' | ||
|
||
## Search in the current namespace for other namespaces | ||
ForEach ($Namespace in $Namespaces) { | ||
|
||
# Assemble the result object | ||
$GetNamespaceRecursive += [PsCustomObject]@{ | ||
Name = $Namespace.Name | ||
Path = $Namespace.Path | ||
FullName = $Namespace.FullName | ||
} | ||
|
||
# Call the function again for the next namespace | ||
Get-WmiNamespaceRecursive -Namespace $Namespace.FullName | ||
} | ||
} | ||
Catch { | ||
Write-Log -Message "Failed to retrieve wmi namespace [$NamespaceRoot] recursively. `n$(Resolve-Error)" -Severity 3 -Source ${CmdletName} | ||
Break | ||
} | ||
} | ||
End { | ||
Write-Output -InputObject $GetNamespaceRecursive | ||
} | ||
} | ||
#endregion |
Oops, something went wrong.