Public/Rename-WmiNamespace.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
#region Function Rename-WmiNamespace
Function Rename-WmiNamespace {
<#
.SYNOPSIS
    This function is used to rename a WMI namespace.
.DESCRIPTION
    This function is used to rename a WMI namespace by creating a new namespace, copying all existing classes to it and removing the old one.
.PARAMETER Namespace
    Specifies the root namespace where to search for the namespace name. Default is: ROOT\cimv2.
.PARAMETER Name
    Specifies the namespace name to be renamed.
.PARAMETER NewName
    Specifies the new namespace name.
.EXAMPLE
    Rename-WmiNamespace -Namespace 'ROOT\cimv2' -Name 'SCCM' -NewName 'SCCMZone'
.NOTES
    This is a module function and can typically be called directly.
.LINK
    https://sccm-zone.com
.LINK
    https://github.com/JhonnyTerminus/SCCM
#>

    [CmdletBinding()]
    Param (
        [Parameter(Mandatory=$false,Position=0)]
        [ValidateNotNullorEmpty()]
        [string]$Namespace = 'ROOT\cimv2',
        [Parameter(Mandatory=$true,Position=1)]
        [ValidateNotNullorEmpty()]
        [string]$Name,
        [Parameter(Mandatory=$true,Position=2)]
        [ValidateNotNullorEmpty()]
        [string]$NewName
    )

    Begin {
        ## Get the name of this function and write header
        [string]${CmdletName} = $PSCmdlet.MyInvocation.MyCommand.Name
        Write-FunctionHeaderOrFooter -CmdletName ${CmdletName} -CmdletBoundParameters $PSBoundParameters -Header
    }
    Process {
        Try {

            ## Set namespace paths
            $NamespaceSource = Join-Path -Path $Namespace -ChildPath $Name
            $NamespaceDestination = Join-Path -Path $Namespace -ChildPath $NewName

            ## Check if the source namespace exists
            $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'

            # Copy the old namespace
            Copy-WmiNamespace -NamespaceSource $NamespaceSource -NamespaceDestination $NamespaceDestination -Force -ErrorAction 'Stop'

            # Remove old Namespace
            Remove-WmiNamespace -Namespace $NamespaceSource -Recurse -Force

            # Write success message to console
            Write-Log -Message "Succesfully renamed namespace [$NamespaceSource -> $NamespaceDestination]" -Source ${CmdletName}
        }
        Catch {
            Write-Log -Message "Failed to rename namespace. `n$(Resolve-Error)" -Severity 3 -Source ${CmdletName}
            Break
        }
        Finally {}
    }
    End {
        Write-FunctionHeaderOrFooter -CmdletName ${CmdletName} -Footer
    }
}
#endregion