Skip to content

Commit

Permalink
Bugfixes, turned debugging off command based help update, standardiza…
Browse files Browse the repository at this point in the history
…tions

## All
* Standardized WmiNameSpace by renaming it to WmiNamespace
## Write-Log
* Fixed logging path
* Turned debugging off
## Get-Namespace
* Added piping and handled non string input ot Get-CimInstance
## Get-NamespaceRecursive
* Fixed bug when no child namespaces are found
* Simplified for each loop
* Standardized input variable name
* Added piping
* Updated command based help
  • Loading branch information
Ioan Popovici committed Feb 2, 2018
1 parent 40911a9 commit f260ee0
Show file tree
Hide file tree
Showing 10 changed files with 75 additions and 68 deletions.
6 changes: 3 additions & 3 deletions Internal/Function Write-Log.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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 {
Expand Down
59 changes: 33 additions & 26 deletions Internal/Get-WmiNamespaceRecursive.ps1
Original file line number Diff line number Diff line change
@@ -1,64 +1,71 @@
#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
WMI Management
#>
[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
}
}
Expand Down
4 changes: 2 additions & 2 deletions Public/Copy-WmiClass.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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."
Expand Down
10 changes: 5 additions & 5 deletions Public/Copy-WmiNamespace.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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
Expand All @@ -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'
Expand Down
2 changes: 1 addition & 1 deletion Public/Get-WmiClass.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
30 changes: 15 additions & 15 deletions Public/Get-WmiNameSpace.ps1
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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 }
}
Expand Down
4 changes: 2 additions & 2 deletions Public/New-WmiClass.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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."
Expand Down
12 changes: 6 additions & 6 deletions Public/New-WmiNameSpace.ps1
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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
Expand All @@ -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) {
Expand Down Expand Up @@ -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
}
Expand Down
10 changes: 5 additions & 5 deletions Public/Remove-WmiNamespace.ps1
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions Public/Rename-WmiNamespace.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down

0 comments on commit f260ee0

Please sign in to comment.