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!
$PSVersionTable.PSVersion>Major Minor Build Revision ----- ----- ----- -------- 3 0 -1 -1