Passing value of variable into get-content

by Lery at 2013-04-16 16:30:52

I am looking to get the contents of a log file. This log file could be anywhere on an endpoint. Therefore, I first need to go look in the registry for the location. This is easily accomplished with my friend and yours get-itemproperty. Once I have the value of that in my variable, I wanted to pass it along to get-content. This is where I’m having problems.

$getlog = get-itemproperty -path “HKLM:\SOFTWARE\Vendor” -Name FilePath

Ok, so now we have the value, which in this example is c:\programdata\vendor\log.log

I was hoping this would work

get-content -path $getlog.FilePath\log.log

It does not. I’ve tried different ways and they all seem to fail for me. I’ve used single quotes, double quotes, etc. etc.

Would anyone know the correct format for what I’m trying to do?
by poshoholic at 2013-04-16 16:48:16
Hi there,

Try this approach instead:
Get-Content -Path "$($getlog.FilePath)\log.log"
Also note that if log.log is part of the FilePath value you are working with, you don’t even need to do that. You could just do this instead:
Get-Content -Path $getlog.FilePath
by Lery at 2013-04-16 17:37:33
Thank you poshoholic! Worked perfectly. The syntax was driving me nuts!

The code you helped me with will return log.log. If I have several .log files that increment from log.log - log20.log I would have to create some kind of loop, yes? For example, I might want to just return log.log or I might want to return 5, 10, or all of the .log files.

To address your comment about the log.log being part of the filepath value. Unfortunately, no, it’s not. FilePath ony contains the location for the log files.
by poshoholic at 2013-04-16 19:21:14
Ok, so it’s actually better in this case since the log file isn’t in the path because you could create a loop to get the content from one or many, as you indicate. Here are a few possibilities:
# Get contents from log1.log, log2.log, … up to log10.log
for ($i = 1; $i -le 10; $i++) {
Get-Content -Path “$($getlog.FilePath)\log${i}.log”

# Get contents from all log*.log files
Get-Content -Path "$($getlog.FilePath)log*.log"
by poshoholic at 2013-04-16 19:24:23
Also, just FYI, inside of double-quotation marks, $() is a subexpression. When you see that, anything inside of the $() enclosures will be invoked and the results will be returned to the string. You’ll also notice ${} used inside the double-quotes in my follow-up above. That is simply a variable to be evaluated in the string. The curly braces are not necessary in this case, however I have gotten into the practice of using curly braces for regular variable evaluations because it makes it easier for me to identify the portions of a double-quoted string that are dynamic. Without the curly braces, it might look to someone else like you are trying to reference the log property on the i variable (e.g. if it were like this “$i.log”).
by Lery at 2013-04-17 08:01:30
Thank you so much Kirk! Very valuable information that helps me learn. Do you have any suggestions for figuring out the correct syntax besides bugging everyone around here? :slight_smile: I know that I can look up the help on get-content. However, the help never indicated what you gave me Get-Content -Path “$($getlog.FilePath)\log.log” So I’m looking for advice on how to figure that sort of stuff out? It seems like the simple stuff is all well documented. Once I want to do something outside the box, so to say, it gets very complicated.

As for your code for looking through the multiple log files, that is just sweet. Thanks again.
by poshoholic at 2013-04-17 08:23:41
Have you reviewed some of the documentation in the about_* files?
# List all PowerShell help files
Get-Help -Category HelpFile

There’s also search (which is more useful once you know the terminology such as “subexpression”):
# Find help documentation about subexpressions
Get-Help subexpression

Note that search won’t always get you what you are looking for. From the search above, you can see what a subexpression is by invoking this:
Get-Help about_Operators
However, that doesn’t describe how they work in strings, so sometimes you need to poke around a little. Here’s a better help file describing strings and how they work in PowerShell in much more detail, and this is where you’ll see examples showing subexpressions inside of double-quoted strings (plus a lot more, with details about single- vs. double-quoted strings, plus here strings as well):
Get-Help about_Quotes
That’s a good bit of information to get you started. When I started learning PowerShell years ago, I relied 100% on the built-in docs for the first 4-6 months so that I could learn how to use PowerShell to learn PowerShell. There are lots of great books out there that explain what is in the docs and more, but knowing how to get the information you need from the language itself is incredibly valuable.