简体   繁体   中英

Escape characters in WMI namespace

I'm trying to find all instances of class ReportServer_ConfigurationSetting (configuration of SSRS) on local machine. This boils down to finding proper namespace and listing objects in it. I have no problems with it on most machines, but there is one catch. I do it using the following code:

function SSRS-GetInstanceNamespace($sqlInstanceName) {
    $ns = "Root\Microsoft\SqlServer\ReportServer\RS_${sqlInstanceName}"
    $ns = ($ns + '\' + (Get-WmiObject -Namespace $ns -Class __Namespace).Name + "\Admin")
    return $ns
}    
$x = (Get-ItemProperty 'HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server').InstalledInstances
$x | % { SSRS-GetInstanceNamespace $_ }

This works in most cases, but one of our machines has instance name that contains underscore, eg "A_B". Unfortunately since underscore is meaningful in WMI instance (at least it seems so), it has to be escaped. I can do it using simple replace, but is there any method that will consistently escape all special characters? In example above what I need is "A_5fB" instead of "A_B".

I never solved this problem the way I wanted to, I simply walk through the namespaces and recursively do:

get-wmiobject -namespace $ns -class __namespace

looking for those that match my criteria and try to match instance name using "heuristics", that is if it does not match directly then I try replacing _ with 5F . This works, but I'm basically certain that this will be a nice source of long stream of errors as more and more clients use this tool.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM