需要在 powershell 脚本末尾创建错误摘要
Need to create a summary of errors at the end of a powershell script
我脑子里有一个想法,我会尽我所能去解释我想做的事情。我有一个基本上需要一堆文件并将它们复制到新位置并重命名的脚本,非常简单。随着文件数量的增加,滚动浏览并检查每一行以确保文件复制成功变得越来越乏味。这是我为每个文件使用的代码示例:
if ( $(Try { Test-Path $source.trim() } Catch { $false }) ) {
Write-Host "Source file was found, now copying."
mv -v -f $source $filedest
Write-Host "Source has been copied, moving onto the next section.!!!!!"
}
Else {
Write-Host "Source file was NOT found, moving onto the next section.XXXXX"
}
我调出脚本开头的所有变量,并使用唯一的变量名称复制此格式。所以这是让我寻找'!!!!!'或 'XXXXX' 查看文件是否被复制。
我想要做的是拥有整个文件 运行,然后在底部有一些摘要,内容如下:
成功:15
失败次数:2
失败文件名:
file7.csv
file12.csv
使用 2 个变量来跟踪成功计数和未找到的文件列表:
$successCount = 0
$failedFileNames = @()
# ...
if ( $(Try { Test-Path $source.trim() } Catch { $false }) ) {
Write-Host "Source file was found, now copying."
mv -v -f $source $filedest
Write-Host "Source has been copied, moving onto the next section.!!!!!"
# update success counter
$successCount++
}
else {
Write-Host "Source file was NOT found, moving onto the next section.XXXXX"
# no file found, add source value to list of failed file names
$failedFileNames += $source
}
然后,当您准备好编译报告时,只需使用失败文件名数组的 Count
:
Write-Host "Success: $successCount"
Write-Host "Failure: $($failedFileNames.Count)"
Write-Host "Failed file(s):"
$failedFileNames |Write-Host
我脑子里有一个想法,我会尽我所能去解释我想做的事情。我有一个基本上需要一堆文件并将它们复制到新位置并重命名的脚本,非常简单。随着文件数量的增加,滚动浏览并检查每一行以确保文件复制成功变得越来越乏味。这是我为每个文件使用的代码示例:
if ( $(Try { Test-Path $source.trim() } Catch { $false }) ) {
Write-Host "Source file was found, now copying."
mv -v -f $source $filedest
Write-Host "Source has been copied, moving onto the next section.!!!!!"
}
Else {
Write-Host "Source file was NOT found, moving onto the next section.XXXXX"
}
我调出脚本开头的所有变量,并使用唯一的变量名称复制此格式。所以这是让我寻找'!!!!!'或 'XXXXX' 查看文件是否被复制。
我想要做的是拥有整个文件 运行,然后在底部有一些摘要,内容如下:
成功:15
失败次数:2
失败文件名:
file7.csv
file12.csv
使用 2 个变量来跟踪成功计数和未找到的文件列表:
$successCount = 0
$failedFileNames = @()
# ...
if ( $(Try { Test-Path $source.trim() } Catch { $false }) ) {
Write-Host "Source file was found, now copying."
mv -v -f $source $filedest
Write-Host "Source has been copied, moving onto the next section.!!!!!"
# update success counter
$successCount++
}
else {
Write-Host "Source file was NOT found, moving onto the next section.XXXXX"
# no file found, add source value to list of failed file names
$failedFileNames += $source
}
然后,当您准备好编译报告时,只需使用失败文件名数组的 Count
:
Write-Host "Success: $successCount"
Write-Host "Failure: $($failedFileNames.Count)"
Write-Host "Failed file(s):"
$failedFileNames |Write-Host