2014-09-02 49 views
2

我想在Powershell脚本中重复使用相同的消息框,而不必每次都重复所有设置。我最初以为我会把它保存为一个变量:有没有办法在Powershell中重复使用消息框设置?

$StandardMessage = [System.Windows.Forms.MessageBox]::Show("Repetitive Message.", "Chores.") 

但正如我发现这仅仅存储用户在变量的消息框响应。

我想这样做的是类似于下面的伪代码的东西:

$StandardMessage = [System.Windows.Forms.MessageBox]::Show("Repetitive Message.", "Chores.") 

While(true){ 

    If(condition){ 
     $StandardMessage 
    } 
    If(condition2){ 
     $StandardMessage 
    } 

} 

凡条件是基于时间的。这实质上是在一天中的特定时间显示消息。

问另一种方式(也许更清楚):是否有可能'定义'一个消息框而不实际'显示'它?

回答

1

您需要使用功能我的好人!

Function Show-MyMessage{ 
    [System.Windows.Forms.MessageBox]::Show("Repetitive Message.", "Chores.") 
} 

While(true){ 

    If(condition){ 
     Show-MyMessage 
    } 
    If(condition2){ 
     Show-MyMessage 
    } 

} 

编辑:个人而言,我手头上有这个功能对于我的几个脚本,以使用需要:

Function Show-MsgBox ($Text,$Title="",[Windows.Forms.MessageBoxButtons]$Button = "OK"){ 
[Windows.Forms.MessageBox]::Show("$Text", "$Title", [Windows.Forms.MessageBoxButtons]::$Button, [Windows.Forms.MessageBoxIcon]::Information) | ?{(!($_ -eq "OK"))} 
} 

然后我就可以把它根据需要,如:

Show-MsgBox -Title "You want the truth?" -Text "You can't handle the truth!" 

而且我弹出了我想要的文本和标题,以及一个OK按钮。

enter image description here

按钮可以指定(有一个在ISE弹出它给的选项),和标题可以,如果我感觉懒惰被排除在外。只有我真的必须养活它才是信息。

+0

谢谢,这是我需要的。我太专注于寻找一种内联解决方案(比如理论上用:: define替换:: show或者沿着这些行的东西)来考虑使用函数。出于好奇,有没有人知道一种没有功能的方法? – Arne 2014-09-02 22:34:13

相关问题