powershell - Efficient way to use a logging function on multiple scripts - Stack Overflow

I did some research about this and I didn't find an easier method to make my function available in

I did some research about this and I didn't find an easier method to make my function available in our scripts.

We are a team of 6 people, 3 of us are writing scripts, most of the time it's me.

To unify the logs, I created a function to write them in a network folder with one file per script.

$logsFileDate = Get-Date -Format "yyyyMMdd" # 20240101
$logsDate= Get-Date -Format "dd-MM-yyyy" # 01-01-2024
$logsTime = Get-Date -Format "HH:mm:ss" # 13:01:01

function writeLogs{
    param (
        $logsFileName,
        $logsText
    )

    $logsFolder = "[networkPath]\Logs\"
    $logsFileNameCSV = $logsFileName+".csv"
    $firstCSVLine = "logsDate,logsTime,text"

    $logsFile = Join-Path -Path $logsFolder -ChildPath $logsFileNameCSV

    if (-not (Test-Path $logsFile)){
        try {
            New-Item -Path $logsFolder -ItemType Directory -Force
        } catch {
            Write-Host "Erreur lors de la création du dossier : $_.Exception.Message"
        }

        try {
            New-Item -Path $logsFolder -Name $logsFileNameCSV -ItemType "file"
        } catch {
            Write-Host "Erreur lors de la création du dossier : $_.Exception.Message"
        }
        Add-Content -Path $logsFile -Value $firstCSVLine
        Write-Host "$firstCSVLine"
        Write-Host "Le fichier a été créé avec Succès"
    } else {
        Write-Host "Le fichier existe déjà "
    }

    $logsText = $logsDate + "," + $logsTime + ","+ $logsText

    Add-Content -Path $logsFile -Value $logsText

    return $logsFile
}

and to use it, I call it like this :

. [Network]\Scripts\VERN-Logs.ps1
$scriptName = $MyInvocation.MyCommand.Name.Split(".")[0]

Is there a more efficient way to be able to call that function but without linking the script ?

A way to declare this function on our network, anything I could push with GPOs ?

PS : I know the script isn't the best, I have a new version coming, just want to take care of this part first :)

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745303504a4621578.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信