Я использую довольно простой сценарий powershell для включения набора вспомогательных сценариев в свой основной.
Всякий раз, когда файл отсутствует, я отображаю уведомление в терминале, работает нормально. Я также хочу написать уведомление в мой лог-файл.
Итак, я написал простую функцию для создания метки времени:
function psa_get_timestamp {
return "[{0:MM/dd/yy} {0:HH:mm:ss}]" -f (Get-Date);
}
Эта функция работает нормально, , но не в первый раз, когда я ее вызываю.
# Set logfile
$logfile = join-path -path $PSScriptRoot -childpath "ps-admin_log.txt";
# Create array of needed files
$supporting_files = @(
'/resources/requirements/environment.ps1',
'/resources/loggers/logger.ps1',
'/resources/menus/menus.ps1',
'/resources/views/headers.ps1'
);
# Include supporting files, throw non-fatal error if fails
foreach($support_file in $supporting_files) {
$file = join-path -path $PSScriptRoot -childpath $support_file;
if(Test-Path $file) {
. $file;
Write-Output "$(psa_get_timestamp) Successfully included file: $file" | Out-file $logfile -append
} else {
# Throw non-fatal error in terminal and write it to logfile
Write-Host "! Notice: Could not include supporting file: $file" -ForegroundColor Yellow;
Write-Output "$(psa_get_timestamp) ! Notice: Could not include supporting file: $file" | Out-file $logfile -append
}
}
Мой файл журнала выглядит следующим образом (обратите внимание на отсутствующую метку времени в первой строке):
�� S u c c e s s f u l l y i n c l u d e d f i l e : <file_name>
[ 0 1 / 2 5 / 1 9 1 9 : 2 1 : 3 1 ] S u c c e s s f u l l y i n c l u d e d f i l e : <file_name>
[ 0 1 / 2 5 / 1 9 1 9 : 2 1 : 3 1 ] S u c c e s s f u l l y i n c l u d e d f i l e : <file_name>
[ 0 1 / 2 5 / 1 9 1 9 : 2 1 : 3 1 ] S u c c e s s f u l l y i n c l u d e d f i l e : <file_name>
Почему моя функция не работает при первом вызове?
Это ошибка при первом вызове:
psa_get_timestamp : The term 'psa_get_timestamp' is not recognized as the name of a cmdlet, function, script file, or
operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try
again.
At <scriptlocation>:19 char:21
+ Write-Output "$(psa_get_timestamp) Successfully included file: $f ...
+ ~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (psa_get_timestamp:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
При использовании -Verbose
:
VERBOSE: Performing the operation "Output to File" on target
"<logfile_location>".
VERBOSE: Performing the operation "Output to File" on target
"<logfile_location>".
VERBOSE: Performing the operation "Output to File" on target
"<logfile_location>".
VERBOSE: Performing the operation "Output to File" on target
"<logfile_location>".