We are trying to clean up student computers by removing User Profiles and the associated Registry entry.
I started with this:
When I entry the full path, it works great.
"SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList\S-1-5-21-2176375418-3681359980-2869953772-23107"
When I try to append it to my script, it fails on the registry deletion part and I do not know why.
Any help is welcomed!
'===========================================================================
On Error Resume Next
'Setting Constants, dims and sets
Const HKEY_LOCAL_MACHINE = &H80000002
Dim strComputer, strDomain, strUsername
Set oReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\default:StdRegProv")
Set FSO = CreateObject("Scripting.FileSystemObject")
Set objShell = CreateObject("WScript.Shell")
strDomain = "ELKRIVER"
'===========================================================================
osType = objShell.ExpandEnvironmentStrings("%PROCESSOR_ARCHITECTURE%")
'Check OS bit Version and set correct User Directory Path
If osType = "x86" Then
UserPath = ("C:\Documents and Settings\")
ElseIf osType = "AMD64" Then
UserPath = ("C:\Users\")
End If
'===========================================================================
' Get Folder Conents as list
ShowSubfolders FSO.GetFolder(UserPath)
Sub ShowSubFolders(Folder)
For Each Subfolder in Folder.SubFolders
' Skip needed folder names
If SubFolder.Name <> "Administrator" AND SubFolder.Name <> "Domain Admins" AND SubFolder.Name <> "administrator.ELKRIVER" AND SubFolder.Name <> "Administrator.ELKRIVER" AND SubFolder.Name <> "All Users" AND SubFolder.Name <> "Default" AND SubFolder.Name <> "Default User" AND SubFolder.Name <> "Public" Then
' Set User name for current SUB
strUsername = SubFolder.Name
wscript.echo strUsername
'===========================================================================
'Delete Users' Folder
'Subfolder.Delete
'CMDLine1 = "cmd /c rd /S /Q " & Subfolder.Name
'objShell.Run CMDLine1
'===========================================================================
'Delete Regist ENTRY
' Object used to get StdRegProv Namespace
Set wmiLocator = CreateObject("WbemScripting.SWbemLocator")
' Object used to determine local machine name
Set wshNetwork = CreateObject("WScript.Network")
' Registry Provider (StdRegProv) lives in root\default namespace.
Set wmiNameSpace = wmiLocator.ConnectServer(wshNetwork.ComputerName, "root\default")
Set objRegistry = wmiNameSpace.Get("StdRegProv")
' Deletes Key with all subkeys
sPath = "SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList\" & getSID
wscript.echo sPath '** This echos out perfectly, but after the script stops.
' If I comment out the line below "lRC" my scripts works just fine.
lRC = DeleteRegEntry(HKEY_LOCAL_MACHINE, sPath)
End If
NEXT
End Sub
'===========================================================================
Function DeleteRegEntry(sHive, sEnumPath)
' Attempt to delete key. If it fails, start the subkey
' enumration process.
lRC = objRegistry.DeleteKey(sHive, sEnumPath)
' The deletion failed, start deleting subkeys.
If (lRC <> 0) Then
' Subkey Enumerator
On Error Resume Next
lRC = objRegistry.EnumKey(HKEY_LOCAL_MACHINE, sEnumPath, sNames)
For Each sKeyName In sNames
If Err.Number <> 0 Then Exit For
lRC = DeleteRegEntry(sHive, sEnumPath & "\" & sKeyName)
Next
On Error Goto 0
' At this point we should have looped through all subkeys, trying
' to delete the registry key again.
lRC = objRegistry.DeleteKey(sHive, sEnumPath)
End If
End Function
'===========================================================================
' get SID for User
Private Function getSID()
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set objAccount = objWMIService.Get("Win32_UserAccount.Name='" & strUsername & "',Domain='" & strDomain & "'")
getSID = objAccount.SID
End Function
'===========================================================================
I started with this:
When I entry the full path, it works great.
"SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList\S-1-5-21-2176375418-3681359980-2869953772-23107"
When I try to append it to my script, it fails on the registry deletion part and I do not know why.
Any help is welcomed!
'===========================================================================
On Error Resume Next
'Setting Constants, dims and sets
Const HKEY_LOCAL_MACHINE = &H80000002
Dim strComputer, strDomain, strUsername
Set oReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\default:StdRegProv")
Set FSO = CreateObject("Scripting.FileSystemObject")
Set objShell = CreateObject("WScript.Shell")
strDomain = "ELKRIVER"
'===========================================================================
osType = objShell.ExpandEnvironmentStrings("%PROCESSOR_ARCHITECTURE%")
'Check OS bit Version and set correct User Directory Path
If osType = "x86" Then
UserPath = ("C:\Documents and Settings\")
ElseIf osType = "AMD64" Then
UserPath = ("C:\Users\")
End If
'===========================================================================
' Get Folder Conents as list
ShowSubfolders FSO.GetFolder(UserPath)
Sub ShowSubFolders(Folder)
For Each Subfolder in Folder.SubFolders
' Skip needed folder names
If SubFolder.Name <> "Administrator" AND SubFolder.Name <> "Domain Admins" AND SubFolder.Name <> "administrator.ELKRIVER" AND SubFolder.Name <> "Administrator.ELKRIVER" AND SubFolder.Name <> "All Users" AND SubFolder.Name <> "Default" AND SubFolder.Name <> "Default User" AND SubFolder.Name <> "Public" Then
' Set User name for current SUB
strUsername = SubFolder.Name
wscript.echo strUsername
'===========================================================================
'Delete Users' Folder
'Subfolder.Delete
'CMDLine1 = "cmd /c rd /S /Q " & Subfolder.Name
'objShell.Run CMDLine1
'===========================================================================
'Delete Regist ENTRY
' Object used to get StdRegProv Namespace
Set wmiLocator = CreateObject("WbemScripting.SWbemLocator")
' Object used to determine local machine name
Set wshNetwork = CreateObject("WScript.Network")
' Registry Provider (StdRegProv) lives in root\default namespace.
Set wmiNameSpace = wmiLocator.ConnectServer(wshNetwork.ComputerName, "root\default")
Set objRegistry = wmiNameSpace.Get("StdRegProv")
' Deletes Key with all subkeys
sPath = "SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList\" & getSID
wscript.echo sPath '** This echos out perfectly, but after the script stops.
' If I comment out the line below "lRC" my scripts works just fine.
lRC = DeleteRegEntry(HKEY_LOCAL_MACHINE, sPath)
End If
NEXT
End Sub
'===========================================================================
Function DeleteRegEntry(sHive, sEnumPath)
' Attempt to delete key. If it fails, start the subkey
' enumration process.
lRC = objRegistry.DeleteKey(sHive, sEnumPath)
' The deletion failed, start deleting subkeys.
If (lRC <> 0) Then
' Subkey Enumerator
On Error Resume Next
lRC = objRegistry.EnumKey(HKEY_LOCAL_MACHINE, sEnumPath, sNames)
For Each sKeyName In sNames
If Err.Number <> 0 Then Exit For
lRC = DeleteRegEntry(sHive, sEnumPath & "\" & sKeyName)
Next
On Error Goto 0
' At this point we should have looped through all subkeys, trying
' to delete the registry key again.
lRC = objRegistry.DeleteKey(sHive, sEnumPath)
End If
End Function
'===========================================================================
' get SID for User
Private Function getSID()
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set objAccount = objWMIService.Get("Win32_UserAccount.Name='" & strUsername & "',Domain='" & strDomain & "'")
getSID = objAccount.SID
End Function
'===========================================================================