I am trying to create a script that performs the below steps:
- Enter Remote machine name
- Enter User's Domain and User name (DOMAIN1\USERNAME)
- Run WMI query to obtain SID
- Open either HKEY_CURRENT_USER (or HKEY_USERS and locate SID)
- Delete Key variable key from defined strKeyPath , in this case = "Software\Policies\Microsoft\Windows\Safer\CodeIdentifiers\0\Paths"
On Error Resume Next
Function GetSIDFromUser(UserName)
Dim DomainName, Result, WMIUser
If InStr(UserName, "DOMAIN1\") > 0 Or InStr(UserName, "DOMAIN2\") > 0 Then
DomainName = Mid(UserName, 1, InStr(UserName, "\") - 1)
UserName = Mid(UserName, InStr(UserName, "\") + 1)
Else
WScript.Echo "ERROR: Please add user's domain. Format DOMAIN1\USERNAME "
WScript.quit
End If
On Error Resume Next
Set WMIUser = GetObject("winmgmts:{impersonationlevel=impersonate}!" _
& "/root/cimv2:Win32_UserAccount.Domain='" & DomainName & "'" _
& ",Name='" & UserName & "'")
If Err = 0 Then Result = WMIUser.SID Else Result = ""
On Error GoTo 0
GetSIDFromUser = Result
End Function
'Constants
Const HKEY_CURRENT_USER = &H80000001
Const HKEY_USERS = &H80000003
'Input
strComputer = UCase(InputBox("Please enter the Computer Name:", "Outlook Policy - Remote Fix"))
strUser = UCase(InputBox("Please enter the User Name:", "Outlook Policy - Remote Fix"))
If (strcomputer <> vbnullstring) Then
If (strUser <> vbnullstring) Then
strSID = GetSIDFromUser(strUser)
'Set objRegistry = GetObject("winmgmts:\\" & _
'strComputer & "\root\default:StdRegProv")
Set objRegistry = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & _
strComputer & "\root\default:StdRegProv")
'Path
'strKeyPath = "Software\Policies\Microsoft\Windows\Safer\CodeIdentifiers\0\Paths"
'Test Path
strKeyPath = "Software\Test"
'Delete keys and subkeys
DeleteSubkeys HKEY_CURRENT_USER, strKeypath
Sub DeleteSubkeys(HKEY_CURRENT_USER, strKeyPath)
'objRegistry.EnumKey HKEY_CURRENT_USER, strKeyPath, arrSubkeys
If IsArray(arrSubkeys) Then
For Each strSubkey In arrSubkeys
DeleteSubkeys HKEY_CURRENT_USER, strKeyPath & "\" & strSubkey
Next
End If
objRegistry.DeleteKey HKEY_CURRENT_USER, strKeyPath
End Sub
'Error reporting
If Err <> 0 Then
WScript.Echo "Error Number: " & Err.Number & VBNewLine & Err.Description
Err.Clear
Else
WScript.Echo "Success! The Outlook policy has now been updated. Please instruct the user to restart the computer, for this policy to take effect."
End If
End if
End if
PS You haven't specified any problem, giving any error description, or error numbers. WMI returns 0 for sucess and a wbemerrorenum for failure. That's at the bottom of the post.
WMi doesn't do HKCU remotely. From Help
If it did it would only do current logged on user as that is HKCU. Therefore you don't need their username.
If you want a non logged on user (remotely) then it doesn't seem possible as a Reg command (see reg load /?) or WMI (no load profile command).
This will load a user profile. LOCALLY
This will run a command on another computer (notice escaping of " and \ with a \) loading a profile