因此,我在脚本上做了一些“House Keeping”,我发现一个区域可以缩小/整理。 把这个GUI我已经创建:Powershell:将参数传递到函数
两个菜单栏add_click
事件重新启动HostnameA和HostnameB调用单独的功能,即使在这两个功能的代码是pratically一样的,唯一不同的是这主机名变量(见下文)。
按钮事件的代码。
$Restart_current_machine.Add_Click(
{
restart_current_machines
})
$Restart_target_machine.Add_Click(
{
restart_target_machines
})
# Function Blocks
function restart_target_machines
{
restart-computer -computer $combobox1.text -force
}
function restart_current_machines
{
restart-computer -computer $combobox2.text -force
}
我的问题是这样的: 是有办法,我可以使用Param()
(或类似的东西),以摆脱function restart_current_machines
从而只有一个功能,要么重新启动机器?
有点像?
$Restart_current_machine.Add_Click(
{
param($input = combobox1.text)
$input | restart_current_machines
})
$Restart_target_machine.Add_Click(
{
param($input = combobox2.text)
$input | restart_current_machines
})
# Only needing one function
function restart_target_machines
{
restart-computer -computer $input -force
}
我知道这很可能是错误的,但只是为了让你更好地了解我正在尝试做什么。
谢谢吉文,你是一个明星,你来我的帮助再次。 你介意解释([字符串[]] $计算机名) - 我得到的变量被称为'$ computename',但'[字符串[]]'是什么意思/做什么? – obious 2012-08-03 15:09:04
当参数类型设置为'[string]'时,参数只接受一个字符串。如果将它设置为'[string []]',它可以接受一个或多个(数组)字符串。当您将多个值传递给此类参数时,用逗号分隔每个字符串。 – 2012-08-03 15:55:19