I am unable to stop a PowerShell script if the batch scripts which are being called by this PowerShell scripts throws an error.
bat 1:
PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& '%PowerShellScriptPath%\CallInstall.ps1'";
CallInstall.ps1 calls few batch files and vbs files, in the event of failure of a prerequisite the bat2 calls a vbs based message box to display an error. Ideally at this time the execution should stop and the bat1 should start the remaining process but in my case CallInstall.ps1 still executes all the batch and vbs scripts before going back to bat1.
CallInstall.ps1:
[System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") | Out-Null
[System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") | Out-Null
Set-Location $PSScriptRoot
$ScriptsHome = Get-Item '.\ScriptInstall\*'
#Define Form
$Form = New-Object System.Windows.Forms.Form
$Form.width = 1000
$Form.height = 200
$Form.Text = "** Installation in Progress**"
$Form.Font = New-Object System.Drawing.Font("Times New Roman" ,12, [System.Drawing.FontStyle]::Regular)
$Form.MinimizeBox = $False
$Form.MaximizeBox = $False
$Form.WindowState = "Normal"
$Form.StartPosition = "CenterScreen"
$Form.Opacity = .8
$Form.BackColor = "Gray"
# Init ProgressBar
$ProgressBar = New-Object System.Windows.Forms.ProgressBar
$ProgressBar.Maximum = $ScriptsHome.Count
$ProgressBar.Minimum = 0
$ProgressBar.Location = new-object System.Drawing.Size(10,70)
$ProgressBar.size = new-object System.Drawing.Size(967,10)
$Form.Controls.Add($ProgressBar)
#$Form.Controls.Add($MessagesLabel)
#Running Script Name
#$Label = New-Object System.Windows.Forms.Label
#$Label.AutoSize = $true
#$Label.Location = New-Object System.Drawing.Point(10,50)
#$Form.Controls.Add($Label)
#Define Array messages
#Array
$Messages = @("Message 1",
"Message 2",
"Message 3",
"Message 4",
"Message 5",
)
$MessagesLabel = New-Object System.Windows.Forms.Label
$MessagesLabel.AutoSize = $true
$MessagesLabel.Location = New-Object System.Drawing.Point(10,50)
$Form.Controls.Add($MessagesLabel)
# Add_Shown action
$ShownFormAction = {
$Form.Activate()
foreach ($script in $ScriptsHome) {
$ProgressBar.Increment(1)
$MessagesLabel.Text = $Messages[$ProgressBar.Value - 1]
Start-Process $script.FullName -Wait -WindowStyle Hidden
}
$Form.Dispose()
}
$Form.Add_Shown($ShownFormAction)
# Show Form
$Form.ShowDialog()
#Create Logs
Invoke-Expression -Command .\LogScript.ps1
Inside the ScriptInstall folder there are 10 batch and vbs scripts on one of the batch script:
:error
cscript %base_dir%\fail.vbs > %Log%
:match
findstr /I /c:"xxxx" c:\match.txt
if %errorlevel% == 0 (
goto nextStep
) else (
goto ErrorCheck
)
and in fail.vbs:
Dim vbCrLf : vbCrLf = Chr(13) & Chr(10)
Set WshShell = WScript.CreateObject("WScript.Shell")
MsgBox "Installation Fail !!!" & vbCrLf & "Message1" & vbCrLf & "Click OK to Exit" , 16, "Fail"
Ideally upon pressing ok on this fail.vbs box CallInstall.ps1 should not proceed with rest of the scripts in ScriptInstall folder but it does otherwise. It continues with remaining scripts in ScriptInstall folder and then returns the execution to bat1.