Page 2 of 2 <12
Topic Options
#180902 - 2007-09-27 01:34 PM Re: Delete key from remote computer [Re: Arend_]
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11623
Loc: CA
Well the script above should run silently and remove based on date if wanted.
Should be a bit more flexible than the Microsoft tool.

Top
#181420 - 2007-10-10 01:05 PM Re: Delete key from remote computer [Re: NTDOC]
BradV Offline
Seasoned Scripter
****

Registered: 2006-08-16
Posts: 686
Loc: Maryland, USA
OK, I've done some more testing. First, I found a small error in the logic of the deldir UDF. It was a recursive algorithm that would never delete the top directory. I put a revised version in the UDF forum (http://www.kixtart.org/forums/ubbthreads.php?ubb=showflat&Number=181418#Post181418).

Now back to trying to delete a remote key. I simplified things by creating a dummy key and then trying to delete it. I keep getting error 6, handle is invalid.

 Code:
Dim $strWks, $strProfReg, $intRetCode
$strWks = ""
While $strWks == ""
   ? "Please enter the name of the workstation from which you wish to delete the key?"
   Gets $strWks
Loop
$strWks = Trim($strWks)
$strProfReg = "\\" + $strWks + "\HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList\testme"
addkey($strProfReg)
if @ERROR = 0
   ? "The key was added.  Now try to delete it."
   $intRetCode = DelKey($strProfReg)
   If @ERROR = 0
      ? "The key was deleted."
   Else
      ? "The key was not deleted.  The error code is: " + $intRetCode
      ? "The error message is: " + @SERROR
   EndIf
Else
   ? "The key was not written."
EndIf


If I remove '"\\" + $strWks +' from $strProfReg so that it works against the local computer, it works.

Is my syntax wrong? Am I missing something?

Thanks!

Brad V

Top
#181427 - 2007-10-10 01:52 PM Re: Delete key from remote computer [Re: BradV]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4396
Loc: New Jersey
Not sure if I posted this, but many of us use something like this to insure that system names are wrapped in "\\" and "\" (or not!)
 Code:
;;EXAMPLES       $Server = SNVerify($Server)
;
Function SNVerify($_System, Optional $_Reverse)

  ; Isolate system name, add leading & trailing slashes if needed
  $SNVerify = IIf(Not $_Reverse And CStr($_System) <> '', '\\' + Join(Split(CStr($_System), '\'), '', 3) + '\', Join(Split(CStr($_System), '\'), '', 3))

  Exit 0

EndFunction



Then you can do
 Code:
$strWks = SNVerify($strWks)
$strProfReg =  $strWks + "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList\testme"

Which will work regardless of whether $strWks contains data or not..

The function can be simplified to just add the leading & trailing slashes to the name (if the name isn't empty), but as a multi-purpose UDF, this form made more sense. I could call it with the REVERSE flag to insure that the name had no slashes for use with WMI calls, or without for file share and remote registry access.

The point here is that this insures that the registry read has the right format all the time.

What kind of remote PC are you querying? Vista turns off the remote registry service by default, and many organizations disable this for "hardening". To confirm, open RegEdit, then File/Connect to Remote System and see if you can.

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

Top
#181449 - 2007-10-10 08:11 PM Re: Delete key from remote computer [Re: Glenn Barnas]
BradV Offline
Seasoned Scripter
****

Registered: 2006-08-16
Posts: 686
Loc: Maryland, USA
I'll take a look at that in the morning. The remote pc is either w2k sp4 or xp sp2. I can remote connect to the registry.

Regards,

Brad V

Top
#181465 - 2007-10-10 10:01 PM Re: Delete key from remote computer [Re: BradV]
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11623
Loc: CA
No your code is correct. I think this is actually a BUG

Simply using DelTree works just fine.


 Code:
Dim $strWks, $strProfReg, $intRetCode, $AK
$strWks = ""
While Not $strWks
   ? "Please enter the name of the workstation from which you wish to delete the key?"
   Gets $strWks
Loop
$strWks = Trim($strWks)
$strProfReg = "\\" + $strWks + "\HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList\testme"
'Profile is: ' + $strProfReg ?
$AK = AddKey($strProfReg)
'Error: ' + @ERROR + ' - ' + @SERROR ?
If @ERROR = 0
   ? "The key was added.  Now try to delete it."
   $intRetCode = DelTree($strProfReg)
   If @ERROR = 0
      ? "The key was deleted."
   Else
      ? "The key was not deleted.  The error code is: " + $intRetCode
      ? "The error message is: " + @SERROR
   EndIf
Else
   ? "The key was not written."
EndIf


Top
#181504 - 2007-10-11 12:47 PM Re: Delete key from remote computer [Re: NTDOC]
BradV Offline
Seasoned Scripter
****

Registered: 2006-08-16
Posts: 686
Loc: Maryland, USA
OK, I feel like an idiot. I have to learn to RTFM more closely! \:\)

Thanks Doc!

Brad V

Top
Page 2 of 2 <12


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

Who's Online
1 registered (Allen) and 466 anonymous users online.
Newest Members
gespanntleuchten, DaveatAdvanced, Paulo_Alves, UsTaaa, xxJJxx
17864 Registered Users

Generated in 0.057 seconds in which 0.024 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