在 Powershell 中创建日志文件 [英] Create Log File in Powershell

查看:28
本文介绍了在 Powershell 中创建日志文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码,目前它会在屏幕上加载所有信息.我希望它登录到 D:\Apps\Logs 上的日志文件.

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.

日志文件需要有它正在加载的计算机的名称 - 所以 COMPUTERNAME.log

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

知道我该怎么做吗?

谢谢

$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"    }

推荐答案

把这个放在文件的顶部:

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
}

然后将您的 Write-host 调用替换为 LogWrite.

Then replace your Write-host calls with LogWrite.

这篇关于在 Powershell 中创建日志文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