Function 我需要为此函数自动编写日志

Function 我需要为此函数自动编写日志,function,powershell,logging,parameters,Function,Powershell,Logging,Parameters,我得到了下面的函数write log,我想将运行脚本时生成的日志放在一个子文件夹中,并将它们重命名为file.logs.old。此外,我想保留脚本文件夹中的最后一个日志。范例 最后日志: C:\scripts\powershellscript\logs.log 子文件夹旧日志: C:\scripts\powershellscript\logfolder\ 旧日志: C:\scripts\powershellscript\logfolder\ C:\scripts\powershellscr

我得到了下面的函数write log,我想将运行脚本时生成的日志放在一个子文件夹中,并将它们重命名为file.logs.old。此外,我想保留脚本文件夹中的最后一个日志。范例

最后日志:

C:\scripts\powershellscript\logs.log
子文件夹旧日志:

C:\scripts\powershellscript\logfolder\
旧日志:

C:\scripts\powershellscript\logfolder\
C:\scripts\powershellscript\logfolder\logs1.log.old
C:\scripts\powershellscript\logfolder\logs2.log.old
C:\scripts\powershellscript\logfolder\logs3.log.old
Etc...
我有以下功能,但它只保存最后一个日志并覆盖它:

function write-Log
{
    Param (
        [Parameter(Mandatory = $false)]
        #Message
        $Message,

        [Parameter(Mandatory = $false)]
        #Errormessage
        $ErrorMessage,

        [Parameter(Mandatory = $false)]
        #Component that create the error
        $Component,

        [Parameter(Mandatory = $false)]
        #Error Type
        [int]$Type,

        [Parameter(Mandatory = $true)]
        #Filepath to the Logfile
        $LogFile
    )

    #Create a Timestamp
    $Date = Get-Date -Format "MM/dd/yyyy"
    $time = Get-Date -Format "HH:mm:ss.fff"

    #If you type a error message the type will automatically switch to type 3 Error
    if ($null -ne $ErrorMessage)
    { $Type = 3
    }
    #If you type no Component the Component will set to space
    if ($null -eq $Component)
    { $Component = " "
    }
    #If you set the type variable it is set to 1 automatically
    if ($null -eq $Type)
    { $Type = 1
    }

    #create the log entry
    $LogMessage = "<![LOG[$Message  $ErrorMessage ]LOG]!>" + "<time=`"$time`" date=`"$Date`" component=`"$Component`" context=`"`" type=`"$Type`" thread=`"`" file=`"`">"
    #write the log entry
    $LogMessage | Out-File -Append -Encoding UTF8 -FilePath $LogFile

}
我怎样才能做到这一点?我正在寻找需要添加到用户查询中的此函数的帮助。脚本正在工作,但只追加最后一个日志。对于检查旧日志来说,长期的检查是不可管理的


谢谢

您想移动哪些日志?您刚刚创建的还是以前创建的

要移动文件,可以使用
mv
命令

mv log old_log_folder/old_log
也许我不明白你的要求。

最好的是,

该解决方案对我有效,截至目前,我保留以下内容:

#Variable
#Add date to the log file name and date
$day = Get-Date -Format dd
$month = Get-Date -Format MM
$year = Get-Date -Format yyyy
$hour = Get-Date -Format HH
$minute = Get-Date -Format mm
$hoy = "$day" + "$month" + "$year" + "$hour" + "$minute"
New-Item C:\Scripts\Logs\Script-$hoy.log -type file -force
#Add end date
这样,使用timestamp和forcenewitem,我总是在手动创建的logs文件夹中有一个新的日志文件


不过,我们仍然希望有更好的解决方案。:)

我想你可能会发现这个模块很有用-

使用安装模块PoShLog安装它

然后,您可以按如下方式设置新的文件记录器:

Import-Module PoShLog

# Setup new logger
New-Logger |
    Set-MinimumLevel -Value Verbose |
    Add-SinkFile -Path 'C:\Scripts\Logs\Script-.log' -RollingInterval Minute  | # Add logging into file
    Start-Logger

# Try some logging methods
Write-InfoLog 'Some information to be logged...'

try {
    ConvertFrom-Json 'xxx'
}
catch {
    Write-ErrorLog 'Error occured!' -ErrorRecord $_
}

Close-Logger

这将每分钟创建一个新的日志文件,但您可以使用参数更改间隔,还可以限制日志目录中的文件数或文件大小,详细信息。在上面的示例中,我只展示了两种日志方法,但有两种


我希望这是有帮助的,如果你有任何问题,请查看或联系我。快乐编码

[1]编写并测试代码以执行您想要的操作。[2] 将其转换为函数。[3] 将该func添加到脚本的开头。[4] 在之前或在
Write Log
函数中调用它来处理该过程。嗨@Lee_Dailey,谢谢你的回答。我已经有了一个工作脚本,但它只保存了一个日志文件。我要寻找的是在函数中自动创建日志,以便将最后一个日志文件保存在脚本文件夹中,并将旧日志文件移动到子文件夹中进行存档。有什么方法可以进一步扩展我在文章中为这个范围添加的功能吗?据我所知,您提到的所有步骤1、2、3、4都正常工作,但只保存一个日志文件。我指的是要添加到您的代码中的附加函数。[grin]在写入之前,我会[A]检查现有文件的大小或日期。[B] 如果文件太大/太旧,请使用时间戳重命名该文件。//另一个解决方案是每天创建一个新文件。类似于-'是今天的文件吗?用它!如果不是从今天开始,用今天的日期做一个新的“-似乎涵盖了这个想法。。。而且它既简单又相当健壮。@Lee_Dailey,感谢您在这方面的投入。我发布了一些对我来说目前还不错的东西。你好,斯塔克,这也是我想的!很高兴看到您能在需要时使用它![grin]我正在构建一个脚本,为了将来使用,我想将“旧日志文件”移动到此文件夹,并仅保留脚本文件夹中的最后一个。这需要添加到函数中,以便在脚本期间完成该过程。我已经更新了帖子。我不想手动操作,而是在函数中构建它。这是一个很好的选择,尽管它不是很简单,需要开发人员/社区支持才能运行。