Powershell SCCM PSDrive and Out-File -Path parameter resulting error

3k views Asked by At

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?

1

There are 1 answers

1
Patrick Meinecke On BEST ANSWER

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