2
0

Fixed Running .bat & .ps1

This commit is contained in:
bschaper 2025-01-29 15:57:27 -06:00
parent aee799326e
commit db38491d7b

View File

@ -1,26 +1,25 @@
# Specify the directory containing the scripts
$scriptDirectory = "C:\Scripts\Tasks\System-Tasks"
# Get all Batch and PowerShell scripts in the directory
$scriptsToRun = Get-ChildItem -Path $scriptDirectory -Filter "*.bat"
$psScripts = Get-ChildItem -Path $scriptDirectory -Filter "*.ps1"
$scriptsToRun += $psScripts
# Get all files and then filter within the loop
$scriptsToRun = Get-ChildItem -Path $scriptDirectory
# Iterate through the script paths and execute each one
foreach ($script in $scriptsToRun) {
if ($script.Extension -in ".ps1", ".bat") { # Check if the extension is .ps1 or .bat
try {
# Execute the script based on its extension
Write-Host "Executing script: $($script.FullName)"
if ($script.Extension -eq ".ps1") {
# Execute PowerShell script
& "$($script.FullName)"
& powershell -File "$($script.FullName)" # -NoProfile, -ExecutionPolicy as needed
} else {
# Execute Batch script
& "$($script.FullName)"
Start-Process -FilePath "$($script.FullName)" -Wait
}
Write-Host "Script '$script' executed successfully."
Start-Sleep -Seconds 5 # Adjust the delay as needed
Write-Host "Script '$($script.FullName)' executed successfully."
Start-Sleep -Seconds 5
} catch {
Write-Warning "Error executing '$script': $($_.Exception.Message)"
Write-Warning "Error executing '$($script.FullName)': $($_.Exception.Message)"
}
} #end extension check
}