diff --git a/Internal/Function Write-Log.ps1 b/Internal/Function Write-Log.ps1 index e242361..4a77097 100644 --- a/Internal/Function Write-Log.ps1 +++ b/Internal/Function Write-Log.ps1 @@ -61,10 +61,10 @@ Function Write-Log { [string]$LogType = 'Legacy', [Parameter(Mandatory=$false,Position=5)] [ValidateNotNullorEmpty()] - [string]$LogFileDirectory = $(Join-Path -Path $Env:windir -ChildPath "\Logs\WmiToolkit"), + [string]$LogFileDirectory = $(Join-Path -Path $Env:windir -ChildPath '\Logs\PSWmiToolKit'), [Parameter(Mandatory=$false,Position=6)] [ValidateNotNullorEmpty()] - [string]$LogFileName = 'WmiTool.log', + [string]$LogFileName = 'PSWmiToolKit.log', [Parameter(Mandatory=$false,Position=7)] [ValidateNotNullorEmpty()] [decimal]$MaxLogFileSizeMB = '5', @@ -79,7 +79,7 @@ Function Write-Log { [Parameter(Mandatory=$false,Position=11)] [switch]$DebugMessage = $false, [Parameter(Mandatory=$false,Position=12)] - [boolean]$LogDebugMessage = $true + [boolean]$LogDebugMessage = $false ) Begin { diff --git a/Internal/Get-WmiNamespaceRecursive.ps1 b/Internal/Get-WmiNamespaceRecursive.ps1 index e14062f..093aa24 100644 --- a/Internal/Get-WmiNamespaceRecursive.ps1 +++ b/Internal/Get-WmiNamespaceRecursive.ps1 @@ -1,26 +1,32 @@ -#region Function Get-WmiNameSpaceRecursive +#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. + This function is used to get wmi namespaces recursively and returns a custom object. +.PARAMETER Namespace + Specifies the root namespace(s) path(s) to search. Cand be piped. +.EXAMPLE + C:\PS> $Result = Get-WmiNamespaceRecursive -NameSpace 'ROOT\SCCM' .EXAMPLE - Get-WmiNamespaceRecursive -NameSpace 'ROOT\SCCM' + C:\PS> $Result = 'ROOT\SCCM', 'ROOT\Appv' | Get-WmiNamespaceRecursive .INPUTS - None. + System.String[]. .OUTPUTS - None. + System.Management.Automation.PSCustomObject. + 'Name' + 'Path' + 'FullName' .NOTES - This is a private module function and should not typically be called directly. + 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. + + This is an internal module function and should not typically be called directly. .LINK - https://sccm-zone.com + https://github.com/JhonnyTerminus/PSWmiToolKit .LINK - https://github.com/JhonnyTerminus/SCCM + https://sccm-zone.com .COMPONENT WMI .FUNCTIONALITY @@ -28,37 +34,38 @@ Function Get-WmiNamespaceRecursive { #> [CmdletBinding()] Param ( - [Parameter(Mandatory=$true,Position=0)] + [Parameter(Mandatory=$true,ValueFromPipeline,Position=0)] [ValidateNotNullorEmpty()] - [string]$NamespaceRoot + [string[]]$Namespace ) Begin { - ## Initialize/Reset resutl object + ## Initialize/Reset result object [PSCustomObject]$GetNamespaceRecursive = @() } Process { Try { ## Get all namespaces in the current root namespace - $Namespaces = Get-WmiNameSpace -Namespace "$NamespaceRoot" -List -ErrorAction 'SilentlyContinue' + $Namespaces = Get-WmiNamespace -Namespace $Namespace -List ## Search in the current namespace for other namespaces - ForEach ($Namespace in $Namespaces) { + If ($Namespaces) { + $Namespaces | ForEach-Object { + # Assemble the result object + $GetNamespaceRecursive += [PsCustomObject]@{ + Name = $_.Name + Path = $_.Path + FullName = $_.FullName + } - # 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 $_.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} + Write-Log -Message "Failed to retrieve wmi namespace [$Namespace] recursively. `n$(Resolve-Error)" -Severity 3 -Source ${CmdletName} Break } } diff --git a/Public/Copy-WmiClass.ps1 b/Public/Copy-WmiClass.ps1 index 545067d..deed24f 100644 --- a/Public/Copy-WmiClass.ps1 +++ b/Public/Copy-WmiClass.ps1 @@ -57,13 +57,13 @@ Function Copy-WmiClass { $WmiClass = (Get-WmiClass -Namespace $NamespaceSource -ErrorAction 'Stop').CimClassName ## Check if the destination namespace exists - $NamespaceDestinationTest = Get-WmiNameSpace -Namespace $NamespaceDestination -ErrorAction 'SilentlyContinue' + $NamespaceDestinationTest = Get-WmiNamespace -Namespace $NamespaceDestination -ErrorAction 'SilentlyContinue' ## Create destination namespace if specified If ((-not $NamespaceDestinationTest) -and $CreateDestination) { # Create destination namespace - New-WmiNameSpace -Namespace $NamespaceDestination -CreateSubTree -ErrorAction 'Stop' + New-WmiNamespace -Namespace $NamespaceDestination -CreateSubTree -ErrorAction 'Stop' } ElseIf (-not $NamespaceDestinationTest) { $DestinationNamespaceNotFoundErr = "Destination namespace [$NamespaceDestination] not found. Use -CreateDestination switch to create the destination automatically." diff --git a/Public/Copy-WmiNamespace.ps1 b/Public/Copy-WmiNamespace.ps1 index babd56c..bacec5a 100644 --- a/Public/Copy-WmiNamespace.ps1 +++ b/Public/Copy-WmiNamespace.ps1 @@ -44,10 +44,10 @@ Function Copy-WmiNamespace { Try { ## Check if the source namespace exists - $null = Get-WmiNameSpace -Namespace $NamespaceSource -ErrorAction 'Stop' + $null = Get-WmiNamespace -Namespace $NamespaceSource -ErrorAction 'Stop' ## Get source namespace tree - $NamespaceSourceTree = Get-WmiNameSpace -Namespace $NamespaceSource -Recurse -ErrorAction 'SilentlyContinue' + $NamespaceSourceTree = Get-WmiNamespace -Namespace $NamespaceSource -Recurse -ErrorAction 'SilentlyContinue' ## Check if we need to copy root namespace classes $ClassNameSourceRoot = Get-WmiClass -Namespace $NamespaceSource -ErrorAction 'SilentlyContinue' @@ -71,11 +71,11 @@ Function Copy-WmiNamespace { [string]$NamespaceDestinationPath = $NamespaceSourcePath -ireplace [regex]::Escape($NamespaceSource), $NamespaceDestination # Check if the destination namespace exists - $NamespaceDestinationTest = Get-WmiNameSpace -Namespace $NamespaceDestinationPath -ErrorAction 'SilentlyContinue' + $NamespaceDestinationTest = Get-WmiNamespace -Namespace $NamespaceDestinationPath -ErrorAction 'SilentlyContinue' # If the namespace already exists in the destination and the -Force switch is specified remove the namespace, otherwise set the $ShouldCopy variable to $false If ($NamespaceDestinationTest -and $Force) { - $null = Remove-WmiNameSpace -Namespace $NamespaceDestinationPath -Force + $null = Remove-WmiNamespace -Namespace $NamespaceDestinationPath -Force } ElseIf ($NamespaceDestinationTest) { $ShouldCopy = $false @@ -85,7 +85,7 @@ Function Copy-WmiNamespace { If ($ShouldCopy) { # Create the destination namespace - $CopyNamespace = New-WmiNameSpace -Namespace $NamespaceDestinationPath -CreateSubTree -ErrorAction 'Stop' + $CopyNamespace = New-WmiNamespace -Namespace $NamespaceDestinationPath -CreateSubTree -ErrorAction 'Stop' # Get current source namespace classes $ClassNameSource = Get-WmiClass -Namespace $NamespaceSourcePath -ErrorAction 'SilentlyContinue' diff --git a/Public/Get-WmiClass.ps1 b/Public/Get-WmiClass.ps1 index 9a0ad37..a800bda 100644 --- a/Public/Get-WmiClass.ps1 +++ b/Public/Get-WmiClass.ps1 @@ -59,7 +59,7 @@ Function Get-WmiClass { Try { ## Check if the namespace exists - $NamespaceTest = Get-WmiNameSpace -Namespace $Namespace -ErrorAction 'SilentlyContinue' + $NamespaceTest = Get-WmiNamespace -Namespace $Namespace -ErrorAction 'SilentlyContinue' If (-not $NamespaceTest) { $NamespaceNotFoundErr = "Namespace [$Namespace] not found." Write-Log -Message $NamespaceNotFoundErr -Severity 2 -Source ${CmdletName} -DebugMessage diff --git a/Public/Get-WmiNameSpace.ps1 b/Public/Get-WmiNameSpace.ps1 index 99d707b..bc64fa7 100644 --- a/Public/Get-WmiNameSpace.ps1 +++ b/Public/Get-WmiNameSpace.ps1 @@ -1,26 +1,26 @@ -#region Function Get-WmiNameSpace -Function Get-WmiNameSpace { +#region Function Get-WmiNamespace +Function Get-WmiNamespace { <# .SYNOPSIS This function is used to get WMI namespace information. .DESCRIPTION This function is used to get the details of one or more WMI namespaces. .PARAMETER Namespace - Specifies the namespace path. Supports wildcards only when not using the -Recurse or -List switch. Can be piped. + Specifies the namespace(s) path(s). Supports wildcards only when not using the -Recurse or -List switch. Can be piped. .PARAMETER List This switch is used to list all namespaces in the specified path. Cannot be used in conjunction with the -Recurse switch. .PARAMETER Recurse This switch is used to get the whole WMI namespace tree recursively. Cannot be used in conjunction with the -List switch. .EXAMPLE - C:\PS> Get-WmiNameSpace -NameSpace 'ROOT\SCCM' + C:\PS> Get-WmiNamespace -NameSpace 'ROOT\SCCM' .EXAMPLE - C:\PS> Get-WmiNameSpace -NameSpace 'ROOT\*CM' + C:\PS> Get-WmiNamespace -NameSpace 'ROOT\*CM' .EXAMPLE - C:\PS> Get-WmiNameSpace -NameSpace 'ROOT' -List + C:\PS> Get-WmiNamespace -NameSpace 'ROOT' -List .EXAMPLE - C:\PS> Get-WmiNameSpace -NameSpace 'ROOT' -Recurse + C:\PS> Get-WmiNamespace -NameSpace 'ROOT' -Recurse .EXAMPLE - C:\PS> 'Root\SCCM', 'Root\SC*' | Get-WmiNameSpace + C:\PS> 'Root\SCCM', 'Root\SC*' | Get-WmiNamespace .INPUTS System.String[]. .OUTPUTS @@ -48,14 +48,14 @@ Function Get-WmiNameSpace { [Parameter(Mandatory=$false,Position=1)] [ValidateNotNullorEmpty()] [ValidateScript({ - If ($Namespace -match '\*') { Throw "Wildcards are not supported with this switch." } + If ($Namespace -match '\*') { Throw 'Wildcards are not supported with this switch.' } Return $true })] [switch]$List = $false, [Parameter(Mandatory=$false,Position=2)] [ValidateNotNullorEmpty()] [ValidateScript({ - If ($Namespace -match '\*') { Throw "Wildcards are not supported with this switch." } + If ($Namespace -match '\*') { Throw 'Wildcards are not supported with this switch.' } Return $true })] [switch]$Recurse = $false @@ -75,19 +75,19 @@ Function Get-WmiNameSpace { ## Get namespace tree recursively if specified, otherwise just get the current namespace If ($Recurse) { - # Call Get-NamespacesRecursive internal function - $GetNamespace = Get-WmiNamespaceRecursive -NamespaceRoot $Namespace -ErrorAction 'SilentlyContinue' | Sort-Object -Property Path + # Call Get-WmiNamespaceRecursive internal function + $GetNamespace = Get-WmiNamespaceRecursive -Namespace $Namespace -ErrorAction 'SilentlyContinue' | Sort-Object -Property Path } Else { ## If namespace is 'ROOT' or -List is specified get namespace else get Parent\Leaf namespace If ($List -or ($Namespace -eq 'ROOT')) { - $WmiNamespace = Get-CimInstance -Namespace $Namespace -ClassName '__Namespace' -ErrorAction 'SilentlyContinue' -ErrorVariable Err + $WmiNamespace = Get-CimInstance -Namespace $([string]$Namespace) -ClassName '__Namespace' -ErrorAction 'SilentlyContinue' -ErrorVariable Err } Else { # Set namespace path and name - $NamespaceParent = $(Split-Path -Path $Namespace -Parent) - $NamespaceLeaf = $(Split-Path -Path $Namespace -Leaf) + [string]$NamespaceParent = $(Split-Path -Path $Namespace -Parent) + [string]$NamespaceLeaf = $(Split-Path -Path $Namespace -Leaf) # Get namespace $WmiNamespace = Get-CimInstance -Namespace $NamespaceParent -ClassName '__Namespace' -ErrorAction 'SilentlyContinue' -ErrorVariable Err | Where-Object { $_.Name -like $NamespaceLeaf } } diff --git a/Public/New-WmiClass.ps1 b/Public/New-WmiClass.ps1 index 8e8ddb1..76006ea 100644 --- a/Public/New-WmiClass.ps1 +++ b/Public/New-WmiClass.ps1 @@ -66,11 +66,11 @@ Function New-WmiClass { $ClassTest = Get-WmiClass -Namespace $Namespace -ClassName $ClassName -ErrorAction 'SilentlyContinue' ## Check if the namespace exists - $NamespaceTest = Get-WmiNameSpace -Namespace $Namespace -ErrorAction 'SilentlyContinue' + $NamespaceTest = Get-WmiNamespace -Namespace $Namespace -ErrorAction 'SilentlyContinue' ## Create destination namespace if specified, otherwise throw error if -ErrorAction 'Stop' is specified If ((-not $NamespaceTest) -and $CreateDestination) { - $null = New-WmiNameSpace $Namespace -CreateSubTree -ErrorAction 'Stop' + $null = New-WmiNamespace $Namespace -CreateSubTree -ErrorAction 'Stop' } ElseIf (-not $NamespaceTest) { $NamespaceNotFoundErr = "Namespace [$Namespace] does not exist. Use the -CreateDestination switch to create namespace." diff --git a/Public/New-WmiNameSpace.ps1 b/Public/New-WmiNameSpace.ps1 index 1d6cc84..af5e1fc 100644 --- a/Public/New-WmiNameSpace.ps1 +++ b/Public/New-WmiNameSpace.ps1 @@ -1,5 +1,5 @@ -#region Function New-WmiNameSpace -Function New-WmiNameSpace { +#region Function New-WmiNamespace +Function New-WmiNamespace { <# .SYNOPSIS This function is used to create a new WMI namespace. @@ -10,9 +10,9 @@ Function New-WmiNameSpace { .PARAMETER CreateSubTree This swith is used to create the whole namespace sub tree if it does not exist. .EXAMPLE - New-WmiNameSpace -Namespace 'ROOT\SCCM' + New-WmiNamespace -Namespace 'ROOT\SCCM' .EXAMPLE - New-WmiNameSpace -Namespace 'ROOT\SCCM\SCCMZone\Blog' -CreateSubTree + New-WmiNamespace -Namespace 'ROOT\SCCM\SCCMZone\Blog' -CreateSubTree .NOTES This is a module function and can typically be called directly. .LINK @@ -39,7 +39,7 @@ Function New-WmiNameSpace { Try { ## Check if the namespace exists - $WmiNamespace = Get-WmiNameSpace -Namespace $Namespace -ErrorAction 'SilentlyContinue' + $WmiNamespace = Get-WmiNamespace -Namespace $Namespace -ErrorAction 'SilentlyContinue' ## Create Namespace if it does not exist If (-not $WmiNamespace) { @@ -79,7 +79,7 @@ Function New-WmiNameSpace { $PathProps = [ordered]@{ 'NamespacePath' = $(Split-Path -Path $Path) 'NamespaceName' = $(Split-Path -Path $Path -Leaf) - 'NamespaceTest' = [boolean]$(Get-WmiNameSpace -Namespace $Path -ErrorAction 'SilentlyContinue') + 'NamespaceTest' = [boolean]$(Get-WmiNamespace -Namespace $Path -ErrorAction 'SilentlyContinue') } $NamespacePathsObject += [PSCustomObject]$PathProps } diff --git a/Public/Remove-WmiNamespace.ps1 b/Public/Remove-WmiNamespace.ps1 index f6a2ba9..24c9736 100644 --- a/Public/Remove-WmiNamespace.ps1 +++ b/Public/Remove-WmiNamespace.ps1 @@ -1,5 +1,5 @@ -#region Function Remove-WmiNameSpace -Function Remove-WmiNameSpace { +#region Function Remove-WmiNamespace +Function Remove-WmiNamespace { <# .SYNOPSIS This function is used to delete a WMI namespace. @@ -12,7 +12,7 @@ Function Remove-WmiNameSpace { .PARAMETER Recurse This switch deletes all existing child namespaces in the specified path. .EXAMPLE - Remove-WmiNameSpace -Namespace 'ROOT\SCCM' -Force -Recurse + Remove-WmiNamespace -Namespace 'ROOT\SCCM' -Force -Recurse .NOTES This is a module function and can typically be called directly. .LINK @@ -47,13 +47,13 @@ Function Remove-WmiNameSpace { $NamespaceName = Split-Path -Path $Namespace -Leaf ## Check if the namespace exists - $null = Get-WmiNameSpace -Namespace $Namespace -ErrorAction 'Stop' + $null = Get-WmiNamespace -Namespace $Namespace -ErrorAction 'Stop' ## Check if there are any classes $ClassTest = Get-WmiClass -Namespace $Namespace -ErrorAction 'SilentlyContinue' ## Check if there are any child namespaces or if the -Recurse switch was specified - $ChildNamespaceTest = (Get-WmiNameSpace -Namespace $($Namespace + '\*') -ErrorAction 'SilentlyContinue').Name + $ChildNamespaceTest = (Get-WmiNamespace -Namespace $($Namespace + '\*') -ErrorAction 'SilentlyContinue').Name If ((-not $ChildNamespaceTest) -or $Recurse) { # Remove all existing classes and instances if the -Force switch was specified diff --git a/Public/Rename-WmiNamespace.ps1 b/Public/Rename-WmiNamespace.ps1 index 92f40b9..e3732bf 100644 --- a/Public/Rename-WmiNamespace.ps1 +++ b/Public/Rename-WmiNamespace.ps1 @@ -46,16 +46,16 @@ Function Rename-WmiNamespace { $NamespaceDestination = Join-Path -Path $Namespace -ChildPath $NewName ## Check if the source namespace exists - $null = Get-WmiNameSpace -Namespace $NamespaceSource -ErrorVariable 'Stop' + $null = Get-WmiNamespace -Namespace $NamespaceSource -ErrorVariable 'Stop' # Create the new namespace but throw an error if it already exists - New-WmiNameSpace -Namespace $NamespaceDestination -ErrorAction 'Stop' + New-WmiNamespace -Namespace $NamespaceDestination -ErrorAction 'Stop' # Copy the old namespace Copy-WmiNamespace -NamespaceSource $NamespaceSource -NamespaceDestination $NamespaceDestination -Force -ErrorAction 'Stop' # Remove old Namespace - Remove-WmiNameSpace -Namespace $NamespaceSource -Recurse -Force + Remove-WmiNamespace -Namespace $NamespaceSource -Recurse -Force # Write success message to console Write-Log -Message "Succesfully renamed namespace [$NamespaceSource -> $NamespaceDestination]" -Source ${CmdletName}