Shaun Cassells at MyITForum.com

SMS 2003 and ConfigMgr 2007, PowerShell, Scripting, Finance, Fitness and Fun

News

Locations of visitors to this page

February 2009 - Posts

Add WBEM to Path

The following script will add %systemroot%;%systemroot%\system32;%systemroot%\system32\wbem to the environment path of a remote computer passed.  However, this utilizes WMI to do the remote connect.  There are other options to do remote administration.. or good ole psexec from www.sysinternals.com

 <careful of line wrap>

'==========================================================================
'
' NAME: AddWbemToPath.vbs
'
'
' COMMENT: Adds "%systemroot%;%systemroot%\system32;%systemroot%\system32\wbem"
'   to the environment path of the remote computer passed to this
'   script.
'   Example commandline: AddWbemToPath.vbs Computername
'
'==========================================================================
On Error Resume Next

'Define Variables
Const HKEY_LOCAL_MACHINE = &H80000002
strKeyPath = "SYSTEM\CurrentControlSet\Control\Session Manager\Environment"
strValueName = "Path"
strVarSysRoot = "%SYSTEMROOT%"
strSysRoot = "C:\WINDOWS"
strSystem32 = "\SYSTEM32"
strwbem = "\WBEM"
bRoot = False
bSys32 = False
bWBEM = False

'=======================
'Process Arguement
Set objArgs = WScript.Arguments
If objArgs.count = 1 Then
 strComputer = objArgs(0)
Else
 WScript.Quit 160
End If


'=======================
'Connect to WMI Registry
'=======================
Set objReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\"&_
    strComputer & "\root\default:StdRegProv")

If Err.Number <> 0 Then
 WScript.Quit Err.Number
End If

'=======================
'Get Path
'=======================
Return = objReg.GetExpandedStringValue(HKEY_LOCAL_MACHINE,_
    strKeyPath,strValueName,strValue)
If (Return = 0) And (Err.Number = 0) Then  
 If strValue <> "" Then
'Remove Trailing Semicolon and assign value to Current Path Variable
  strCheck = Mid(strValue,Len(strValue))
  If strCheck = ";" Then
   strCurPath=Mid(strValue,1,Len(strValue)-1)
  Else
   strCurPath = strValue
  End If
 Else
  WScript.Quit 3
 End If
Else
    WScript.Quit Err.Number
End If

'=======================
'Manipulate Path
'=======================
arrPath = Split(strCurPath,";")
Set objDict = CreateObject("Scripting.Dictionary")
For i = 0 To UBound(arrPath)
 strPath = arrPath(i)

'Remove trailing slash
If strPath <> "" Then
 checkSlash = Mid(strPath,Len(strpath))
 If checkSlash = "\" Then strPath=Mid(strPath,1,Len(strpath)-1)
End If

'Add to dictionary
 objDict.Add i, UCase(strPath)
Next
iLastEntry = UBound(arrPath)

'Check current Path
For Each item In objDict
 If (objDict(item)=strVarSysRoot) Or (objDict(item)=strSysRoot) Then
  bRoot = True
 End If
 If (objDict(item)=strVarSysRoot & strSystem32) Or (objDict(item)=strSysRoot & strSystem32) Then
  bSys32 = True
 End If
 If (objDict(item)=strVarSysRoot & strSystem32 & strwbem) Or (objDict(item)=strSysRoot & strSystem32 & strwbem) Then
  bWBEM = True
 End If
Next

'Update Dictionary
If bRoot=False Then
 iLastEntry = iLastEntry + 1
 objDict.Add iLastEntry, UCase(strVarSysRoot)
End If
If bSys32=False Then
 iLastEntry = iLastEntry + 1
 objDict.Add iLastEntry, UCase(strVarSysRoot & strSystem32)
End If
If bWBEM = False Then
 iLastEntry = iLastEntry + 1
 objDict.Add iLastEntry, UCase(strVarSysRoot & strSystem32 & strwbem)
End If

'Compile new path string
For Each item In objDict
 If objDict(item) <> "" Then strNewPath = strNewPath & ";" & objDict(item)
Next
strNewPath = Mid(strNewPath,2,Len(strNewPath)-1) & ";"

'=======================
'Set Path
'=======================
strValue = strNewPath

If bRoot And bSys32 And bWBEM Then
 Wscript.Echo "No Path Change"
 WScript.Quit 0
Else
' Write expanded string value
 Wscript.Echo strValue
 Return = objReg.SetExpandedStringValue( _
     HKEY_LOCAL_MACHINE,strKeyPath,strValueName,strValue)
 If (Return = 0) And (Err.Number = 0) Then  
     WScript.Quit 3010
 Else
     WScript.Quit Err.Number
 End If
End If

WScript.Quit Err.Number