I am writing some PowerCLI and want a user to cancel a script from a popup that's provided.
$path = "$home\Desktop\deploy.csv"
$a = new-object -comobject wscript.shell
If (Test-Path $path ) {
$b = $a.popup("This file already exists on your Desktop and will be overwritten",0,"PowerCLI Script",1)
}
} Else {
echo $null >> $home\Desktop\deploy.csv
}
The popup provides a cancel and OK option, what would I need to add for the user to cancel out of the script to not overwrite the existing deploy.csv file? I found $MainForm.Close()
but I am unsure how to structure the syntax and if this is the best way to achieve such a thing.
What you can do is evaluate the value returned by the popup which is stored in $b
. If the value is 2 it means the user clicked on Cancel. Then you can simply exit.
$path = "$home\Desktop\deploy.csv"
$a = new-object -comobject wscript.shell
If (Test-Path $path )
{
$b = $a.popup("This file already exists on your Desktop and will be overwritten",0,"PowerCLI Script",1)
if ($b -eq 2)
{
exit
}
}
Else
{
echo $null >> $home\Desktop\deploy.csv
}
PS: I fixed a couple of issues with the code as well. Unless this is a portion of larger code, the Else
statement could not have processed.