powershell newb here。嘗試將輸出記錄到文件時遇到一些困難。我嘗試了兩種策略,這兩種策略都不適合我。第一個是使用Start/Stop-Transcript cmdlet。這在我的本地機器上測試時效果很好,但在我部署到工作站的腳本中似乎沒有任何作用。如何將輸出記錄到文件
$path1 = Test-Path ($env:ProgramFiles + "\Sophos\Sophos Anti-Virus\SavService.exe")
$path2 = Test-Path (${env:ProgramFiles(x86)} + "\Sophos\Sophos Anti-Virus\SavService.exe")
$shareloc = '\\SERVER1\NETLOGON\SophosPackages\SophosInstall_wFW_Silent.exe'
$logpath = '\\SERVER1\NETLOGON\si_sophos_log.txt'
if (($path1 -eq $true) -or ($path2 -eq $true)) {} ELSE {
& $shareloc
Start-Transcript -Append -Path $logpath | Out-Null
Write-Output ""
Get-Date
Write-Output "Sophos has been installed on `"$env:COMPUTERNAME`""
Write-Output ""
Stop-Transcript
}
我寧願做它的方式,是使用:| Out-File -Append -FilePath $logpath
我認爲這將是首選方法,因爲它會趕上作爲並列開始,成績單可能發生在日誌中,任何錯誤。當我嘗試但是用這種方法,我在管道"An empty pipeline element is not allowed."
$path1 = Test-Path ($env:ProgramFiles + "\Sophos\Sophos Anti-Virus\SavService.exe")
$path2 = Test-Path (${env:ProgramFiles(x86)} + "\Sophos\Sophos Anti-Virus\SavService.exe")
$shareloc = '\\SERVER1\NETLOGON\SophosPackages\SophosInstall_wFW_Silent.exe'
$logpath = '\\SERVER1\NETLOGON\si_sophos_log.txt'
if (($path1 -eq $true) -or ($path2 -eq $true)) {} ELSE {
& $shareloc
Write-Output ""
Get-Date
Write-Output "Sophos has been installed on `"$env:COMPUTERNAME`""
Write-Output ""
} | Out-File -Append -FilePath $logpath
預先感謝您的任何援助得到一個錯誤!
這樣做的技巧,謝謝! – user3216526