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

(1/2) > >>

Stoic Joker:
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 ---$logline = "$(Get-Date), $ChangeType, $FullPath"    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 ---$LogFile = "C:\Users\Me\Them\NPM Change Log.txt"        $logline = "$(Get-Date), $ChangeType, $FullPath"    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:
Did you try \\? i.e.


--- Code: PowerShell ---$LogFile = "C:\\Users\\Me\\Them\\NPM Change Log.txt"        $logline = "$(Get-Date), $ChangeType, $FullPath"    Add-content "$LogFile -value $logline
single \ is escaping the next character.

Stoic Joker:
Did you try \\? i.e. -wraith808 (January 31, 2022, 11:00 AM)
--- End quote ---

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

single \ is escaping the next character.-wraith808 (January 31, 2022, 11:00 AM)
--- End quote ---

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:
Yeah, I can see now that it's not required - https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_variables?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://stackoverflow.com/questions/18537098/spaces-cause-split-in-path-with-powershell

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

Stoic Joker:
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...

Navigation

[0] Message Index

[#] Next page

Go to full version