OMGWTFLOLBBQ

Text to Array

10 posts in this topic

Hello!

 I've been going through all of the help files and examples, but I feel lost. I can't seem to find what I need. Essentially, what I'm doing is simple(in my head).

Create a file based on the information needed, select a line in that file, then put that line back in DOS. My code is below, I know it's messy, but I'll take the heat if you can help.

 

;BitlockerUpdate
;2016.05.24

HotKeySet("{ESC}", "Terminate")

Run("C:\Windows\System32\CMD.exe") ;open command prompt
Sleep(500) ;sleep half second
if ProcessExists("cmd.exe") Then
   winactivate("Administrator: C:\Windows\System32\CMD.exe") ;refocus command prompt
   Send("C:")
   Send("{ENTER}")
   send("CD{SPACE}%windir%\sysnative\")
   send("{ENTER}")
EndIf

;create file with Bitlocker Protectors
Send("manage-bde.exe{SPACE}-protectors{SPACE}-get{space}c:{space}>{Space}%userprofile%\Desktop\protectors.txt") ;send code "manage-bde -protectors -get c: > %userprofile%\Desktop\protectors.txt"
Send("{ENTER}") ;send enter key to create text file on desktop with protectors
Sleep(1000) ;sleep one second(1s)


;edit txt file/create array, pull line and paste into new file, copy text then paste into cmd window below


;update data
if ProcessExists("cmd.exe") Then
   winactivate("Administrator: C:\Windows\System32\CMD.exe") ;refocus command prompt
   Send("manage-bde.exe{SPACE}-protectors{SPACE}-adbackup{SPACE}c:{SPACE}-id{SPACE}{COPIEDINFORMATIONGOESHERE}")
   Send("{ENTER}")
   ;don't forget to delete temp file if you create one created.
EndIf

;remove files
if ProcessExists("cmd.exe") Then
   winactivate("Administrator: C:\Windows\System32\CMD.exe") ;refocus command prompt
   Send("DEL{SPACE}%userprofile%\Desktop\protectors.txt") ;send code to delete protectors.txt
   Send("{ENTER}")
   ;don't forget to delete temp file if you create one created.
EndIf

MsgBox(0,"Done","Copy and upload are done. Please check Computer in AD.")

Func Terminate()
    Exit 0
EndFunc

 

The protectors.txt file looks like this:

BitLocker Drive Encryption: Configuration Tool version 6.1.7601
Copyright (C) Microsoft Corporation. All rights reserved.

Volume C: [OSDisk]
All Key Protectors

    TPM:
      ID: {0000000-0000-0000-0000-0000000}

    Numerical Password:
      ID: {1000000-1000-1000-1000-1000000}
      Password:
        000000-000000-000000-000000-000000-000000-000000

    Data Recovery Agent (Certificate Based):
      ID: {2000000-2000-2000-2000-2000000}
      Certificate Thumbprint:
        1111111a1aa1a1aa1a1a1a1aaaaaa1aaa1

I would specifically need the line after Numerical Password. 

Original line:      ID: {1000000-1000-1000-1000-1000000}

Needed information: -ID: {1000000-1000-1000-1000-1000000}

 

I apologize in advance for inconveniencing anyone with my silly issues. Hopefully this can stand as a great learning experience for both me and anyone else looking.

 

Share this post


Link to post
Share on other sites

@OMGWTFLOLBBQ I have moved your question to the appropriate forum, the Developers forum specifically states it is not for AutoIt-related questions. Please be mindful of where you post ;)

 


√-1 2^3 ∑ π, and it was delicious!

Share this post


Link to post
Share on other sites
Just now, JLogan3o13 said:

@OMGWTFLOLBBQ I have moved your question to the appropriate forum, the Developers forum specifically states it is not for AutoIt-related questions. Please be mindful of where you post ;)

 

Apologies for the mistake, I'll be more careful in the future.

Share this post


