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