4

I have a logging module that I wrote that write various lines to a file using Out-File -Append. I'm in the process of writing some scripts and modules for SCCM. Most of the SCCM functions / commands don't allow you to target a specific provider / location through a parameter.

This means that I need to switch to the 'CMSite' provider to run the majority of the commands.

The problem I'm having is that if I switch to the 'CMSite' provider, it breaks my logging for some reason, resulting in the error:

PSLogging.psm1 (144, 15): ERROR: At Line: 144 char: 15
ERROR: +             $logline | Out-File -Append -Encoding utf8 -FilePath $logFullPath;
ERROR: +                        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ERROR:     + CategoryInfo          : InvalidArgument: (:) [Out-File], PSInvalidOperationException
ERROR:     + FullyQualifiedErrorId : ReadWriteFileNotFileSystemProvider,Microsoft.PowerShell.Commands.OutFileCommand
ERROR: Out-File : Cannot open file because the current provider (AdminUI.PS.Provider\CMSite) cannot open a file.

Even though I'm providing the full path in the Out-File parameters. The error is obviously because the CMSite provider is not a filesystem provider but I thought providing the full path would be context sensitive and use the correct provider automatically?

If I do a:

Set-Location C:

Before logging anything it works fine.

One thing I've thought is that the logging location is actually a UNC path (that we control with a global variable), maybe it's not switching the provider correctly as it's not a 'PSDrive' provider?

Am I missing something or do I needed to 'Set-Location' and switch PSDrives every single time I want to run a logging command to a file?

mhouston100
  • 1,173
  • 2
  • 19
  • 41
  • I can only replicate this when not providing a full path. How have you confirmed `$logFullPath` contains the full drive and directory details? – xXhRQ8sD2L7Z Dec 21 '16 at 01:13
  • By both outputting the variable and because it works if I change the PSDrive (my logging module records the log path within the log) – mhouston100 Dec 21 '16 at 01:16

1 Answers1

4

Yeah UNC paths need a little help being defined as FileSystem providers. Try this:

$logline | Out-File -Append -Encoding utf8 -FilePath ('FileSystem::' + $logFullPath)

Edit: More info PowerShell Gotcha: UNC paths and Providers

Patrick Meinecke
  • 3,963
  • 2
  • 18
  • 26
  • You are a genius. One tiny modification to my logging module and it works like a charm. I was scratching my head over that for way longer than I should have been. Thanks! – mhouston100 Dec 21 '16 at 01:20