LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-06-2012, 03:08 PM   #1
mikes88
Member
 
Registered: Jan 2012
Posts: 61

Rep: Reputation: Disabled
vbscript removing administrator folder


Hey everyone,

I have this vbscript that im testing to remove user profiles from a pc (Win7/2008 R2 DC). Now when i run it, it removes all the profiles like its suppose too and skipping the vitals (Public, all users, default, default user). The problem that arises, is that its removing the local administrator profile as well. Ive specified it with the other folders to skip it but for some reason its not.

Heres the code that i have:

Code:
const HKEY_LOCAL_MACHINE = &H80000002

set oDic=createobject("scripting.dictionary")
Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\default:StdRegProv")
Set oWMIService = GetObject("winmgmts:\\.\root\cimv2")
set oFSO=createobject("scripting.filesystemobject")
set oShell=createobject("wscript.shell")

on error resume next

'are we on an AV machine? if so, quit - leave profiles alone in theatres
if ofso.fileexists("c:\windows\av") then wscript.quit

Set colItems = oWMIService.ExecQuery("Select * from Win32_UserProfile where special=false and loaded=false",,48)
For Each oItem in colItems
  sSid=oItem.SID
  Set oUserProfile = GetObject("winmgmts:{impersonationlevel=impersonate}!\\.\root\cimv2:Win32_UserProfile.SID='" & sSID &"'")
  oUserProfile.Delete_
Next

'now clean up directories not attached to profiles
'and profiles not completely deleted by first step
'build a list of the directories used by profiles

sPath = "SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList"
oReg.EnumKey HKEY_LOCAL_MACHINE, sPath, arrSubKeys
For Each subkey In arrSubKeys
  lRc=oReg.GetStringValue(HKEY_LOCAL_MACHINE, sPath & "\" & subkey ,"ProfileImagePath",sDir)
  sDir=lcase(sDir)
  oDic.add sDir, subkey
Next

'now add the "fixed" profiles

lRc=oReg.GetStringValue(HKEY_LOCAL_MACHINE, sPath ,"ProfilesDirectory",sRoot)
sRoot=lcase(oShell.expandenvironmentstrings(sRoot))
sRoot=sRoot & "\"

oDic.add sRoot & "public","public"
oDic.add sRoot & "all users", "all users"
oDic.add sRoot & "default","default"
oDic.add sRoot & "default user", "default user"
oDic.add sRoot & "administrator", "administrator"


set oFolder=ofso.getfolder("c:\users")
for each oSubFolder in oFolder.subfolders
  sFolder=sRoot & lcase(oSubFolder.name)
  if not(oDic.exists(sFolder)) then
    'orphaned folder so delete it
    ofso.deletefolder sFolder, true
  end if
next
Any help would be greatly appreciated.
 
Old 07-06-2012, 04:05 PM   #2
Kustom42
Senior Member
 
Registered: Mar 2012
Distribution: Red Hat
Posts: 1,604

Rep: Reputation: 415Reputation: 415Reputation: 415Reputation: 415Reputation: 415
Not sure if this forum is the best outlet for windows and VB script questions. Hopefully someone can follow up with some insight as I cannot but you may want to consider posting this in a windows/vb based forum as well
 
Old 07-06-2012, 04:55 PM   #3
273
LQ Addict
 
Registered: Dec 2011
Location: UK
Distribution: Debian Sid AMD64, Raspbian Wheezy, various VMs
Posts: 7,680

Rep: Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373
Personally I'd put the test as an "if" in the loop body rather than part of the loop. Then try echoing the things you're reading and if needed change the test you're applying.
I've no idea whether the above is good or bad practice but all my VBScripts have worked in the end.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Removing a mounted drive folder safely elliot01 Linux - Software 3 12-04-2009 11:48 AM
Removing non existant folder squady Linux - Newbie 2 10-11-2005 12:10 AM
Removing a Directory from the Terminal without deleting each file or folder lennysokol Linux - General 4 05-30-2005 11:30 PM
Vbscript in linux mabs Linux - Software 3 07-11-2004 07:16 AM
vbscript damax Linux - Newbie 1 12-02-2003 11:46 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:45 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration