我是powershell新闻。我在尝试将输出记录到文件时遇到了一些困难。我尝试了两种战术,这两种战术对我都不起作用。第一种是使用开始/停止-转录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谢谢您的任何帮助!
发布于 2014-01-21 04:41:48
如果您写了以下内容:
if ($true) {Write-Output "titi"} else {Write-Output "toto"} | Out-File -Append c:\temp\titi您将得到相同的错误,因为在管道中没有计算if条件。
你可以试着强迫我评估它
$(if ($true) {Write-Output "titi"} else {Write-Output "toto"}) | Out-File -Append c:\temp\titi发布于 2014-01-20 20:02:04
当if条件计算为true时,空scriptblock将被管道传输到Out,这将导致您的错误。例如,以下抛出您指定的错误:
if($true) { } else { Write-Output "Something" } | Out-File -Append -FilePath C:\temp\myfile.txt
https://stackoverflow.com/questions/21242838
复制相似问题