Link to post
Share on other sites

In answer to your question: First, have you tried using just Run or ShellExecute with the appropriate switches, rather than all the Sends? Something like so?

ShellExecute("manage-bde.exe", "-protectors -get " & @DesktopDir & "\protectors.txt")

 

Second: You could read the file to an array, and parse through it that way. Something like this:

#include <File.au3>

$sFile = @DesktopDir & "\Protectors.txt"
$aFile = FileReadToArray($sFile)


For $i = 0 To UBound($aFile)
    If StringInStr($aFile[$i], "Numerical Password:") Then MsgBox($MB_OK, "", $aFile[$i + 1])
Next

One of our RegEx gurus will come along and do that more quickly, I am sure, but that should get you started :)


√-1 2^3 ∑ π, and it was delicious!

Share this post


Link to post
Share on other sites
23 minutes ago, JLogan3o13 said:

In answer to your question: First, have you tried using just Run or ShellExecute with the appropriate switches, rather than all the Sends? Something like so?

ShellExecute("manage-bde.exe", "-protectors -get " & @DesktopDir & "\protectors.txt")

 

Second: You could read the file to an array, and parse through it that way. Something like this:

#include <File.au3>

$sFile = @DesktopDir & "\Protectors.txt"
$aFile = FileReadToArray($sFile)


For $i = 0 To UBound($aFile)
    If StringInStr($aFile[$i], "Numerical Password:") Then MsgBox($MB_OK, "", $aFile[$i + 1])
Next

One of our RegEx gurus will come along and do that more quickly, I am sure, but that should get you started :)

Thanks for the suggestions! Definitely got a few gears running. As for the first, I attempted ShellExecute, but it kept giving an error about "windows cannot find "manage-bde.exe". I know it's messy, but it seems to work...for now. After I get this working, I'll definitely work on streamlining it for the future.

 

Second, I've edited code based off of what you've suggested. 

$sFile = "%userprofile%\Desktop\protectors.txt"
$aFile = FileReadToArray($sFile)

For $i = 0 To UBound($aFile)
    If StringInStr($aFile[$i], "Numerical Password:") Then _ArrayToClip($MB_OK, "", $aFile[$i + 1])
Next

if ProcessExists("cmd.exe") Then
   winactivate("Administrator: C:\Windows\System32\CMD.exe") ;refocus command prompt
EndIf

;update data
if ProcessExists("cmd.exe") Then
   winactivate("Administrator: C:\Windows\System32\CMD.exe") ;refocus command prompt
   Send("manage-bde.exe{SPACE}-protectors{SPACE}-adbackup{SPACE}c:{SPACE}")
   Send("^v")
   Send("{ENTER}")
   ;don't forget to delete temp file if you create one created.
EndIf

I put that right in the middle, thinking I could skip the message box(which does appear to be showing the correct data), but nothing is pasting in with the Send("^V") command.... 

I appreciate the suggestions and I'll think this through for a bit tonight.

Share this post


Link to post
Share on other sites

I am guessing you don't want the entire array on the clipboard, just the one line right? Instead of _ArrayClip, do something like:

Local $sFile = "%userprofile%\Desktop\protectors.txt"
Local $aFile = FileReadToArray($sFile)
Local $sInfo

For $i = 0 To UBound($aFile)
    If StringInStr($aFile[$i], "Numerical Password:") Then $sInfo = $aFile[$i + 1]
Next

if ProcessExists("cmd.exe") Then
   winactivate("Administrator: C:\Windows\System32\CMD.exe") ;refocus command prompt
EndIf

;update data
if ProcessExists("cmd.exe") Then
   winactivate("Administrator: C:\Windows\System32\CMD.exe") ;refocus command prompt
   Send("manage-bde.exe{SPACE}-protectors{SPACE}-adbackup{SPACE}c:{SPACE}")
   Send($sInfo)
   Send("{ENTER}")
   ;don't forget to delete temp file if you create one created.
