ATTENTION: You are viewing a page formatted for mobile devices; to view the full web page, click HERE.

Other Software > Developer's Corner

Question About Updating Logfile with PowerShell Add-Content

<< < (2/2)

4wd:
Just FYI, PowerShell doesn't care if you use \ or / in paths so you could try:

--- Code: PowerShell ---$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 ---Add-content "$LogFile -value $logline
To insert variables into a string you can try:


--- Code: PowerShell ---Add-content "$($LogFile)" -value $loglineThat 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 ---"$(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.

wraith808:
You can also try for grins to use a path with no spaces- and looking for that, I see that's the correct answer.
-wraith808 (January 31, 2022, 01:12 PM)
--- End quote ---

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...
-Stoic Joker (January 31, 2022, 03:09 PM)
--- End quote ---

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

Stoic Joker:
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.
-4wd (January 31, 2022, 04:12 PM)
--- End quote ---

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?
-wraith808 (January 31, 2022, 10:31 PM)
--- End quote ---

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:
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 ---$LogFile = "R:\te st\Me\NPM Change Log.txt"$ChangeType = "Added a line"$FullPath = "R:\te st\Me\NPM Change Log.txt" Set-Content "$($LogFile)" "This is a log file"$logline = "$(Get-Date), $ChangeType, $FullPath"Add-content "$($LogFile)" -value $logline# and again just in caseAdd-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-Stoic Joker (February 01, 2022, 08:03 AM)
--- End quote ---

Or maybe not  :P

Navigation

[0] Message Index

[*] Previous page

Go to full version