1

I managed to get rid of the message by changing line 53 to :

New-Item -Path $sFullPath -ItemType File -Force

Which also allows removal of the Test-Path above this (thanks to -Force), But I don't understand why New-Item would try to create a new folder when -ItemType File has been specified, AND the path and name have both been provided ...


I am trying to use Logging_Functions.ps1 Module which I got here.

This is how I am calling it:

."\\DC1\NETLOGON\PSSubs\Logging_Functions.ps1"

Log-Start -LogPath "\\DC1\NETLOGON\PSSubs" -LogName "Test_Log.log" -ScriptVersion "1.5"

As per the documentation. However, when running the above I am seeing this annoying message:

New-Item : The file '\DC1\NETLOGON\PSSubs' already exists. At \DC1\NETLOGON\PSSubs\Logging_Functions.ps1:53 char:5 + New-Item -Path $LogPath -Value $LogName -ItemType File + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : WriteError: (\DC1\NETLOGON\PSSubs:String) [New-Item], IOException + FullyQualifiedErrorId : NewItemIOError,Microsoft.PowerShell.Commands.NewItemCommand

Even though this error appears, the file is created and I have no idea why it is complaining about the folder name, as line 53 is not asking it to create the folder.

When I run the exact same line inside the actual Logging_Functions.ps1 file, it works without error. I've included the Log-Start function below (which I altered slightly on line 53 to replace -Value with -Name as this seems correct):

Function Log-Start{    
  [CmdletBinding()]

  Param (
    [Parameter(Mandatory=$true)][string]$LogPath, 
    [Parameter(Mandatory=$true)][string]$LogName, 
    [Parameter(Mandatory=$true)][string]$ScriptVersion
    )

  Process{
    $sFullPath = $LogPath + "\" + $LogName

    #Check if file exists and delete if it does
    If((Test-Path -Path $sFullPath)){
      Remove-Item -Path $sFullPath -Force
    }

    #Create file and start logging
    New-Item -Path $LogPath -Name $LogName -ItemType File

    Add-Content -Path $sFullPath -Value "***************************************************************************************************"
    Add-Content -Path $sFullPath -Value "Started processing at [$([DateTime]::Now)]."
    Add-Content -Path $sFullPath -Value "***************************************************************************************************"
    Add-Content -Path $sFullPath -Value ""
    Add-Content -Path $sFullPath -Value "Running script version [$ScriptVersion]."
    Add-Content -Path $sFullPath -Value ""
    Add-Content -Path $sFullPath -Value "***************************************************************************************************"
    Add-Content -Path $sFullPath -Value ""

    #Write to screen for debug mode
    Write-Debug "***************************************************************************************************"
    Write-Debug "Started processing at [$([DateTime]::Now)]."
    Write-Debug "***************************************************************************************************"
    Write-Debug ""
    Write-Debug "Running script version [$ScriptVersion]."
    Write-Debug ""
    Write-Debug "***************************************************************************************************"
    Write-Debug ""
  }
}

Can anyone please help explain what the difference is here? Why would one way cause an error and not the other, when they are supposedly doing the exact same thing? Thanks!

2
  • 1
    That does seem odd at first glance. It should work the same. What PowerShell version are you running so I can try to replicate this? Commented Mar 23, 2016 at 11:45
  • @Matt $PSVersionTable.PSVersion >Major Minor Build Revision ----- ----- ----- -------- 3 0 -1 -1 Commented Mar 23, 2016 at 13:51

1 Answer 1

1

You can change all of this:

#Check if file exists and delete if it does
If((Test-Path -Path $sFullPath)){
  Remove-Item -Path $sFullPath -Force
}

#Create file and start logging
New-Item -Path $LogPath -Name $LogName -ItemType File

To the following one liner, as the -Force will overwrite any file that already exists:

New-Item -Path $sFullPath -ItemType File -Force 
Sign up to request clarification or add additional context in comments.

1 Comment

Yes, the op is aware of this but he is not asking for this as a fix. This is good extra information though.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.