简体   繁体   English

使用Powershell从弹出窗口中取消脚本

[英]Cancel script from popup with Powershell

I am writing some PowerCLI and want a user to cancel a script from a popup that's provided. 我正在编写一些PowerCLI,希望用户从提供的弹出窗口中取消脚本。

$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? 弹出窗口提供了“取消”和“确定”选项,我需要添加什么以便用户取消脚本而不会覆盖现有的deploy.csv文件? 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. 我找到了$MainForm.Close()但是我不确定如何构造语法以及这是否是实现这种目标的最佳方法。

What you can do is evaluate the value returned by the popup which is stored in $b . 您可以做的是评估存储在$b的弹出窗口返回的值。 If the value is 2 it means the user clicked on Cancel. 如果值为2,则表示用户单击了“取消”。 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. PS:我也修复了代码中的几个问题。 Unless this is a portion of larger code, the Else statement could not have processed. 除非这是较大代码的一部分, Else语句将无法处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM