Powershell任务对话框

Powershell任务对话框,powershell,powershell-2.0,Powershell,Powershell 2.0,是否可以在PowerShell中使用Windows 7任务对话框 我想将以下messagebox转换为TaskDialog: [System.Windows.Forms.MessageBox]::Show( "There are currently one or more Microsoft Office applications running.`n`nYou must close down all open Office applications before the templat

是否可以在PowerShell中使用Windows 7任务对话框

我想将以下messagebox转换为TaskDialog:

[System.Windows.Forms.MessageBox]::Show(
    "There are currently one or more Microsoft Office applications running.`n`nYou must close down all open Office applications before the template update can continue.", 
    "Updating Templates", 
    [System.Windows.Forms.MessageBoxButtons]::RetryCancel, 
    [System.Windows.Forms.MessageBoxIcon]::Warning )
有人知道如何/如果可以做到这一点吗

谢谢


Ben

您可以使用
Add Type
cmdlet动态编译C#类并导入类型。因此,您可以编写C代码与本机TaskDialog函数接口,然后从PowerShell使用它。例如,您可以使用。构建它,然后使用它

Add-Type -File TaskDialog.dll
然后可以重新创建文章中显示的示例

$taskDialog = New-Object Microsoft.Samples.TaskDialog
$taskDialog.WindowTitle = "My Application"
$taskDialog.MainInstruction = "Do you want to do this?"
$taskDialog.CommonButtons = [Microsoft.Samples.TaskDialogCommonButtons]::Yes -bor [Microsoft.Samples.TaskDialogCommonButtons]::No
$result = $taskDialog.Show()
if ($result -eq 6)
{
    # Do it.

}

但是,我注意到PowerShell无法找到公共控件DLL的入口点。对此没有多少线索,也许C#代码中的P/Invoke声明必须请求一个特定的版本才能工作。很抱歉您可能仍然可以将必要的内容封装到一个小的命令行应用程序中,然后再运行。不理想,但可能是最简单的方法。

您需要使用Microsoft的,它非常简单,但尽管它在PowerShell ISE、PoshConsole、PowerGUI等中可以正常工作,但我认为它在PowerShell.exe中无法工作,因为控制台加载了错误版本的comctl32.dll(公共控件库)

希望很明显,
$result
值实际上是一个枚举值(类型为
[Microsoft.WindowsAPICodePack.Dialogs.TaskDialogResult]
)。。。但在PowerShell中,如果愿意,基本上可以将其视为字符串或int


当然,这仅仅是对TaskDialog所能做的事情的一个初步了解——如果您仅将其用于此代码,它的外观和行为与您当前的对话框非常相似——但您可以自己探索其他可能性——我可以从中推荐TaskDialog builder工具,作为学习许多选项的一种方式。

抱歉-仍然有点困惑。我如何使用本文中的代码,因为这是c#?我想我遗漏了什么…将其转换为等效的PowerShell代码。您可以使用
新建对象创建对象,然后正常设置属性。
# import the library dll from wherever you put it:
add-type -path .\Libraries\Microsoft.WindowsAPICodePack.dll

# Create and configure the TaskDialog
$td = New-Object Microsoft.WindowsAPICodePack.Dialogs.TaskDialog
$td.Caption = "Updating Templates"
$td.Text = "There are currently one or more Microsoft Office applications running.`n`nYou must close down all open Office applications before the template update can continue."
$td.StandardButtons = "Retry,Cancel"
$td.Icon = "Warning"

# Show the dialog and capture the resulting choice
$result = $td.Show()  # will return either "Retry" or "Cancel"