Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create Log File in Powershell

Tags:

powershell

I have the below code and currently it loads all the information on screen. I want it to log to a log file on D:\Apps\Logs.

The log file needs to have the name of the computer it is loading against - so COMPUTERNAME.log

Any idea how I can do this?

Thanks

$computer = gc env:computername  $onetcp = ((get-childitem c:\windows\system32\drivers\tcpip.sys).Versioninfo.ProductMajorPart).tostring() $twotcp = ((get-childitem c:\windows\system32\drivers\tcpip.sys).Versioninfo.ProductMinorPart).tostring() $threetcp = ((get-childitem c:\windows\system32\drivers\tcpip.sys).Versioninfo.ProductBuildPart).tostring() $fourtcp = ((get-childitem c:\windows\system32\drivers\tcpip.sys).Versioninfo.ProductPrivatePart).tostring()   $onedfsr = ((get-childitem c:\windows\system32\dfsrs.exe).Versioninfo.ProductMajorPart).tostring() $twodfsr = ((get-childitem c:\windows\system32\dfsrs.exe).Versioninfo.ProductMinorPart).tostring() $threedfsr = ((get-childitem c:\windows\system32\dfsrs.exe).Versioninfo.ProductBuildPart).tostring() $fourdfsr = ((get-childitem c:\windows\system32\dfsrs.exe).Versioninfo.ProductPrivatePart).tostring()  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"    } 
like image 980
lara400 Avatar asked Oct 20 '11 10:10

lara400


People also ask

How do I create a log file in PowerShell?

PowerShell has a built-in transcript feature to save all commands and outputs shown in the PS console to a text log file. To log your current PowerShell session, the Start-Transcript cmdlet is used. The –Append option indicates that new sessions will be logged to the end of the file (without overwriting it).

How do I create a log file using date and time in PowerShell?

PowerShell create log file with date Basically, it will create the file with the file name as of today's date. The script will check if any file exists with name as today's date, if the file exists, then it will delete will first delete the file, else it will create the file and write the log message into it.


1 Answers

Put this at the top of your file:

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

Then replace your Write-host calls with LogWrite.

like image 100
JNK Avatar answered Sep 20 '22 21:09

JNK