Link to home
Start Free TrialLog in
Avatar of Alex
AlexFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Parsing text file in powershell

Morning all,

So, I have this text file which is a currently used licence file from LMStat

Users of 64300ACD_F:  (Total of 20 licenses issued;  Total of 5 licenses in use)

What I need to do, is parse out just this single line so I can put that into a location I can then do some graphs out of.... More importantly, everything between "(total" and "use)"

I'm going to have it run every hour and add to the file, so is it possible to section out that area....

I tried looking at Select-string but couldn't get it working :(

Cheers

Alex
Avatar of Bill Prew
Bill Prew

So what do you want in the output file, and do you want it in a CSV format?  Perhaps date, time, issued, in-use?  What about the "64300ACD_F", does that need to be reported?  And how should that line be selected from the file, would looking for "64300ACD_F" be sufficient, or are there other lines containing that that need to be skipped?  Can you provide a larger sample of the input file, and an example of the output file?

~bp
Set the $LMStat variable in line 1 to whatever generates the string(s) you need to parse; strings not matching the format will be discarded:
$LMStat = 'Users of 64300ACD_F:  (Total of 20 licenses issued;  Total of 5 licenses in use)'
$Date = Get-Date
$LMStat | ForEach-Object {
	If ($_ -match 'Users of (?<Name>[^:]*?):\s*\(Total of\s+(?<Issued>\d+)\s+licenses issued;\s*Total of\s+(?<Used>\d+)\s+licenses in use') {
		'' | Select-Object -Property `
			@{n='Date'; e={$Date}},
			@{n='Name'; e={$Matches['Name']}},
			@{n='Issued'; e={$Matches['Issued']}},
			@{n='Used'; e={$Matches['Used']}}
	}
}

Open in new window

$someText = Get-Content $PathToSolutionFile
$someText| Select-String "Total.*use" -AllMatches | Foreach-Object {$_.Matches.Value}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Alex

ASKER

OK,

So we're getting there, what i've basically been tasked to do is make it so we can get a yearly report on how many licences are being used over the course of a year.

The above file is what the LMutil application pushes out, the content of said file will actually change (namely how many licences are in use)

So, if I do the following


$LMStat = 'get-content c:\temp\serverstat.txt [14..15]'
$OutFile = 'C:\Temp\LMStat.csv'
$Date = Get-Date
$LMStat | ForEach-Object {
	If ($_ -match 'Users of (?<Name>[^:]*?):\s*\(Total of\s+(?<Issued>\d+)\s+licenses issued;\s*Total of\s+(?<Used>\d+)\s+licenses in use') {
		'' | Select-Object -Property `
			@{n='Date'; e={$Date}},
			@{n='Name'; e={$Matches['Name']}},
			@{n='Issued'; e={$Matches['Issued']}},
			@{n='Used'; e={$Matches['Used']}}
	}
} | Export-Csv -NoTypeInformation -Path $OutFile -Append

Open in new window


That should, in theory get me what I need right?
The first line would need to be something like this:
$LMStat = (Get-Content 'c:\temp\serverstat.txt')[14..15]

Open in new window

Since I don't know the contents of said file, I can't judge what happens. The script will only process lines that match the format you specified, any additional lines will just be skipped, so you might not even need to pick lines from the output.
What is the lmutil command you're using? If the command writes to stdout, you wouldn't even need the temporary file.
Avatar of Alex

ASKER

lmutil - Copyright (c) 1989-2014 Flexera Software LLC. All Rights Reserved.
Flexible License Manager status on Mon 1/23/2017 09:15

[Detecting lmgrd processes...]
License server status: 27000@UK*******
    License file(s) on UK******* C:\Program Files\Autodesk Network License Manager\Company 110216.lic:

UK*******: license server UP (MASTER) v11.12.1

Vendor daemon status (on UK********):

  adskflex: UP v11.12.1
Feature usage info:

Users of 64300A**_F:  (Total of 20 licenses issued;  Total of 5 licenses in use)

Now, we really don't need to worry about anything after that, I just need "Users of 64300A**_F:  (Total of 20 licenses issued;  Total of 5 licenses in use)" that line, which can change (the numbers change) and dropped into a CSV file on a new line each time the script runs
As I said: the script doesn't care about lines not matching the pattern, you can just pass the full file (unless you have lines that match the format, but which you don't want to process, but those could probably be filtered out, too ...). Processing time saved by picking individual lines is negligible.

But what I meant with "What is the lmutil command you're using?" is the actual command line you're using to generate the temporary file, not its output.
Avatar of Alex

ASKER

Legend!