Créer un fichier journal dans Powershell

J’ai le code ci-dessous et actuellement il charge toutes les informations à l’écran. Je veux qu’il se connecte à un fichier journal sur D: \ Apps \ Logs.

Le fichier journal doit avoir le nom de l’ordinateur sur lequel il est chargé – afin que COMPUTERNAME.log

Une idée de comment je peux faire ça?

Merci

$computer = gc env:computername $onetcp = ((get-childitem c:\windows\system32\drivers\tcpip.sys).Versioninfo.ProductMajorPart).tossortingng() $twotcp = ((get-childitem c:\windows\system32\drivers\tcpip.sys).Versioninfo.ProductMinorPart).tossortingng() $threetcp = ((get-childitem c:\windows\system32\drivers\tcpip.sys).Versioninfo.ProductBuildPart).tossortingng() $fourtcp = ((get-childitem c:\windows\system32\drivers\tcpip.sys).Versioninfo.ProductPrivatePart).tossortingng() $onedfsr = ((get-childitem c:\windows\system32\dfsrs.exe).Versioninfo.ProductMajorPart).tossortingng() $twodfsr = ((get-childitem c:\windows\system32\dfsrs.exe).Versioninfo.ProductMinorPart).tossortingng() $threedfsr = ((get-childitem c:\windows\system32\dfsrs.exe).Versioninfo.ProductBuildPart).tossortingng() $fourdfsr = ((get-childitem c:\windows\system32\dfsrs.exe).Versioninfo.ProductPrivatePart).tossortingng() write-host TCPIP.sys Version on $computer is: "$onetcp.$twotcp.$threetcp.$fourtcp" Write-Host write-host DFSRS.exe Version on $computer is: "$onedfsr.$twodfsr.$threedfsr.$fourdfsr" Write-Host If (get-wmiobject win32_share | where-object {$_.Name -eq "REMINST"}) { Write-Host "The REMINST share exists on $computer" } Else { Write-Host "The REMINST share DOES NOT exist on $computer - Please create as per standards" } Write-Host $hotfix1 = Get-HotFix -Id KB2450944 -ErrorAction SilentlyContinue $hotfix2 = Get-HotFix -Id KB2582284 -ErrorAction SilentlyContinue $hotfix3 = Get-HotFix -Id KB979808 -ErrorAction SilentlyContinue If ($hotfix1) { Write-Host "Hotfix KB2450944 is installed" -BackgroundColor Green -ForegroundColor Black } else { Write-Host "Hotfix KB2450944 is NOT installed - Please ensure you install this hotfix" -ForegroundColor "red" } If ($hotfix2) { Write-Host "Hotfix KB2582284 is installed" -BackgroundColor Green -ForegroundColor Black } else { Write-Host "Hotfix KB2582284 is NOT installed - Please ensure you install this hotfix" -ForegroundColor "red" } If ($hotfix3) { Write-Host "Hotfix KB979808 is installed" -BackgroundColor Green -ForegroundColor Black } else { Write-Host "Hotfix KB979808 is NOT installed - Please ensure you install this hotfix" -ForegroundColor "red" } 

Mettez ceci en haut de votre fichier:

 $Logfile = "D:\Apps\Logs\$(gc env:computername).log" Function LogWrite { Param ([ssortingng]$logssortingng) Add-content $Logfile -value $logssortingng } 

Remplacez ensuite vos appels Write-host par LogWrite .

Une fonction qui prend ces principes un peu plus loin.

  1. Ajouter les horodatages – ne peut pas avoir de journal sans horodatage.
  2. Ajoutez un niveau (utilise INFO par défaut), ce qui signifie que vous pouvez mettre en évidence de gros problèmes.
  3. Permet une sortie de console optionnelle. Si vous ne définissez pas de destination pour le journal, il le vide simplement.

     Function Write-Log { [CmdletBinding()] Param( [Parameter(Mandatory=$False)] [ValidateSet("INFO","WARN","ERROR","FATAL","DEBUG")] [Ssortingng] $Level = "INFO", [Parameter(Mandatory=$True)] [ssortingng] $Message, [Parameter(Mandatory=$False)] [ssortingng] $logfile ) $Stamp = (Get-Date).toSsortingng("yyyy/MM/dd HH:mm:ss") $Line = "$Stamp $Level $Message" If($logfile) { Add-Content $logfile -Value $Line } Else { Write-Output $Line } } 

Gist avec rotation des journaux: https://gist.github.com/barsv/85c93b599a763206f47aec150fb41ca0

Usage:

 . .\logger.ps1 Write-Log "debug message" Write-Log "info message" "INFO" 

En utilisant ce cadre de Log-Entry :

Scénario:

 Function Main { Log -File "D:\Apps\Logs\$Env:computername.log" $tcp = (get-childitem c:\windows\system32\drivers\tcpip.sys).Versioninfo.ProductVersionRaw $dfs = (get-childitem C:\Windows\Microsoft.NET\Framework\v2.0.50727\dfsvc.exe).Versioninfo.ProductVersionRaw Log "TCPIP.sys Version on $computer is:" $tcp Log "DFSVC.exe Version on $computer is:" $dfs If (get-wmiobject win32_share | where-object {$_.Name -eq "REMINST"}) {Log "The REMINST share exists on $computer"} Else {Log "The REMINST share DOES NOT exist on $computer - Please create as per standards"} "KB2450944", "KB3150513", "KB3176935" | ForEach { $hotfix = Get-HotFix -Id $_ -ErrorAction SilentlyContinue If ($hotfix) {Log -Color Green Hotfix $_ is installed} Else {Log -Color Red Hotfix $_ " is NOT installed - Please ensure you install this hotfix"} } } 

Sortie écran: Sortie d'écran

Fichier journal (dans D:\Apps\Logs\.log ):

 2017-05-31 Write-Log (version: 01.00.02, PowerShell version: 5.1.14393.1198) 19:19:29.00 C:\Users\User\PowerShell\Write-Log\Check.ps1 19:19:29.47 TCPIP.sys Version on is: {Major: 10, Minor: 0, Build: 14393, Revision: 1066, MajorRevision: 0, MinorRevision: 1066} 19:19:29.50 DFSVC.exe Version on is: {Major: 2, Minor: 0, Build: 50727, Revision: 8745, MajorRevision: 0, MinorRevision: 8745} 19:19:29.60 The REMINST share DOES NOT exist on - Please create as per standards Error at 25,13: Cannot find the requested hotfix on the 'localhost' computer. Verify the input and run the command again. 19:19:33.41 Hotfix KB2450944 is NOT installed - Please ensure you install this hotfix 19:19:37.03 Hotfix KB3150513 is installed 19:19:40.77 Hotfix KB3176935 is installed 19:19:40.77 End