2011-10-20 33 views
36

Tôi có mã bên dưới và hiện tại nó tải tất cả thông tin trên màn hình. Tôi muốn nó đăng nhập vào một tệp nhật ký trên D: \ Apps \ Nhật ký.Tạo tệp nhật ký trong Powershell

Các tập tin log cần phải có tên của máy tính, nó đang tải chống lại - vì vậy COMPUTERNAME.log

ý tưởng Bất kỳ làm thế nào tôi có thể làm điều này?

Cảm ơn

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

Log: Thuật ngữ 'Log' không được công nhận là tên của một lệnh, kịch bản tệp hoặc chương trình có thể hoạt động. Kiểm tra chính tả của tên đã được bao gồm, xác minh rằng đường dẫn là chính xác và thử lại. Bạn có chức năng đăng nhập không? – Hecatonchires

Trả lời

86

Đặt này ở phía trên cùng của tập tin của bạn:

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

Function LogWrite 
{ 
    Param ([string]$logstring) 

    Add-content $Logfile -value $logstring 
} 

Sau đó thay thế Write-host cuộc gọi của bạn với LogWrite.

+0

cảm ơn vì điều đó - hoạt động tốt nhưng nó không hiển thị phiên bản TCPIP.sys bây giờ - nó chỉ hiển thị "TCPIP.sys" - tương tự cho DFSR.exe. Tôi đã thay đổi Write-Host thành LogWrite nhưng cả hai không hiển thị phiên bản trong tệp nhật ký. Ngoài ra, bất kỳ ý tưởng làm thế nào tôi viết lại tệp nhật ký mỗi khi nó được chạy? Hiện tại nó gắn thêm tệp nhật ký; muốn nó nếu nó chỉ có thể tạo ra một mới mỗi lần. Cảm ơn. – lara400

+2

Đối với các thuộc tính của một đối tượng, bạn cần đóng gói toàn bộ nội dung trong dấu ngoặc đơn với $ ở phía trước. Hãy thử thay đổi nó thành: '$ (TCPIP.sys)' – JNK

+1

Để lấy tệp nhật ký để làm lại mỗi lần, bạn cần phải thêm phần khác ở trên cùng để xóa/tạo tệp nếu nó tồn tại. – JNK

25

Một chức năng có thêm các nguyên tắc này một chút.

  1. Dấu thời gian của thêm - không thể có nhật ký không có dấu thời gian.
  2. Thêm cấp độ (sử dụng INFO theo mặc định) nghĩa là bạn có thể làm nổi bật các vấn đề lớn.
  3. Cho phép đầu ra giao diện điều khiển tùy chọn. Nếu bạn không đặt đích đến đăng nhập, nó chỉ đơn giản là bơm nó ra.

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

Sử dụng này 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 đầu ra: Screen output

Log File (tại D:\Apps\Logs\<computername>.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 
Các vấn đề liên quan