2017-02-23 96 views
-1

我最近開始使用Powershell,我一直試圖儘可能地使用它,但我非常沮喪。 我有這樣的代碼,我想要做什麼,但日誌不工作,但我不明白的錯誤:Powershell我沒有得到日誌工作

Function logWrite 
{ 
param ([string]$logstring) 
$Computer = gc env:computername 
$date = $(Get-Date -UFormat "%d-%m-%Y") 
$Logfile = C:\Windows\Temp\Key-KB-Testing-$Computer-$date.log 
Add-content -Path $Logfile -Value $logstring 
} 

$Stamp = (Get-Date).toString("dd/MM/yyyy HH:mm:ss") 
logWrite "Testing KB3142037" 
$KB3142037result= gwmi -cl win32_reliabilityRecords -filter "sourcename = 'Microsoft-Windows-WindowsUpdateClient'" | where { $_.message -match 'KB3142037'} | select -Expand Message 
logWrite $Stamp 
logWrite "Testing KB3142033" 
$KB3142033result= gwmi -cl win32_reliabilityRecords -filter "sourcename = 'Microsoft-Windows-WindowsUpdateClient'" | where { $_.message -match 'KB3142033'} | select -Expand Message 
logWrite $Stamp 
$Key = Get-ItemProperty HKLM:\SOFTWARE\Wow6432Node\Microsoft\.NetFramework\v4.0.30319\ -Name SchUseStrongCrypto | Select-Object -ExpandProperty SchUseStrongCrypto 
$arquitecture = (gwmi win32_computersystem).SystemType 

If ($arquitecture -eq "x32-based PC") 
{ 
    If ($Key -eq $true) 
    { 
     logWrite "The key exist and the value is '$Key'" 
     if ($Key -ne "0") 
     { 
      Set-ItemProperty -Path 'HKLM:\SOFTWARE\Wow6432Node\Microsoft\.NetFramework\v4.0.30319' -Name 'SchUseStrongCrypto' -Value '0' -Type DWord -ErrorAction SilentlyContinue -Verbose 
      logWrite "The key has been modified" 
      logWrite $Stamp 
     } 
    } 
Else { 
     logWrite "There is no key. New registry key will be created" 
     New-ItemProperty -Path HKLM:\SOFTWARE\Microsoft\.NetFramework\v4.0.30319 -Name SchUseStrongCrypto -PropertyType DWord -Value 0 -ErrorAction SilentlyContinue -Verbose 
     logWrite $Stamp 
     logWrite "The Key has been created" 
    } 

} 
Else 
{ 
    If ($Key -eq $true) 
    { 
     logWrite "The key exist and the value is '$Key'" 
     if ($Key -ne "0") 
     { 
      Set-ItemProperty -Path 'HKLM:\SOFTWARE\Wow6432Node\Microsoft\.NetFramework\v4.0.30319' -Name 'SchUseStrongCrypto' -Value '0' -Type DWord -ErrorAction SilentlyContinue -Verbose 
      logWrite "The key has been modified" 
      logWrite $Stamp 
     } 
    } 
Else { 
     logWrite "There is no key. New registry key will be created" 
     New-ItemProperty -Path HKLM:\SOFTWARE\Wow6432Node\Microsoft\.NetFramework\v4.0.30319 -Name SchUseStrongCrypto -PropertyType DWord -Value 0 -ErrorAction SilentlyContinue -Verbose 
     logWrite $Stamp 
     logWrite "The Key has been created" 
    } 

} 

任何幫助,鏈接或指導將是有益的。

提前感謝您的時間。

問候

+0

你在哪裏指定$日誌文件 –

+0

寫入到系統目錄可能會導致UAC和東西的問題。將路徑更改爲「C:\ temp」,並將寫入權限授予適當的用戶帳戶/組。 – vonPryz

+0

「日誌不起作用」是什麼意思?日誌文件是否完全沒有創建?它是空的嗎?內容是否錯誤? – TToni

回答

2

嘗試

$Logfile = "C:\Windows\Temp\Key-KB-Testing-$Computer-$date.log" 
+0

謝謝,我感到很尷尬......現在作品「」...對這個問題抱歉,但是,我會牢記在心。 –

+0

@DiegoVictorino請將此標記爲正確的答案,如果這對你有用。 – Random206

+0

我仍然不明白你爲什麼沒有得到任何錯誤。 – TToni