在 Powershell中创建日志文件

我有下面的代码,目前它加载屏幕上的所有信息。我希望它记录到 D:\ Apps\ Logs 上的日志文件。

我有的代码,目前它加载屏幕上的所有信息。我希望它记录到 D:\ Apps\ Logs 上的日志文件。

日志文件需要有它加载的计算机的名称-所以 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"    }
160

把这个放在你的文件的顶部:

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

然后用LogWrite替换您的Write-host调用。

82

一个将这些原则更进一步的函数。

添加的时间戳-不能有一个日志没有时间戳。

Add 是一个级别(默认情况下使用 INFO),这意味着您可以突出显示大问题。

允许可选的控制台输出。如果您没有设置日志目标,它只是将其抽出。

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

我相信这是将屏幕上的所有内容放入文件的最简单方法,它是本机 PS CmdLet,因此您不必更改或安装脚本中的任何内容

Start-Transcript -Path Computer.log
Write-Host "everything will end up in Computer.log"
Stop-Transcript
9
function WriteLog
{
    Param ([string]$LogString)
    $LogFile = "C:\$(gc env:computername).log"
    $DateTime = "[{0:MM/dd/yy} {0:HH:mm:ss}]" -f (Get-Date)
    $LogMessage = "$Datetime $LogString"
    Add-content $LogFile -value $LogMessage
}
WriteLog "This is my log message"

本站系公益性非盈利分享网址,本文来自用户投稿,不代表码文网立场,如若转载,请注明出处

(574)
在 Powershell中创建日志文件
上一篇
最小化 NUMA架构中的核心间通信
下一篇

相关推荐

发表评论

登录 后才能评论

评论列表(13条)