EndIf

 

1 person likes this

√-1 2^3 ∑ π, and it was delicious!

Share this post


Link to post
Share on other sites

This gets the needed info

$stxt = FileRead("@desktopdir & "\protectors.txt")
$sInfo = StringRegExp($stxt, 'Numerical Password:\s*(\N+)', 1)[0] 
Msgbox(0,"", $sInfo)

 

Share this post


Link to post
Share on other sites
18 hours ago, JLogan3o13 said:

I am guessing you don't want the entire array on the clipboard, just the one line right? Instead of _ArrayClip, do something like:

Local $sFile = "%userprofile%\Desktop\protectors.txt"
Local $aFile = FileReadToArray($sFile)
Local $sInfo

For $i = 0 To UBound($aFile)
    If StringInStr($aFile[$i], "Numerical Password:") Then $sInfo = $aFile[$i + 1]
Next

if ProcessExists("cmd.exe") Then
   winactivate("Administrator: C:\Windows\System32\CMD.exe") ;refocus command prompt
EndIf

;update data
if ProcessExists("cmd.exe") Then
   winactivate("Administrator: C:\Windows\System32\CMD.exe") ;refocus command prompt
   Send("manage-bde.exe{SPACE}-protectors{SPACE}-adbackup{SPACE}c:{SPACE}")
   Send($sInfo)
   Send("{ENTER}")
   ;don't forget to delete temp file if you create one created.
EndIf

 

Edited the whole bit... see below.

;BitlockerUpdate
;2016.05.25

#include <AutoItConstants.au3>
#include <Array.au3>
#include <File.au3>

;definitions
Local $pro = @DesktopDir & "\Protectors.txt" ;"%userprofile%\Desktop\protectors.txt" operating txt directory
Local $act =  WinActivate("Administrator: C:\Windows\System32\CMD.exe") ;refocus command prompt
Local $bde = "manage-bde.exe{SPACE}"
Local $arr = FileReadToArray($pro)
Local $inf

;open command and enter proper directory for use
Run("C:\Windows\System32\CMD.exe") ;open command prompt
Sleep(250) ;sleep quarter second
if ProcessExists("cmd.exe") Then
   $act ;refocus command prompt
   Send("C:" & "{ENTER}") ;send code "c:" then press enter
   Send("CD{SPACE}%windir%\sysnative\" & "{ENTER}") ;send code "CD %windir%\sysnative\" then press enter
   Send($bde & "-protectors{SPACE}-get{space}c:{space}>{Space}" & $pro & "{ENTER}") ;create protectors.txt on current desktop
  ;Sleep(250) ;sleep quarter second(.25s)
EndIf

;Needs Work
For $i = 0 To UBound($arr)
    If StringInStr($arr, "Numerical Password:") Then $inf = $arr[$i + 1]
Next

;update data
if ProcessExists("cmd.exe") Then
   winactivate("Administrator: C:\Windows\System32\CMD.exe") ;refocus command prompt
   Send($bde & "-protectors{SPACE}-adbackup{SPACE}c:{SPACE}")
   Send($inf & "{ENTER}")
   ;don't forget to delete temp file if you create one created.
EndIf
;remove files
if ProcessExists("cmd.exe") Then
   winactivate("Administrator: C:\Windows\System32\CMD.exe") ;refocus command prompt
   Send("DEL{SPACE}" & $pro & "{ENTER}") ;delete protectors.txt from current desktop
EndIf
;completed & confirm message box
MsgBox(0,"Done","Copy and upload are done. Please check Computer in AD.")

When I run the code, no errors appear...but nothing is pasted in after the " Send($inf  & "{Enter}") " code is applied after collecting the info from the array. Please see the attached image.

The line that should appear is: manage-bde.exe -protectors -adbackup c: -ID {1000000-1000-1000-1000-1000000} 

{1000000-1000-1000-1000-1000000}  is the variable though, it should be pulling the information from within protectors.txt from that line. 

I appreciate what you've assisted me with creating. Is it possible to get a simple definition of what you've done?

output.PNG

Share this post


Link to post
Share on other sites

Your For Loop is incorrect. Look at how I have mine written again. You need to look at $arr[$i] in your StringInStr command, not all of $arr.


√-1 2^3 ∑ π, and it was delicious!

Share this post


Link to post
Share on other sites

Here is an example script with a function that will return a PC's Numerical Password without having to automate the command prompt.  This function returns an array with the Numerical Password ID and Password.  I wrote it while working on a project to harvest BitLocker Numerical Passwords from our PCs.  You could use this function to as a template for creating a function to backup protectors to AD.  

#RequireAdmin
#include <WinAPIFiles.au3>

;Turn off redirection for a 32-bit script on 64-bit system.
If @OSArch = "X64" And Not @AutoItX64 Then _WinAPI_Wow64EnableWow64FsRedirection(False)

Global $vBitLockerProtectors = _BitLockerGetNumericalPassword()
If @error Then Exit MsgBox(16, "ERROR", StringReplace($vBitLockerProtectors, "  ", @CRLF))

If IsArray($vBitLockerProtectors) Then 
    MsgBox(0, "Numerical Password", "ID: " & $vBitLockerProtectors[0] & @CRLF & "Password: " & $vBitLockerProtectors[1])
EndIf


Func _BitLockerGetNumericalPassword($sComputerName = @ComputerName, $sDrive = "C:")

    Local $sManageBdeCmd = "manage-bde -protectors -get " & $sDrive & " -cn " & $sComputerName

    Local $iPIDMangeBde = Run($sManageBdeCmd, @SystemDir, @SW_HIDE, $STDERR_MERGED)
    ProcessWaitClose($iPIDMangeBde)

    Local $sManageBdeOutput = StringStripWS(StdoutRead($iPIDMangeBde), 3)
;~  ConsoleWrite($sManageBdeOutput & @CRLF & @CRLF) ;For testing.
;~  ConsoleWrite(StringReplace($sManageBdeOutput, @CRLF, "  ") & @CRLF & @CRLF) ;For testing.

    Local $aManageBdeOutput = StringRegExp($sManageBdeOutput, "(?s)Numerical Password:.+ID:\s+{(.+)}.+Password:\s+([-0-9]+)", $STR_REGEXPARRAYGLOBALMATCH)
    If @error Then
        Local $aManageBdeOutput[2] = ["", ""]
        Local $iError = 1
        If StringInStr($sManageBdeOutput, "ERROR: No key protectors found.") Then $iError = 2 ;Fail test, PC not encrypted.
        If StringInStr($sManageBdeOutput, "ERROR: An error occurred while connecting to the BitLocker management" & @CRLF & "interface.") Then $iError = 3 ;Fail test, PC does not exist or inaccessible.
        If StringInStr($sManageBdeOutput, "ERROR: An error occurred (code 0x80070057)") Then $iError = 4 ;Fail test, drive does not exist.
        If StringInStr($sManageBdeOutput, "ERROR: An attempt to access a required resource was denied.") Then $iError = 5 ;An attempt to access a required resource was denied.
;~      Return SetError($iError, 0, $aManageBdeOutput) ;Comment the next line, and uncomment this line to get a blank array output on error.  
        Return SetError($iError, 0, StringReplace($sManageBdeOutput, @CRLF, "  ")) ;Error text returned by manage-bde.  
    EndIf
;~  _ArrayDisplay($aManageBdeOutput) ;For Testing.

    Return $aManageBdeOutput
EndFunc

If you have any questions, please let me know.  

 

Adam

 

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

  • Similar Content

    • Ascer
      By Ascer
      1. Description
      Anti-bot system to avoid spam. Vector graphic style generates 4 random numbers from 0-9 and create a picture. 2. Requirements
      .NET Framework 1.1 - 4.5 (on this version Microsoft destroy old rules) 3. Possibilities.

      4. Downloads.
      Source package Capatcha.rar
    • Ascer
      By Ascer
      1. Description.
      Udf working with MSDN System.Collections.ArrayList. Allow you to make fast operations on huge arrays, speed is even x10 better than basic _ArrayAdd.  Not prefered for small arrays < 600 items. 2. Requirements
      .NET Framework 1.1 - 4.5 (on this version Microsoft destroy old rules) System Windows 3. Possibilities.
      ;=============================================================================================================== ; UDF Name: List.au3 ; ; Date: 2018-02-17, 10:52 ; Description: Simple udf to create System Collections as ArrayList and make multiple actions on them. ; ; Function(s): _ListCreate -> Creates a new list ; _ListCapacity -> Gets a list size in bytes ; _ListCount -> Gets items count in list ; _ListIsFixedSize -> Get bool if list if fixed size ; _ListIsReadOnly -> Get bool if list is read only ; _ListIsSynchronized -> Get bool if list is synchronized ; _ListGetItem -> Get item on index ; _ListSetItem -> Set item on index ; ; _ListAdd -> Add item at end of list ; _ListClear -> Remove all list items ; _ListClone -> Duplicate list in new var ; _ListContains -> Get bool if item is in list ; _ListGetHashCode -> Get hash code for list ; _ListGetRange -> Get list with items between indexs ; _ListIndexOf -> Get index of item ; _ListInsert -> Insert a new item on index ; _ListInsertRange -> Insert list into list on index ; _ListLastIndexOf -> Get index last of item ; _ListRemove -> Remove first found item ; _ListRemoveAt -> Remove item in index ; _ListRemoveRange -> Remove items between indexs ; _ListReverse -> Reverse all items in list ; _ListSetRange -> Set new value for items in range ; _ListSort -> Sort items in list (speed of reading) ; _ListToString -> Get list object name ; _ListTrimToSize -> Remove unused space in list ; ; Author(s): Ascer ;=============================================================================================================== 4. Downloads
      List.au3 5. Examples
      SpeedTest _ArrayAdd vs ListAdd SpeedTest ArraySearch vs ListIndexOf Basic usage - crating guild with members  
    • Valnurat
      By Valnurat
      How can I get combobox value by using  the ENTER key?
    • ronmage
      By ronmage
      So I have a loop that keeps reading data from an array and searching it for the same value. If the value is no there it does work then adds the value to the array to prevent it from doing the same work.
      If _ArraySearch($ID,$filearray[$i]) = -1 Then Work.... _ArrayAdd($ID,$filearray[$i]) EndIf This is in a for loop hence $i
      So what is happening is the code works great for several hours. After a period of time _ArraySearch($ID,$filearray[$i]) will result in -1 even if $ID = $filearray. So it ready as if there is no data in the array. Anyone have this problem? 
       
      Also I am just running in using F5 not compiling it and running it if that makes a difference.
       
    • vyperhand
      By vyperhand
      I'm struggling to launch a VBS file via autoit using RunWait.  Due to the nature of the deployment tool I am using for said script, I only know that the .vbs files will be in the same directory as my AutoIt-generated .exe, but not what that path will be.  The path will look something like this:
      c:\programdata\vendor\lots\of\folders\randomnumber
      This is generated during deployment and I have no way of predicting the path - therefore, I am not sure how to call back to "same directory" in order to successfully launch the .vbs.
      This line is as close as I have gotten - this fires off the cmd window, but it closes immediately with out information, and the .vbs is not launched.
      RunWait (@ComSpec & " /c" & 'cscript.exe WORKPLEASE.vbs ALL /Q /NoCancel') I thought /c might be the problem, but leaving out the /c element causes me not to even see the momentary CMD window flash by.  Any help will be greatly appreciated, and thanks in advance.