extracting license key and formatting into 2 columns

This topic contains 0 replies, has 1 voice, and was last updated by Profile photo of Forums Archives Forums Archives 5 years, 5 months ago.

  • Author
    Posts
  • #5248

    by dw_anderson at 2012-10-30 12:35:34

    I am a powershell noob. I would like to extract a license key from a log and output it and the computer name to a file with 2 columns. Also I'm not sure how to extract only the 15 character license key which is always on the ninth line of the log file at the end of the line.

    Here's what I have cobbled together:

    $computers = Get-Content -Path c:\computers-license.txt

    ForEach ($SingleComp in $computers)
    {
    $dir = "\\$SingleComp\c$\Users\\AppData\Roaming\Eyemaginations.LUMA\manager\logs"
    $file = Get-ChildItem $dir | sort lastwritetime | select -Last 1

    $fullpath = Join-Path $dir $file
    $license = Get-Content $fullpath | Select-String "license" -SimpleMatch | select-object line

    $license2 = $license += $SingleComp

    $license2 | Out-File -width 130 -append c:\license-keys.txt

    Thanks-
    Drew

    by Makovec at 2012-10-30 14:38:47

    Hi,
    Would be great if you can paste snipet of the file. Before then, just few tips:

    1) You can have 9th line of file using this code: (Get-Content c]
    2) Your $file variable already contains property FullName, you can use it instead of Join-Path $dir $file
    3) You can extract last 15 chars from string this way: $text.Substring($text.Length-15, 15)

    Hope it helps a bit. Let me know if you need more.

    David

    by dw_anderson at 2012-10-31 07:18:43

    Thanks David, I made changes based on your advice. Here's the updated code and result. It's very close to what I need. Now I just need to split the license and computer name into 2 columns, or just insert a few spaces between the fields.

    $computers = Get-Content -Path c:\computers-license.txt

    ForEach ($SingleComp in $computers)
    {
    $dir = "\\$SingleComp\c$\Users\\AppData\Roaming\Eyemaginations.LUMA\manager\logs"
    $file = Get-ChildItem $dir | sort lastwritetime | select -Last 1

    $fullpath = Join-Path $dir $file
    $LicenseString = (Get-Content $fullpath)[8]

    $license = $LicenseString.substring($LicenseString.length-18, 18)

    $license2 = $license += $SingleComp

    $license2 | Out-File -width 130 -append c]

    Here is the output c:\license-keys.txt. The 2 computer names are ld-drew and ld-00782.

    a074000000Nxxxxxxxxld-drew
    a074000000Nxxxxxxx1ld-00782

    by Makovec at 2012-10-31 08:27:24

    I see that on line $license2 = $license += $SingleComp you are joining records that you want to split later. So something like:
    $license2 = "$license $SingleComp"should work for you. Just place anything you want between the two variables. Maybe to add comma, so your output file will be like CSV file for later import to Excel for example.

    David

    by dw_anderson at 2012-10-31 10:06:32

    Thanks again David!

    by dw_anderson at 2012-11-02 12:31:57

    Could someone suggest a way to log errors to a file if the directory on the remote computer doesn't exist? I tried making some script blocks but didn't get anywhere. I wanted to add something like this but I'm not sure how to get the script blocks correct.

    {
    #"ERROR - folder not there" | add-content $ErrorLog
    #}

You must be logged in to reply to this topic.