I have a simple powershell script that executes afdiag.exe
with a few parameters.
Example:
AFDiag.exe /Database:"Distributed SYS_Test" /Template:"STS-Heartbeat" /DelEF:"2024-06-01 00:00:00";"2024-06-03 15:00:00"
However it seems to reject the command I’m thinking to the way it’s reading it in. I have tried several methods including set-location
so as to just afdiag.exe
without the path and it still fails. My two eyes can’t see where I’ve gone wrong.
Code
# Define paths and parameters
$AFDiagPath = "D:Program FilesPIPCAFAFDiag.exe"
$TemplatesFile = "D:ScriptsPI AF Monthly CleanupcfgPIAF MDB Cleanup Templates.csv"
$AFDatabase = "Distributed SYS_Test"
$OutputFolder = "D:ScriptsPI AF Monthly Cleanuplog"
# 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"
This is the error generated
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
1