在Powershell中创建日志文件

lar*_*400 61 powershell

我有以下代码,目前它加载屏幕上的所有信息.我希望它登录到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"    }
Run Code Online (Sandbox Code Playgroud)

JNK*_*JNK 125

把它放在你文件的顶部:

$Logfile = "D:\Apps\Logs\$(gc env:computername).log"

Function LogWrite
{
   Param ([string]$logstring)

   Add-content $Logfile -value $logstring
}
Run Code Online (Sandbox Code Playgroud)

然后Write-hostLogWrite.替换你的电话.

  • 对于对象的属性,您需要将整个事物封装在括号中,前面是$.尝试将其更改为:`$(TCPIP.sys)` (3认同)
  • 我建议根据已知动词的命名约定来命名您的函数:Write-Log (3认同)
  • 我还想使用-passthrough标志,以便日志记录也输出到shell窗口. (2认同)
  • 只需注意,您可以使用$ env变量将计算机名称嵌入到字符串中,而不使用`get-content`和变量赋值.`Write-Host"我的本地计算机名是:$ env:computername"` (2认同)

小智 56

一种将这些原则更进一步的功能.

  1. 添加时间戳 - 没有时间戳的日志.
  2. 添加一个级别(默认情况下使用INFO)意味着您可以突出显示重大问题.
  3. 允许可选的控制台输出.如果您没有设置日志目的地,它只会将其抽出.

    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
        }
    }
    
    Run Code Online (Sandbox Code Playgroud)


Car*_*cia 34

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

Start-Transcript -Path Computer.log

Write-Host "everything will end up in Computer.log"

Stop-Transcript
Run Code Online (Sandbox Code Playgroud)

  • 使用起来非常简单。我建议添加 `-Append` 或 `NoClobber` 否则它会覆盖日志。另请注意,它会输出很多有关进程和主机的内容作为标头,因此它并不直接等同于屏幕上的内容,除非您使用“-UseMinimalHeader”等参数。 (5认同)
  • 这应该是公认的答案。它是原生的和最小的。 (3认同)

Mol*_*lly 10

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"
Run Code Online (Sandbox Code Playgroud)


Sta*_*lav 5

日志轮换的要点:https://gist.github.com/barsv/85c93b599a763206f47aec150fb41ca0

用法:

. .\logger.ps1
Write-Log "debug message"
Write-Log "info message" "INFO"
Run Code Online (Sandbox Code Playgroud)


iRo*_*Ron 5

使用这个Log-Entry框架:

脚本:

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"}
    }
}
Run Code Online (Sandbox Code Playgroud)

屏幕输出: 屏幕输出

日志文件(位于D:\Apps\Logs\<computername>.log):

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"}
    }
}
Run Code Online (Sandbox Code Playgroud)