#180782 - 2007-09-24 02:15 PM
Delete key from remote computer
|
BradV
Seasoned Scripter
Registered: 2006-08-16
Posts: 686
Loc: Maryland, USA
|
Good Morning,
I'm trying to delete a key on a remote computer. My account has admin privelages on the remote and local computer. They key does not have any sub-keys (it does have values). I am using kixtart 4.53 and the computers are a combination of w2k SP4 and xp SP2. When I try to delete the key, I get "The handle is invalid," error code 6. Here is a portion of the code ($strWks already has the name of the remote computer):
$strProfReg = "\\" + $strWks + "\HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList"
$arrProfs = ArrayEnumKey($strProfReg)
For Each $strProf In $arrProfs
If Len($strProf) > 8
$strSID = Right($strProf,Len($strProf)-InStrRev($strProf,"-"))
If $strSID <> "500"
$strPath = ExpandEnvironmentVars(ReadValue($strProfReg + "\" + $strProf,"ProfileImagePath"))
DelDir($strPath)
$intRetCode = DelKey($strProfReg + "\" + $strProf)
? @SERROR
If $intRetCode <> 0
? "There was a problem deleting the registry key:"
? $strProfReg + "\" + $strProf
? "The error code is: " + $intRetCode
EndIf
EndIf
EndIf
Next
There is some more code in there so that I am not deleting all the profiles. Everything else works fine, except when I try to delete the key.
Regards,
Brad V
|
Top
|
|
|
|
#180784 - 2007-09-24 03:44 PM
Re: Delete key from remote computer
[Re: Glenn Barnas]
|
Benny69
Moderator
Registered: 2003-10-29
Posts: 1036
Loc: Lincoln, Ne
|
good questions all;
as well as if you reduce your script to just: delkey("\\SomeComputerName\HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList") does it work?
|
Top
|
|
|
|
#180786 - 2007-09-24 04:21 PM
Re: Delete key from remote computer
[Re: BradV]
|
BradV
Seasoned Scripter
Registered: 2006-08-16
Posts: 686
Loc: Maryland, USA
|
Just trying to delete:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList\S-1-5-21-85914438-2897524180-28975
works. If I put "\\workstation_name\" in front of it. It does not.
Regards,
Brad V
|
Top
|
|
|
|
#180821 - 2007-09-25 01:03 PM
Re: Delete key from remote computer
[Re: Sealeopard]
|
BradV
Seasoned Scripter
Registered: 2006-08-16
Posts: 686
Loc: Maryland, USA
|
OK, what I'm trying to do is write a script to delete all the roaming profiles from a remote computer. Retrieving the list of users and determining the correct ones to delete works. I'm now to the point where I am trying to do the file and registry deletions. That is where I am running into problems. Here is the script so far:
Break On
Dim $SO
$SO = SetOption('Explicit', 'On')
$SO = SetOption('NoMacrosInStrings', 'On')
DIM $strWks, $strProfReg, $arrProfs, $strProf
DIM $strSID, $strPath, $strUser, $strFile, $intRetCode
$strWks = ""
While $strWks == ""
? "Please enter the name of the workstation you wish to delete profiles from: "
Gets $strWks
Loop
$strWks = Trim($strWks)
$strFile = "C:\Documents and Settings\" + @userid + "\Desktop\" + $strWks + "_profiles.ini"
; Enumerate the keys under:
; \\$strWks\HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList
; The local administrator key ends with "-500"
$strProfReg = "\\" + $strWks + "\HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList"
$arrProfs = ArrayEnumKey($strProfReg)
For Each $strProf In $arrProfs
If Len($strProf) > 8
; Local accounts are short. System is S-1-5-18, etc.
; Grab the last portion of the SID (after the last hyphen).
$strSID = Right($strProf,Len($strProf)-InStrRev($strProf,"-"))
If $strSid <> "500"
$strPath = ExpandEnvironmentVars(ReadValue($strProfReg + "\" + $strProf, "ProfileImagePath"))
$strUser = Right($strPath,Len($strPath)-InStrRev($strPath,"\"))
If Left($strUser,4) <> "SMSC"
; Don't want to delete the SMS accounts.
; Which should leave us with the accounts we do want to delete.
$intRetCode = WriteProfileStrin($strFile,SIDtoName($strProf),"Path",$strPath)
? "Deleting files for " + SIDtoName($strProf)
? "Deleting path " + $strPath
DelDir($strPath)
; The directory is now gone, delete the registry key.
$intRetCode = DelKey($strProfReg + "\" + $strProf)
? @SERROR
If $intRetCode <> 0
? There was a problem deleting the regsitry key:
? $strProfReg + "\" + $strProf
? "The error code is: " + $intRetCode
EndIf
EndIf
EndIf
EndIf
Next
;
Function ArrayEnumKey($strRegKey)
Dim $intIndex, $arrOfKeys[0], $strKey
If KeyExist($strRegKey)
$intIndex = 0
$strKey = EnumKey($strRegKey,$intIndex)
While @Error = 0
ReDim Preserve $arrOfKeys[$intIndex]
$arrOfKeys[$intIndex] = $strKey
$intIndex = $intIndex + 1
$strKey = EnumKey($strRegKey,$intIndex)
Loop
$ArrayEnumKey = $arrOfKeys
Else
$ArrayEnumKey = ""
EndIf
EndFunction
;
Function DelDir($strPath)
Dim $strFilename
$strFilename = Dir($strPath + "\*.*")
While $strFilename <> "" And @Error = 0
If $strFilename <> "." And $strFilename <> ".."
If (GetFileAttr($strPath + "\" + $strFilename) & 16)
DelDir($strPath + "\" + $strFilename)
SetFileAttr($strPath + "\" + $strFilename, 128)
Rd $strPath + "\" + $strFilename
Else
SetFileAttr($strPath + "\" + $strFilename, 128)
Del $strPath + "\" + $strFilename
EndIf
EndIf
$strFilename = Dir()
Loop
; At this point, the directory should be empty. We just need to delete it now.
Rd $strPath
EndFunction
I've left out a lot of comments just because I had to re-type it here. I was doing my testing on my own machine. When I tried it from my machine to my co-workers, it deleted the files from my computer and still gave the invalid handle message when trying to delete the registry entry.
It looks like DelDir doesn't work remotely nor does DelKey.
Regards,
Brad V
|
Top
|
|
|
|
#180822 - 2007-09-25 01:36 PM
Re: Delete key from remote computer
[Re: BradV]
|
Glenn Barnas
KiX Supporter
Registered: 2003-01-28
Posts: 4396
Loc: New Jersey
|
Well, remote delete using DelDir does work for me, but - without appropriate error return coding, you won't know what's failing.
This deleted the remote Start Menu folder contents, but not the folder itself. Note the added error handling. $DelDir will be 1, 2, or 3 depending on what step failed, and will return the error code in the Exit. $DelDir will be zero on success. Honestly, this is not how I'd code it for production use - I would return 1 on success, 0 on error, but for now you need to know which step has failed.
Glenn
$Path = '\\thatPC\c$\Documents and Settings\user3\Start Menu'
; Call DelDir, let the return value fall to the screen,
; followed by the error value / message
DelDir($Path)
' / ' @ERROR ' / ' @SERROR ?
Function DelDir($strPath)
Dim $strFilename
$strFilename = Dir($strPath + "\*.*")
While $strFilename <> "" And @Error = 0
If $strFilename <> "." And $strFilename <> ".."
If (GetFileAttr($strPath + "\" + $strFilename) & 16)
DelDir($strPath + "\" + $strFilename)
If @ERROR $DelDir = 1 Exit @ERROR EndIf
SetFileAttr($strPath + "\" + $strFilename, 128)
Rd $strPath + "\" + $strFilename
If @ERROR $DelDir = 2 Exit @ERROR EndIf
Else
SetFileAttr($strPath + "\" + $strFilename, 128)
Del $strPath + "\" + $strFilename
If @ERROR Exit @ERROR EndIf
EndIf
EndIf
$strFilename = Dir()
Loop
; At this point, the directory should be empty. We just need to delete it now.
Rd $strPath
If @ERROR $DelDir = 3 Exit @ERROR EndIf
EndFunction
_________________________
Actually I am a Rocket Scientist!
|
Top
|
|
|
|
#180826 - 2007-09-25 03:58 PM
Re: Delete key from remote computer
[Re: Glenn Barnas]
|
BradV
Seasoned Scripter
Registered: 2006-08-16
Posts: 686
Loc: Maryland, USA
|
Thanks Glen, I'll give that a shot. deldir is just a udf I found in the collection. It doesn't have any author information, so I could not attribute it properly. I'll see what errors I'm getting from it.
Regards,
Brad V
|
Top
|
|
|
|
#180844 - 2007-09-26 08:37 AM
Re: Delete key from remote computer
[Re: NTDOC]
|
Arend_
MM club member
Registered: 2005-01-17
Posts: 1894
Loc: Hilversum, The Netherlands
|
Just use delprof.exe from the windows 2003 resource kit and invoke that from the loginscript and your set
|
Top
|
|
|
|
#180895 - 2007-09-27 06:07 AM
Re: Delete key from remote computer
[Re: BradV]
|
NTDOC
Administrator
Registered: 2000-07-28
Posts: 11623
Loc: CA
|
Okay Brad I found the script I was talking about.
Please read this post and it should help you to achieve what you're wanting to do I think.
Removing old profiles with a script http://www.kixtart.org/forums/ubbthreads.php?ubb=showflat&Number=161631
Note that my code removes the folders but not the data within the registry whereas the code from Christophe Melin addresses the registry.
|
Top
|
|
|
|
Moderator: Glenn Barnas, NTDOC, Arend_, Jochen, Radimus, Allen, ShaneEP, Ruud van Velsen, Mart
|
0 registered
and 248 anonymous users online.
|
|
|