我有一个简单的 powershell 脚本,它使用一些参数执行
afdiag.exe
。
示例:
AFDiag.exe /Database:"Distributed SYS_Test" /Template:"STS-Heartbeat" /DelEF:"2024-06-01 00:00:00";"2024-06-03 15:00:00"
然而,它似乎拒绝了我正在思考的命令的读取方式。我尝试了几种方法,包括
set-location
,以便在没有路径的情况下仅afdiag.exe
,但它仍然失败。我的两只眼睛看不出我哪里出了问题。
代码
# Define paths and parameters
$AFDiagPath = "D:\Program Files\PIPC\AF\AFDiag.exe"
$TemplatesFile = "D:\Scripts\PI AF Monthly Cleanup\cfg\PIAF MDB Cleanup Templates.csv"
$AFDatabase = "Distributed SYS_Test"
$OutputFolder = "D:\Scripts\PI AF Monthly Cleanup\log"
# Calculate date range (today and last 30 days)
$EndDate = Get-Date
$StartDate = $EndDate.AddDays(-30)
# Format dates for AFDiag
$StartDateFormatted = $StartDate.ToString("yyyy-MM-dd")
$EndDateFormatted = $EndDate.ToString("yyyy-MM-dd")
# Read template names from CSV file
$TemplateNames = Import-Csv -Path $TemplatesFile | Select-Object -ExpandProperty AFTemplates
# Create a timestamp for the log file
$Timestamp = Get-Date -Format "yyyyMMdd-HHmmss"
$LogFileName = "piaf-mdbc-log-$Timestamp.txt"
$LogFile = Join-Path -Path $OutputFolder -ChildPath $LogFileName
# Iterate through template names and execute AFDiag command
foreach ($TemplateName in $TemplateNames) {
# $AFDiagCommand = "AFDiag.exe /Database:`"$AFDatabase`" /Template:`"$TemplateName`" /DelEF:`"$StartDateFormatted`";`"$EndDateFormatted`""
$AFDiagCommand = "$AFDiagPath /Database:`"$AFDatabase`" /Template:`"$TemplateName`" /DelEF:`"$StartDateFormatted`";`"$EndDateFormatted`""
$Result = Invoke-Expression $AFDiagCommand
# Append result to the log file
$Result | Out-File -Append -FilePath $LogFile
}
# Display a message
Write-Host "AFDiag commands executed. Results logged in $LogFile"
这是生成的错误
AFDiag.exe : The term 'AFDiag.exe' 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 line:1 char:1
+ AFDiag.exe /Database:"Distributed SYS_Test" /Template:"STS-Heartbea ...
+ ~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (AFDiag.exe:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
更新了代码以利用“Set-Location”来解决涉及“Program Files(x)”空白的路径问题。这似乎一劳永逸地解决了问题。
Set-Location -Path "D:\Program Files\PIPC\AF\"
$AFDiagPath = ".\AFDiag.exe"