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.by Makovec at 2012-10-30 14:38:47
Here’s what I have cobbled together:
$computers = Get-Content -Path c:\computers-license.txt
ForEach ($SingleComp in $computers)
{
$dir = "\$SingleComp\c$\Users<username>\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
Hi,by dw_anderson at 2012-10-31 07:18:43
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
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.by Makovec at 2012-10-31 08:27:24$computers = Get-Content -Path c:\computers-license.txt
ForEach ($SingleComp in $computers)
{
$dir = "\$SingleComp\c$\Users<username>\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
I see that on line $license2 = $license += $SingleComp you are joining records that you want to split later. So something like:by dw_anderson at 2012-10-31 10:06:32$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
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
#}