Create Log File in Powershell

前端 未结 8 2134
半阙折子戏
半阙折子戏 2020-11-30 22:11

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

8条回答
  •  日久生厌
    2020-11-30 22:51

    Using this Log-Entry framework:

    Script:

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

    Screen output:

    Log File (at 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
    

提交回复
热议问题