0

I am using Write-EventLog to log some file not found errors, where the error text includes the path. And it works great other than the automatic link creation seems to be stuck in the 90's. So any path that has a space is broken when you look at it in Event Viewer. Is there a way to force the link creation to work with modern paths? Or even force a particular event to not auto-create links? Or do I need to just not include paths? FWIW I have to deal with PS2.0 as well as later.

EDIT: here is a little code stub that I just verified exhibits this behavior in Windows 7/PS2.0

$eventParameters = @{
    logName = 'Application'
    source = 'Test'
    entryType = 'Error'
    eventID = 1
    message = "This is broken \\C:\Folder Name"
}

New-EventLog –logName:'Application' –source:'Test'
Write-EventLog @eventParameters

And these are broken too

message = 'This is broken "\\C:\Folder Name"'
message = 'This is broken \\Server\Folder Name'

But this "works", in that no link is created, so the space can't break it.

message = 'This is not broken C:\Folder Name'

So is the issue just that Event Viewer can't cope with UNC paths?

Gordon
  • 6,257
  • 6
  • 36
  • 89
  • What do you mean "modern paths"? URLs aren't supposed to contain spaces. If they do, replace the spaces with `%20`. – Ansgar Wiechers Oct 07 '16 at 14:28
  • Possible duplicate of [How do I replace spaces with %20 in PowerShell](http://stackoverflow.com/questions/23548386/how-do-i-replace-spaces-with-20-in-powershell) – TessellatingHeckler Oct 07 '16 at 14:56
  • Not URLs, local network paths. \\Server\Folder Name\... etc. All sorts of old Microsoft tools couldn't handle spaces, and usually you deal with it by putting double quotes around the part that needs to have spaces. But even that doesn't seem to work as I tried escaped double quotes in a double quoted string (because I also have variables that need expanded) and still the link is created up to the first space, and thus points at nothing meaningful. – Gordon Oct 07 '16 at 15:18
  • Hae you tried wrapping it in quotes twice. '"C:\Folder with\Space"' – Bob007 Oct 07 '16 at 15:32
  • No, but I tried "`" & """ and neither worked. In both cases the quotes show up in the event message, and the link is still broken at the first space. – Gordon Oct 07 '16 at 16:33
  • You have to share the code that is causing the problem. – user4317867 Oct 07 '16 at 16:37
  • It looks like you're trying to use the UNC path format with a local path; that's not possible. Please see https://msdn.microsoft.com/en-us/library/gg465305.aspx . If you were to use '\\ComputerName\c$\folder name', I think it would work. I can't see how '\\C:\Folder Name' would work anywhere. – jbsmith Oct 07 '16 at 17:07
  • Yeah, that was a mistake. But as you see in the revised info, it looks like it's correctly done UNC paths are broken, while local paths are not. Well, kinda not, in that they don't create shortcuts, so the shortcut isn't broken. I think it's time to admit Microsoft just doesn't have it's shit together and move on with no paths of any kind. It would be a nice "feature" to have a link to the folder where the files are supposed to be, but not that important. – Gordon Oct 07 '16 at 17:32
  • You can convert the paths to short filenames by getting the output of `for %I in ("C:\Folder Name") do echo %~sI`. See http://stackoverflow.com/questions/4051088/how-to-get-dos-path-instead-of-windows-path – Tony Hinkle Oct 07 '16 at 19:12

1 Answers1

0

Replace all space with %20

Prefix local file path with file:/// (if you only use file:// the hyperlink will cover the whole text but Event Viewer won't correctly invoke it)

"This is working file:///C:\Temp\file%20with%20space.txt"

Replace UNC path start \\ with file://

"This also works file://MACHINE\SHARE\file%20with%20space.txt"

You can achieve the result with a single line of c#

new Uri(Path.GetFullPath(local_or_unc_path)).AbsoluteUri

Or powershell

(New-Object System.Uri ([System.IO.Path]::GetFullPath($local_or_unc_path))).AbsoluteUri
Lawrence Ward
  • 549
  • 1
  • 5
  • 17