远程计算机文件传输PowerShell脚本日志功能异常求助
Let's dig into why your Write-Log function isn't outputting to the console while still successfully writing to log files. I've spotted a few key issues and fixes:
1. Critical Syntax Error in Parameter Binding
Looking through your script, there's a missing space in one of the Write-Log calls that breaks parameter parsing:
# Broken line (no space after "Successful") Write-Log -OperationSuccessful "Successful"-Message $message
PowerShell interprets "Successful"-Message as a single string value for the -OperationSuccessful parameter, instead of recognizing -Message as a separate parameter. This throws off the logic in your Write-Log function, potentially skipping the expected colored console output.
Fix: Add a space between the parameter value and the next parameter:
Write-Log -OperationSuccessful "Successful" -Message $message
2. Passing Exception Objects Instead of Strings
In your error handling blocks, you're passing raw Exception objects to the -LogSeparator parameter:
Write-Log -OperationSuccessful "Failed" -LogSeparator $_.Exception
While Add-Content automatically converts objects to strings for log files, Write-Host can behave unpredictably with non-string inputs, especially in some PowerShell versions.
Fix: Convert the exception to a string explicitly:
Write-Log -OperationSuccessful "Failed" -Message $_.Exception.ToString()
Or update your Write-Log function to handle non-string inputs for $LogSeparator:
function Write-Log { param ( [Parameter(Position = 0, Mandatory = $false)] [String] $OperationSuccessful, [Parameter(Position = 1, Mandatory = $false)] [String] $Message, [Parameter(Position = 2, Mandatory = $false)] [Object] $LogSeparator # Change type to Object ) if($null -eq $LogSeparator) { $timestamp = Get-Date -Format "yyyy.MM.dd. HH:mm:ss:fff" $logEntry = $timestamp + " - " + $Message } else { $logEntry = $LogSeparator.ToString() # Convert to string explicitly } # Rest of your function code... }
3. Undefined $Destination Variable (Bonus Fix)
At the end of your script, you're using an undefined $Destination variable in log messages:
$message = "Successfully transferred " + $successfulTransfers + " files to " + $Destination + " folder"
This will output an empty value in your logs and console. Replace $Destination with a valid path reference (like $configuration.TransferFolder or a dynamic path from your transfer logic).
4. Debugging Console Output
To verify that Write-Host is working at all, add a test line at the top of your script:
Write-Host "Testing console output - script starting..." -ForegroundColor Cyan
If this doesn't show up in your console, check how you're running the script:
- Ensure you're not redirecting output (e.g.,
.\script.ps1 > log.txt) which would send all console output to a file. - Make sure you're running in an interactive PowerShell session (not
-NonInteractivemode, which suppresses Write-Host output).
After applying these fixes, your Write-Log function should output to both the console and log files as expected.
内容的提问来源于stack exchange,提问作者Zoran Jankov

