by matttilford at 2013-04-23 14:38:46
Ok, a quick bit of background. We have AutoDesk network licences that we can view the current usage of using a program called JTB FlexReport. I am using it to output a html file which i am then reading into powershell so i can see which program is using the licence (mainly Revit or AutoCAD) but also which version.by mjolinor at 2013-04-23 15:07:26
The html output is mostly a standard html table.$ReportApp = "JTB FlexReport LT.exe"
$ReportAppOpt = "/h"
Start-Process $ReportApp $ReportAppOpt
sleep -seconds 10
$file = Get-Content "$env:localappdata/Temp/JTBFlexReportLT.htm" | select-string "@" | Select-String "td"
$file | ForEach-Object {
$out = $file -replace "<td nowrap>",""
$out = $out -replace "</td>",""
$out = $out -split "@"
}
$pclist =@()
$out | ForEach-Object {
if ($_ -match "PE-"){
$pclist += $
}
}
$pclist = $pclist | sort -Unique
$results = Invoke-Command -ComputerName $pclist -ScriptBlock {
Get-Process -Name Revit, Acad -FileVersionInfo | select -Property FileName
}
$results | Out-GridView
The split on the @ is because the result is given as username@computername, so then loop through the data to get the computer names. I also need to get the unique entries as each entry will be in at least twice: once for the version (i.e. BDS 2013) and once for the overall count.
What i’m mainly wondering is if there is a better way to handle the string? Most of the script is getting data from a line such as<td nowrap>abc@PE-PC-01</td>
Am i missing some obvious way of working with the string i’m given? Also my apologies for $, i’m still working with some PS2 on Server 2003 so i’ve not moved to $PSItem yet.
It would help to see the original string.by poshoholic at 2013-04-23 21:04:52
Also, you don’t need to apologize for using $.by matttilford at 2013-04-24 02:25:14
t’s still supported, and the odds your scripts will run on both v2 and v3 go up when you use $, and down when you use $PSItem. I’ll be using $_ for a long time yet.
Yeah i agree i wasn’t very clear in my original post. The Get-Content extracts the lines i need to work with i.eby mjolinor at 2013-04-24 03:26:25<td nowrap>abc@PE-PC-01</td>
What i’m looking at is the next 10 lines of code that are there just to extract the computer name from that string. Am i missing a trick or is that the best way to handle it?
Try this:by matttilford at 2013-04-24 09:03:43$regex = '\s*<td nowrap>.+?@(.+)</td>\s*'
$pclist = (Get-Content "$env:localappdata/Temp/JTBFlexReportLT.htm") -match $regex -replace $regex,'$1'
The -match and -replace operators will work on both an array and on a scalar (single object). When you use -match against a single string, it returns True/False. If you use it against an array, it returns all the elements of the array that match. You can also chain those opertions together in a single command line.
The parens around the get-content will produce an array of all the lines, the -match filters out all the lines you don’t want, and the -replace gets rid of all the text you don’t want in the lines that did match.
You are a legend, not only does it work perfectly i even understand why! As an added bonus i piped that into sort -unique which got rid of another line.by eisenbergz at 2013-04-24 22:36:04
Could you explain the $1 ? I don’t see where that object comes from. Thanks.by matttilford at 2013-04-25 03:24:01
I had to quickly remind myself what it was, but i have used it before. Basically it’s the bit inside the brackets, so $0 is the whole string and $1 is the bit inside the first brackets. If i wanted to get the username from that string as well i could change the regexby eisenbergz at 2013-04-25 05:12:33$regex = ‘\s*<td nowrap>(.+?)@(.+)</td>\s*’
So then $1 would be the username and $2 would be the computer name.
Makes sense. Does that variable persist until the end of the script?