Home > Software design >  How to read a string from multiple notepad files and pass those values into a command using for lo
How to read a string from multiple notepad files and pass those values into a command using for lo

Time:01-17

I have the device ID of my drives captured into a text file and I am able to read each ID and pass into a command as a variable and generate a .bin.sup files.

Again I am able to open the .bin.sup files using notepad , but not able to store the values from the multiple notepad files and pass it as variable in another command. 0I have a software, which needs certain inputs to generate the log. This is the final piece I am stuck at.

My code:

$filePath = "C:\Users\Administrator\Documents\tools\DeviceNames.txt"

foreach ($line in Get-Content $filePath ) {
    Write-Host "drive info of $line"
    C:\Users\Administrator\Desktop\soft\software.exe -K -o zsup -f "${line}.bin" -n $line
    C:\Python27\python.exe tss.py --sup="" "${line}.bin"
      
}

The above piece generates .bin and then .bin.sup files

Now, I used notepad to read the .bin.sup files using a loop within (may be I am wrong).

foreach ($line in Get-Content $filePath ) {
    Write-Host "Unlocking drive $line"
    C:\Users\Administrator\Desktop\soft\software.exe -K -o zsup -f "${line}.bin" -n $line
    C:\Python27\python.exe tss.py --sup="" "${line}.bin"

    
    foreach ($line in Get-Content $filePath ) {
        $code = start "C:\Program Files (x86)\Notepad  \notepad  .exe" "${line}.bin.sup"    
        .\software.exe -K -o supauth -p ${code} -n $line
        }
}

In the 2nd for-loop above, I tried storing the value in a variable $code and tried to pass it into a command but its not working. It just opens all the notepad and fails to run the next cmd

The below line is able to open all the .bin.sup files using notepad but not able to store into $code (may be its not knowing which value to store).

start "C:\Program Files (x86)\Notepad  \notepad  .exe" "${line}.bin.sup"

FYI: Each output from .bin.sup looks like (random unique string) this - "dsf34fsdfjksh45fef9843f"

My goal: To read and store value from below command

start "C:\Program Files (x86)\Notepad  \notepad  .exe" "${line}.bin.sup"

and pass each value into below cmd as many times as the bin.sup files are there, using a for loop:

.\software.exe -K -o supauth -p ${code} -n $line

Hope my question is clear. Please help. Thanks in advance

CodePudding user response:

So it sounds like after you run the first 2 commands you now have a .bin.sup file that contains a code that you need to plug into the last command.

The first issue is that your 2nd foreach is nested inside your first which would result in looping through all the lines in filepath for every line in file path (if that makes sense).

Rather than running another foreach loop you can just add the new steps to the end of your existing foreach loop.

2nd issue is that you are trying to read the sup files using Notepad back into PowerShell which isn't possible. Fortunately PowerShell provides a way to read the contents of files (which you've already used at the beginning of your code): Get-Content

foreach ($line in Get-Content $filePath ) {
    Write-Host "Unlocking drive $line"
    C:\Users\Administrator\Desktop\soft\software.exe -K -o zsup -f "$line.bin" -n $line
    C:\Python27\python.exe tss.py --sup="" "$line.bin"

    # Retrive the code from the .bin.sup file
    $code = Get-Content -Path "$line.bin.sup"    
    .\software.exe -K -o supauth -p $code -n $line
}
  • Related