topbanner_forum
  *

avatar image

Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
  • Tuesday December 6, 2022, 6:18 pm
  • Proudly celebrating 15+ years online.
  • Donate now to become a lifetime supporting member of the site and get a non-expiring license key for all of our programs.
  • donate

Author Topic: Question About Updating Logfile with PowerShell Add-Content  (Read 2986 times)

Stoic Joker

  • Honorary Member
  • Joined in 2008
  • **
  • Posts: 6,643
    • View Profile
    • Donate to Member
So from what I'm reading, this is supposed to be simple - I'm just screwing it up somehow.

Issue I'm having is that if I use the full [path][filename] in the code it works fine. But if I try to store the path in a string - so I don't have to edit it 5 times if there is a change - logging fails and the script locks up.

I've tried every variation I could either find, or think of … But have as of yet, not found the correct answer.

Snippet of problem code below:
Code: PowerShell [Select]
  1. $logline = "$(Get-Date), $ChangeType, $FullPath"
  2.     Add-content "C:\Users\Me\Them\NPM Change Log.txt" -value $logline

Now ^that^ works fine, but what I need is something more like this:
Code: PowerShell [Select]
  1. $LogFile = "C:\Users\Me\Them\NPM Change Log.txt"
  2.    
  3.     $logline = "$(Get-Date), $ChangeType, $FullPath"
  4.     Add-content "$LogFile -value $logline

But I can't get it to work; double quotes, single quotes, with/without the -path parameter identifier - everything fails and locks the script.

Any Ideas on what I might be missing?

TIA

Stoic Joker

wraith808

  • Supporting Member
  • Joined in 2006
  • **
  • default avatar
  • Posts: 11,148
    • View Profile
    • Donate to Member
Re: Question About Updating Logfile with PowerShell Add-Content
« Reply #1 on: January 31, 2022, 11:00 AM »
Did you try \\? i.e.

Code: PowerShell [Select]
  1. $LogFile = "C:\\Users\\Me\\Them\\NPM Change Log.txt"
  2.    
  3.     $logline = "$(Get-Date), $ChangeType, $FullPath"
  4.     Add-content "$LogFile -value $logline

single \ is escaping the next character.

Stoic Joker

  • Honorary Member
  • Joined in 2008
  • **
  • Posts: 6,643
    • View Profile
    • Donate to Member
Re: Question About Updating Logfile with PowerShell Add-Content
« Reply #2 on: January 31, 2022, 01:09 PM »
Did you try \\? i.e.

Well I hadn't yet, but I just did - And no joy. :(

single \ is escaping the next character.

I recall that behavior from C++/C#/etc., but in PowerShell too? I've got other path strings in the script that don't seem bothered by it...is why I ask.

I generally don't do much with PowerShell unless I have to, so this project has predominantly consisted of staring at the screen in disbelief while "quietly" chanting a litany of obscenities'. :D

wraith808

  • Supporting Member
  • Joined in 2006
  • **
  • default avatar
  • Posts: 11,148
    • View Profile
    • Donate to Member
Re: Question About Updating Logfile with PowerShell Add-Content
« Reply #3 on: January 31, 2022, 01:12 PM »
Yeah, I can see now that it's not required - https://docs.microso...?view=powershell-7.2

