Powershell Logging_Function导致无法解释的错误

时间:2016-03-23 10:49:15

标签: powershell logging

我设法通过将第53行更改为:

来消除该消息

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

这也允许删除Test-Path以上(感谢-Force),但我不明白为什么New-Item会在指定-ItemType File时尝试创建新文件夹,并且路径和名称都已提供......

我正在尝试使用Logging_Functions.ps1模块here

这就是我所说的:

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

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

根据文档。但是,在运行上述内容时,我看到了这个恼人的消息:

  

New-Item:文件'\ DC1 \ NETLOGON \ PSSubs'已经存在。   在\ 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

即使出现此错误,也会创建文件并且我不知道它为什么抱怨文件夹名称,因为第53行没有要求它创建文件夹。

当我在实际Logging_Functions.ps1文件中运行完全相同的行时,它可以正常运行。我已在下面添加了Log-Start函数(我在第53行稍作修改,将-Value替换为-Name,因为这似乎是正确的):

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 ""
  }
}

任何人都可以帮忙解释这里有什么区别吗?为什么一种方式导致错误而不是另一种方式,当他们据说做同样的事情时?谢谢!

1 个答案:

答案 0 :(得分:1)

你可以改变这一切:

#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

对于以下一个班轮,因为-Force将覆盖任何已存在的文件:

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