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条)