So, what I did in order to do it and not worry about it (which is why I hadn't really notice) is captured the args into a variable. My paths are passed in as args from a batch file/commandline.

Worst case, you could do that.

You can also try for grins to use a path with no spaces- and looking for that, I see that's the correct answer.

https://stackoverflo...path-with-powershell

Use & to preface the string- it's like @ in c#.


Stoic Joker

  • Honorary Member
  • Joined in 2008
  • **
  • Posts: 6,643
    • View Profile
    • Donate to Member
Re: Question About Updating Logfile with PowerShell Add-Content
« Reply #4 on: January 31, 2022, 03:09 PM »
You can also try for grins to use a path with no spaces- and looking for that, I see that's the correct answer.

1. Target is buried in the middle of clients 1,500,000 files/folders file server...  :D
2. Just for fun and curiosity, I tried it - and it still didn't work. Which actually, is rather interesting.

(I used the inhouse lab copy for this) Taking all spaced out of both path and filename, still caused the script to fail (same as before) at the add-content command … regardless of what I did to the string I was trying to pass in. So it seems at this point to be an issue with add-content itself, and how it needs/wants to be spoon-fed the string.

Gaaaa! - It's almost 5:00pm - Happy Thoughts, happy thoughts...

4wd

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 5,591
    • View Profile
    • Donate to Member
Re: Question About Updating Logfile with PowerShell Add-Content
« Reply #5 on: January 31, 2022, 04:12 PM »
Just FYI, PowerShell doesn't care if you use \ or / in paths so you could try:
Code: PowerShell [Select]
  1. $LogFile = "C:/Users/Me/Them/NPM Change Log.txt"

To get around possibly having to escape \

Also, is it just a typo but you seem to have only one " in:

Code: PowerShell [Select]
  1. Add-content "$LogFile -value $logline

To insert variables into a string you can try:

Code: PowerShell [Select]
  1. Add-content "$($LogFile)" -value $logline
That should expand the variable within the quotes.

I'm on a tablet atm so can't test these, in a couple of hours I can have a play.

Addendum: IIRC I had a similar thing with Add-Content that I was doing and the result was that Add-Content was one of the very few PowerShell commandlets that couldn't take a file in a variable.

I modified my script to use Out-File instead, eg.

Code: PowerShell [Select]
  1. "$(Get-Date), $ChangeType, $FullPath" | Out-File $Logfile -Append

Or similar, you may have to play around with enclosing the file variable in quotes as above.
« Last Edit: January 31, 2022, 04:32 PM by 4wd »

wraith808

  • Supporting Member
  • Joined in 2006
  • **
  • default avatar
  • Posts: 11,148
    • View Profile
    • Donate to Member
Re: Question About Updating Logfile with PowerShell Add-Content
« Reply #6 on: January 31, 2022, 10:31 PM »
You can also try for grins to use a path with no spaces- and looking for that, I see that's the correct answer.

1. Target is buried in the middle of clients 1,500,000 files/folders file server...  :D
2. Just for fun and curiosity, I tried it - and it still didn't work. Which actually, is rather interesting.

(I used the inhouse lab copy for this) Taking all spaced out of both path and filename, still caused the script to fail (same as before) at the add-content command … regardless of what I did to the string I was trying to pass in. So it seems at this point to be an issue with add-content itself, and how it needs/wants to be spoon-fed the string.

Gaaaa! - It's almost 5:00pm - Happy Thoughts, happy thoughts...

Did you try prefacing it with the &? Did that work?

Stoic Joker

  • Honorary Member
  • Joined in 2008
  • **
  • Posts: 6,643
    • View Profile
    • Donate to Member
Re: Question About Updating Logfile with PowerShell Add-Content
« Reply #7 on: February 01, 2022, 08:03 AM »
Addendum: IIRC I had a similar thing with Add-Content that I was doing and the result was that Add-Content was one of the very few PowerShell commandlets that couldn't take a file in a variable.

Ah-ha-ha! Given the inane shenanigans I've gone through trying to get this to work. That is the conclusion I was coming too - And your recollection is way gooder than mine. :D

Thanks, I'll go a different route with this then.


Did you try prefacing it with the &? Did that work?

That one made the script lock/crash on load. I tried everything from your link, and a few variations on what was suggested, but to no avail. So I'm going with the (4wd confirmed) assertion that the Add-Content commandlet (I can never remember that term) is just broken like that or something.

Thanks guys!

4wd

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 5,591
    • View Profile
    • Donate to Member
Re: Question About Updating Logfile with PowerShell Add-Content
« Reply #8 on: February 03, 2022, 01:13 AM »
So anyway the following works in Powershell 5.1 (included with Windows) and Powershell 7 (to be included in Windows at some point):

Code: PowerShell [Select]
  1. $LogFile = "R:\te st\Me\NPM Change Log.txt"
  2. $ChangeType = "Added a line"
  3. $FullPath = "R:\te st\Me\NPM Change Log.txt"
  4.  
  5. Set-Content "$($LogFile)" "This is a log file"
  6. $logline = "$(Get-Date), $ChangeType, $FullPath"
  7. Add-content "$($LogFile)" -value $logline
  8. # and again just in case
  9. Add-Content "$($LogFile)" "$(Get-Date), $($ChangeType), $($FullPath)"

FWIW, I normally always put variables in $() in strings that are within double quotes these days, then I'm sure the variables will be expanded.

And your recollection is way gooder than mine. :D

Or maybe not  :P
« Last Edit: February 03, 2022, 01:18 AM by 4wd »