Private/Get-WmiNamespaceRecursive.ps1

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
#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 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
    C:\PS> $Result = 'ROOT\SCCM', 'ROOT\Appv' | Get-WmiNamespaceRecursive
.INPUTS
    System.String[].
.OUTPUTS
    System.Management.Automation.PSCustomObject.
        'Name'
        'Path'
        'FullName'
.NOTES
    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://github.com/JhonnyTerminus/PSWmiToolKit
.LINK
    https://sccm-zone.com
.COMPONENT
    WMI
.FUNCTIONALITY
    WMI Management
#>

    [CmdletBinding()]
    Param (
        [Parameter(Mandatory=$true,ValueFromPipeline,Position=0)]
        [ValidateNotNullorEmpty()]
        [string[]]$Namespace
    )

    Begin {
        ## Initialize/Reset result object
        [PSCustomObject]$GetNamespaceRecursive = @()
    }
    Process {
        Try {

            ## Get all namespaces in the current root namespace
            $Namespaces = Get-WmiNamespace -Namespace $Namespace -List

            ## Search in the current namespace for other namespaces
            If ($Namespaces) {
                $Namespaces | ForEach-Object {
                    # Assemble the result object
                    $GetNamespaceRecursive += [PsCustomObject]@{
                        Name = $_.Name
                        Path = $_.Path
                        FullName = $_.FullName
                    }

                    # Call the function again for the next namespace
                    Get-WmiNamespaceRecursive -Namespace $_.FullName
                }
            }
        }
        Catch {
            Write-Log -Message "Failed to retrieve wmi namespace [$Namespace] recursively. `n$(Resolve-Error)" -Severity 3 -Source ${CmdletName}
            Break
        }
    }
    End {
        Write-Output -InputObject $GetNamespaceRecursive
    }
}
#endregion