Jump to content
Sign in to follow this  
gcue

converting a script from vb

Recommended Posts

gcue

Hello world =)

I am trying to convert this script to autoit - im pretty close but still not working.  the script pulls bitlocker recovery passwords from an active directory workstation object.  (special thanks to water for the AD UDF which made it easy to get the object's "distinguishedname"!)

original script:

' --------------------------------------------------------------------------------
' Securely access the Active Directory computer object using Kerberos
' --------------------------------------------------------------------------------
 
Set objDSO = GetObject("LDAP:")
strPathToComputer = "LDAP:/cn=Smith,ou=Sales,dc=MyDomain,dc=com" ;path really has 2 // but to get around forum bug i posted using 1
 
'WScript.Echo "Accessing object: " + strPathToComputer
Const ADS_SECURE_AUTHENTICATION = 1
Const ADS_USE_SEALING = 64 '0x40
Const ADS_USE_SIGNING = 128 '0x80
 
' --------------------------------------------------------------------------------
' Get all BitLocker recovery information from the Active Directory computer object
' --------------------------------------------------------------------------------
' Get all the recovery information child objects of the computer object
Set objFveInfos = objDSO.OpenDSObject(strPathToComputer, vbNullString, vbNullString, _
                                   ADS_SECURE_AUTHENTICATION + ADS_USE_SEALING + ADS_USE_SIGNING)
objFveInfos.Filter = Array("msFVE-RecoveryInformation")
' Iterate through each recovery information object
For Each objFveInfo in objFveInfos
   strName = objFveInfo.Get("name")
   strRecoveryPassword = objFveInfo.Get("msFVE-RecoveryPassword")
   'WScript.echo  
   'WScript.echo "name: " + strName 
   'WScript.echo "msFVE-RecoveryPassword: " + strRecoveryPassword  
Next
 
WScript.echo "msFVE-RecoveryPassword: " + strRecoveryPassword  
 
WScript.Quit

what i have so far:

#include <ad.au3>
#include <array.au3>
 
$msg_normal = 0
 
$asset = @ComputerName
 
;~ $oMyError = ObjEvent("AutoIt.Error", "MyErrFunc") ; Install a custom error handler
 
_AD_Open()
$array = _AD_GetObjectProperties($asset & "$", "distinguishedname")
If @error Then Debug("error")
_AD_Close()
 
$strPathToComputer = "LDAP:/" & $array[1][1] ;path really has 2 // but to get around forum bug i posted using 1
 
Const $ADS_SECURE_AUTHENTICATION = 1;"&h1";
Const $ADS_USE_SEALING = 64;"&h40";0x40;
Const $ADS_USE_SIGNING = 128;"&h80";0x80;
 
$objDSO = ObjGet("LDAP:")
 
;~ $iSecurity = BitOr($ADS_SECURE_AUTHENTICATION, $ADS_USE_SEALING,$ADS_USE_SIGNING)
$iSecurity = $ADS_SECURE_AUTHENTICATION + $ADS_USE_SEALING + $ADS_USE_SIGNING
 
;~ $null = Chr(0)
Local $oVBS = ObjCreate("ScriptControl")
$oVBS.language = "VBScript"
Global Const $NULL = $oVBS.eval("vbNullString")
$oVBS = 0
 
$objFveInfos = $objDSO.OpenDSObject($strPathToComputer, $NULL, $NULL, $iSecurity)
 
If @error Then Debug(@error)
 
Dim $filter[1] = ["msFVE-RecoveryInformation"]
 
$objDSO.filter = $filter
 
For $objFveInfo In $objFveInfos
$strName = $objFveInfo.Get("name")
Debug($strName)
$strRecoveryPassword = $objFveInfo.Get("msFVE-RecoveryPassword")
Next
 
Debug($strRecoveryPassword)
 
;~ Func MyErrFunc()
;~  $HexNumber = Hex($oMyError.number, 8)
;~  MsgBox(0, "", "We intercepted a COM Error !" & @CRLF & _
;~  "Number is: " & $HexNumber & @CRLF & _
;~  "Windescription is: " & $oMyError.windescription)
 
;~  SetError(1) ; something to check for when this function returns
;~ EndFunc   ;==>MyErrFunc
 
 
Func Debug($variable1 = "", $variable2 = "", $variable3 = "")
 
If IsArray($variable1) Then
_ArrayDisplay($variable1)
Else
If $variable2 <> "" Then
$variable1 &= @CRLF & $variable2
EndIf
 
If $variable3 <> "" Then
$variable1 &= @CRLF & $variable3
EndIf
 
ClipPut($variable1)
MsgBox($msg_normal, "Debug", $variable1)
EndIf
 
EndFunc   ;==>Debug
Edited by gcue

Share this post


Link to post
Share on other sites
gcue

sorry had some issues with posting the original post (due to a bug in the forums when trying using : / / (without spaces) and took me forever to figure out and eventually post)

nasty but desperate 

$temp_file = @TempDir & "\bitlocker_recovery.vbs"
 
If FileExists($temp_file) Then
FileDelete($temp_file)
EndIf
 
$text = 'Set objDSO = GetObject("LDAP:")' & @LF
$text &= 'strPathToComputer = "' & $strPathToComputer & '"' & @LF
$text &= '' & @LF
$text &= 'Const ADS_SECURE_AUTHENTICATION = 1' & @LF
$text &= 'Const ADS_USE_SEALING = 64' & @LF
$text &= 'Const ADS_USE_SIGNING = 128' & @LF
$text &= '' & @LF
$text &= 'Set objFveInfos = objDSO.OpenDSObject(strPathToComputer, vbNullString, vbNullString, _' & @LF
$text &= '                         ADS_SECURE_AUTHENTICATION + ADS_USE_SEALING + ADS_USE_SIGNING)' & @LF
$text &= 'objFveInfos.Filter = Array("msFVE-RecoveryInformation")' & @LF
$text &= 'For Each objFveInfo in objFveInfos' & @LF
$text &= '   strName = objFveInfo.Get("name")' & @LF
$text &= '   strRecoveryPassword = objFveInfo.Get("msFVE-RecoveryPassword")' & @LF
$text &= 'Next' & @LF
$text &= '' & @LF
$text &= 'WScript.echo "msFVE-RecoveryPassword: " + strRecoveryPassword' & @LF
$text &= 'WScript.Quit' & @LF
 
FileWrite($temp_file, $text)
 
Local $foo = Run(@ComSpec & " /c cscript " & $temp_file, "", @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD)
 
Local $line
 
While 1
$line = StdoutRead($foo)
If @error Then ExitLoop
 
If $line <> "" Then
$array = StringRegExp($line, 'msFVE-RecoveryPassword: ([\w\W]*)', 3)
If Not @error Then
$recovery_password = $array[0]
EndIf
EndIf
WEnd
 
FileDelete($temp_file)
Edited by gcue

Share this post


Link to post
Share on other sites
BrewManNH

You should convert the VBScript to AutoIt, not use AutoIt to write the script to disk and run it. The syntax of the 2 languages are very similar and the conversion very straight-forward.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites
gcue

couldnt agree with you more

i tried to convert it - my first post is the furthest i got

Edited by gcue

Share this post


Link to post
Share on other sites
water

I would try this with the latest Beta version of AutoIt:

; --------------------------------------------------------------------------------
; Securely access the Active Directory computer object using Kerberos
; --------------------------------------------------------------------------------
 
Global $objDSO = ObjGet("LDAP:")
Global $strPathToComputer = "LDAP:/cn=Smith,ou=Sales,dc=MyDomain,dc=com" ;path really has 2 // but to get around forum bug i posted using 1
 
; WScript.Echo "Accessing object: " + strPathToComputer
Global Const $ADS_SECURE_AUTHENTICATION = 1
Global Const $ADS_USE_SEALING = 64 ; 0x40
Global Const $ADS_USE_SIGNING = 128 ; 0x80
 
; --------------------------------------------------------------------------------
; Get all BitLocker recovery information from the Active Directory computer object
; --------------------------------------------------------------------------------
; Get all the recovery information child objects of the computer object
Global $objFveInfos = $objDSO.OpenDSObject($strPathToComputer, Null, Null, BitOr($ADS_SECURE_AUTHENTICATION, $ADS_USE_SEALING, $ADS_USE_SIGNING))
Global $TempArray[] = ["msFVE-RecoveryInformation"]
$objFveInfos.Filter = $TempArray
; Iterate through each recovery information object
For $objFveInfo in $objFveInfos
   $strName = $objFveInfo.Get("name")
   $strRecoveryPassword = $objFveInfo.Get("msFVE-RecoveryPassword" & @LF)
   ConsoleWrite("  name: " & $strName)
   ConsoleWrite("  msFVE-RecoveryPassword: " & $strRecoveryPassword & @LF)  
Next
Edited by water

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-01-27 - Version 1.3.3.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites
water

OK, lets add some debugging code:

; --------------------------------------------------------------------------------
; Securely access the Active Directory computer object using Kerberos
; --------------------------------------------------------------------------------
 
Global $objDSO = ObjGet("LDAP:")
ConsoleWrite("ObjGet: @error = " & @error & ", @extended = " & @extended & @LF)
Global $strPathToComputer = "LDAP:/cn=Smith,ou=Sales,dc=MyDomain,dc=com" ;path really has 2 // but to get around forum bug i posted using 1
 
; WScript.Echo "Accessing object: " + strPathToComputer
Global Const $ADS_SECURE_AUTHENTICATION = 1
Global Const $ADS_USE_SEALING = 64 ; 0x40
Global Const $ADS_USE_SIGNING = 128 ; 0x80
 
; --------------------------------------------------------------------------------
; Get all BitLocker recovery information from the Active Directory computer object
; --------------------------------------------------------------------------------
; Get all the recovery information child objects of the computer object
Global $objFveInfos = $objDSO.OpenDSObject($strPathToComputer, Null, Null, BitOr($ADS_SECURE_AUTHENTICATION, $ADS_USE_SEALING, $ADS_USE_SIGNING))
ConsoleWrite("OpenDSObject: @error = " & @error & ", @extended = " & @extended & @LF)
Global $TempArray[] = ["msFVE-RecoveryInformation"]
$objFveInfos.Filter = $TempArray
ConsoleWrite("Filter: @error = " & @error & ", @extended = " & @extended & @LF)
; Iterate through each recovery information object
For $objFveInfo in $objFveInfos
   $strName = $objFveInfo.Get("name")
   ConsoleWrite("Get(name): @error = " & @error & ", @extended = " & @extended & @LF)
   $strRecoveryPassword = $objFveInfo.Get("msFVE-RecoveryPassword" & @LF)
   ConsoleWrite("ObjGet(msFVE-RecoveryPassword): @error = " & @error & ", @extended = " & @extended & @LF)
   ConsoleWrite("  name: " & $strName)
   ConsoleWrite("  msFVE-RecoveryPassword: " & $strRecoveryPassword & @LF)  
Next

Can you post the complete output from the SciTE console pane?

Edited by water

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-01-27 - Version 1.3.3.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites
gcue


>"C:\Program Files\AutoIt3\SciTE\AutoIt3Wrapper\AutoIt3Wrapper.exe" /run /prod /ErrorStdOut /in "C:scriptstest.au3" /UserParams    
+>07:05:17 Starting AutoIt3Wrapper v.2.1.2.9    Environment(Language:0409  Keyboard:00000409  OS:WIN_XP/Service Pack 3  CPU:X64 OS:X86)
>Running AU3Check (1.54.22.0)  from:C:Program FilesAutoIt3
+>07:05:17 AU3Check ended.rc:0
>Running:(3.3.8.1):C:Program FilesAutoIt3autoit3.exe "C:scriptstest.au3"    
--> Press Ctrl+Alt+F5 to Restart or Ctrl+Break to Stop
ObjGet: @error = 0, @extended = 0
C:scriptstest.au3 (37) : ==> The requested action with this object has failed.:
Global $objFveInfos = $objDSO.OpenDSObject($strPathToComputer, Null, Null, BitOr($ADS_SECURE_AUTHENTICATION, $ADS_USE_SEALING, $ADS_USE_SIGNING))
Global $objFveInfos = $objDSO.OpenDSObject($strPathToComputer, Null, Null, BitOr($ADS_SECURE_AUTHENTICATION, $ADS_USE_SEALING, $ADS_USE_SIGNING))^ ERROR
->07:05:18 AutoIt3.exe ended.rc:1
>Exit code: 1    Time: 1.499

 

Edited by gcue

Share this post


Link to post
Share on other sites
BrewManNH

You're not using the Beta version as mentioned in post 5, Null doesn't work prior to that.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites
gcue

i am.  i think the output doesnt show the beta?

but i do have 3.3.9.21 installed

Share this post


Link to post
Share on other sites
water

Press Alt+F5 to run the beta vesion of AutoIt.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-01-27 - Version 1.3.3.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites
gcue


>"C:\Program Files\AutoIt3\SciTE\AutoIt3Wrapper\AutoIt3Wrapper.exe" /run /beta /ErrorStdOut /in "C:scriptstest.au3" /UserParams    
+>07:17:45 Starting AutoIt3Wrapper v.2.1.2.9    Environment(Language:0409  Keyboard:00000409  OS:WIN_XP/Service Pack 3  CPU:X64 OS:X86)
-> Skipping AU3check: Current version doesn't support the AutoIt3 v 3.3.9.5+ syntax.
>Running:(3.3.9.21):C:\Program Files\AutoIt3\Beta\autoit3.exe "C:scriptstest.au3"    
--> Press Ctrl+Alt+F5 to Restart or Ctrl+Break to Stop
ObjGet: @error = 0, @extended = 0
OpenDSObject: @error = -2147352567, @extended = 0
"C:scriptstest.au3" (40) : ==> Variable must be of type "Object".:
$objFveInfos.Filter = $TempArray
$objFveInfos^ ERROR
->07:17:45 AutoIt3.exe ended.rc:1
>Exit code: 1    Time: 0.966

Share this post


Link to post
Share on other sites
water

Seems to be a connection problem.
Here it works as soon as I replace

Global $objFveInfos = $objDSO.OpenDSObject($strPathToComputer, "", "", BitOr($ADS_SECURE_AUTHENTICATION, $ADS_USE_SEALING, $ADS_USE_SIGNING))

with

Global $objFveInfos = ObjGet($strPathToComputer)

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-01-27 - Version 1.3.3.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites
gcue


>"C:\Program Files\AutoIt3\SciTE\AutoIt3Wrapper\AutoIt3Wrapper.exe" /run /beta /ErrorStdOut /in "C:scriptstest.au3" /UserParams    
+>07:35:37 Starting AutoIt3Wrapper v.2.1.2.9    Environment(Language:0409  Keyboard:00000409  OS:WIN_XP/Service Pack 3  CPU:X64 OS:X86)
-> Skipping AU3check: Current version doesn't support the AutoIt3 v 3.3.9.5+ syntax.
>Running:(3.3.9.21):C:\Program Files\AutoIt3\Beta\autoit3.exe "C:scriptstest.au3"    
--> Press Ctrl+Alt+F5 to Restart or Ctrl+Break to Stop
ObjGet: @error = 0, @extended = 0
OpenDSObject: @error = -2147463168, @extended = 0
"C:scriptstest.au3" (41) : ==> Variable must be of type "Object".:
$objFveInfos.Filter = $TempArray
$objFveInfos^ ERROR
->07:35:37 AutoIt3.exe ended.rc:1
>Exit code: 1    Time: 0.627

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×