From db38491d7ba8c9a48bb5b39c4afcb9a88e2a3d0f Mon Sep 17 00:00:00 2001 From: bschaper Date: Wed, 29 Jan 2025 15:57:27 -0600 Subject: [PATCH] Fixed Running .bat & .ps1 --- Tasks/Run-System-Tasks.ps1 | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/Tasks/Run-System-Tasks.ps1 b/Tasks/Run-System-Tasks.ps1 index 02b3928..27fc07d 100644 --- a/Tasks/Run-System-Tasks.ps1 +++ b/Tasks/Run-System-Tasks.ps1 @@ -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 }