Page 1 of 2 12>
Topic Options
#180782 - 2007-09-24 02:15 PM Delete key from remote computer
BradV Offline
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):

 Code:
$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
#180783 - 2007-09-24 03:34 PM Re: Delete key from remote computer [Re: BradV]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4396
Loc: New Jersey
Couple of possibly obvious questions:

Does the logic work elsewhere? What about on the local computer?

Are the security settings on the registry different? Use Regedt32 instead of RegEdit to confirm.

Remote Registry enabled on the target system?

Glenn
_________________________
Actually I am a Rocket Scientist! \:D

Top
#180784 - 2007-09-24 03:44 PM Re: Delete key from remote computer [Re: Glenn Barnas]
Benny69 Offline
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?
_________________________
Wait don't order yet,... get KiXforms Designer .NET 2.0 (Beta)
KiXforms Designer .NET 2.0 (Beta)

Top
#180785 - 2007-09-24 04:03 PM Re: Delete key from remote computer [Re: Benny69]
BradV Offline
Seasoned Scripter
****

Registered: 2006-08-16
Posts: 686
Loc: Maryland, USA
Well, I've been testing on my computer, which means it is local. The permissions are the the local admin group has full control and my account is a member of the local admin group on all computers. I wouldn't think there is a problem with the strings since I've already retrieved all the information. I will have it a try and see what happens.

Regards,

Brad V

Top
#180786 - 2007-09-24 04:21 PM Re: Delete key from remote computer [Re: BradV]
BradV Offline
Seasoned Scripter
****

Registered: 2006-08-16
Posts: 686
Loc: Maryland, USA
Just trying to delete:

 Code:
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
#180791 - 2007-09-24 06:53 PM Re: Delete key from remote computer [Re: BradV]
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
Where are you getting the workstation name from? If pulling it from a file, Trim() it as there could be a trailing space.
_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#180794 - 2007-09-24 08:18 PM Re: Delete key from remote computer [Re: Les]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4396
Loc: New Jersey
Good call, Les - been bit by spaces myself in the past.

Brad - if you're manually creating the local name, I'd still look at local/group policy that could disable remote registry. Not sure if it's an all or nothing, or can be configured to block writes only.

Glenn
_________________________
Actually I am a Rocket Scientist! \:D

Top
#180795 - 2007-09-24 09:27 PM Re: Delete key from remote computer [Re: Glenn Barnas]
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11623
Loc: CA
I think a policy only prevents "local" registry editing tools from running (and only default built-in tools) if you use a non Microsoft tool I think it will still run, or if you run it remotely as an Admin I think it will work.

Need to verify the ERROR CODES returned to determine what is really happening.

Top
#180797 - 2007-09-24 09:32 PM Re: Delete key from remote computer [Re: NTDOC]
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4672
Loc: The Netherlands
 Quote:

....
"The handle is invalid," error code 6
....


From experience I would guess that there is something wrong with the name of the remote system. Been bitten by this in the past. What do you see if you just display the name of the system you got from somewhere? Is this a valid name or is there some crap like spaces or whatever in front or at the back of it?
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
#180805 - 2007-09-25 05:20 AM Re: Delete key from remote computer [Re: Mart]
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11164
Loc: Boston, MA, USA
Also, "ExpandEnvironmentVars" will not resolve against the remote computer but the local computer.
_________________________
There are two types of vessels, submarines and targets.

Top
#180821 - 2007-09-25 01:03 PM Re: Delete key from remote computer [Re: Sealeopard]
BradV Offline
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:

 Code:
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 Administrator Offline
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

 Code:
$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! \:D

Top
#180826 - 2007-09-25 03:58 PM Re: Delete key from remote computer [Re: Glenn Barnas]
BradV Offline
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
#180829 - 2007-09-25 05:47 PM Re: Delete key from remote computer [Re: BradV]
Witto Offline
MM club member
*****

Registered: 2004-09-29
Posts: 1828
Loc: Belgium
ISTR that deleting user profiles on remote computers has been discussed before.
Could this thread be of some help?
http://www.kixtart.org/forums/ubbthreads...true#Post161631

Top
#180842 - 2007-09-26 05:59 AM Re: Delete key from remote computer [Re: BradV]
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11623
Loc: CA
I'm not 100% certain but I could swear there is a UDF for this already, or at least a posted script as I did one and I forget who but someone else did one as well at the same time and we compared notes, so I know it's on the board somewhere, you just need to search and find it.


Top
#180844 - 2007-09-26 08:37 AM Re: Delete key from remote computer [Re: NTDOC]
Arend_ Moderator Offline
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
#180846 - 2007-09-26 09:21 AM Re: Delete key from remote computer [Re: Arend_]
ChristopheM Offline
Hey THIS is FUN
*****

Registered: 2002-05-13
Posts: 309
Loc: STRASBOURG, France
Just an idea to bypass problem with remote commands :
write a script that delete local profiles (script A).
write a script that treats computers (script B).

in the script B, copy the script A on the remote computer and execute it with psexec. if you execute psexec -d, psexec doesn't wait for remote process to terminate, so your local script (B) can continue next computers.

I often use this method because i have about 4000 workstation to manage and it is faster. One problem with this solution is that by default, remote script has no access to network resources. If you need this, you have to give user and password to psexec
_________________________
Christophe

Top
#180851 - 2007-09-26 01:23 PM Re: Delete key from remote computer [Re: ChristopheM]
BradV Offline
Seasoned Scripter
****

Registered: 2006-08-16
Posts: 686
Loc: Maryland, USA
I found a major part of my problem. I need to add "\c$" after the workstation name and before the path. I was just giving deldir("\\workstation\temp") for example and that won't work since temp is not a share! Duh. I am finding some other problems in the logic. It decends down a path and deletes it, but then it gets stuck trying to go parallel. I will have to look at a little closer.

As far as delprof.exe is concerned, I don't think it can work in my case. I've never used it, so I may be wrong. However, from the syntax on Microsoft's web site, it looks like it either interactively asks you which profiles to delete or it deletes based on age. I want to be able to delete a specific profile. Ideally just one profile. If I can do that, I can string it together and do multiple ones.

I would like to be able to do single profiles to assist when we rebuild someone's profile. I would like to do all profiles other than the required ones on the system for computers such as in conference rooms. With so many people logging in, the local file system often gets filled and we have to go in and manually clean it up. I just wanted to be able to script this.

I'll let you know what I find out about fixing deldir.

Regards,

Brad V

Top
#180895 - 2007-09-27 06:07 AM Re: Delete key from remote computer [Re: BradV]
NTDOC Administrator Offline
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
#180899 - 2007-09-27 10:00 AM Re: Delete key from remote computer [Re: NTDOC]
Arend_ Moderator Offline
MM club member
*****

Registered: 2005-01-17
Posts: 1894
Loc: Hilversum, The Netherlands
delprof can run silently, which is how I use it in my loginscripts.
However it deletes all inactive profiles, except the system accounts.

Top
Page 1 of 2 12>


Moderator:  Glenn Barnas, NTDOC, Arend_, Jochen, Radimus, Allen, ShaneEP, Ruud van Velsen, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 248 anonymous users online.
Newest Members
gespanntleuchten, DaveatAdvanced, Paulo_Alves, UsTaaa, xxJJxx
17864 Registered Users

Generated in 0.075 seconds in which 0.026 seconds were spent on a total of 14 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org