我有这个代码,但我遇到的问题是我的函数外的变量$ RestartActionChoice是空的..在它的1或2里面但是当我输入Try / Catch时它被清除了..有人可以帮忙吗。< / p>
谢谢,
#Param
$ ErrorActionPreference =“停止”
$ Resource =读取主机“输入群集资源”
$ Application =(Read-Host“Application”)。ToUpper()
$ Stage =(读取主机“输入阶段”)。ToUpper()
$ Cluster =(读取主机“输入群集”)。ToUpper()
$ Group =(读取主机“输入组”)。ToUpper()
set-variable -Name RestartActionChoice -scope global
function popup
{
#function to ask to failover or not.
$message = "Parameter : If restart is unsuccessful,fail over all resources in this service or application?"
$yes = New-Object System.Management.Automation.Host.ChoiceDescription "&Yes", `
"Enable the parameter."
$no = New-Object System.Management.Automation.Host.ChoiceDescription "&No", `
"Disable the parameter."
$options = [System.Management.Automation.Host.ChoiceDescription[]]($yes, $no)
$result = $host.ui.PromptForChoice($title, $message, $options, 0)
switch ($result)
{
0 {"You selected Yes."}
1 {"You selected No."}
}
if($result -eq 0)
{
$RestartActionChoice = 2
}
Else
{
$RestartActionChoice = 1
}
return,$RestartActionChoice
}#End function
Try
{
popup
Write-Host -BackgroundColor DarkRed "RestartThreshold will be 3 by default"
}
答案 0 :(得分:0)
两件事:从函数的最后一行中删除 return,,只需声明变量$ RestartActionChoice,这样内部的值就会出现在函数的管道上。然后将其分配给变量并在写主机中显示:
$结果=弹出
Write-Host -BackgroundColor DarkRed&#34; RestartThreshold默认为$ result;#34;
或只是这样做
Write-Host -BackgroundColor DarkRed&#34; RestartThreshold默认为$(popup)&#34;