基于参数

时间:2018-06-05 00:12:15

标签: powershell powershell-v4.0

我有一些函数可以从Jenkins作为管道的一部分调用,它们也可以从pester测试中调用,或者最后可以从powershell控制台调用它们。我真正的问题源于Jenkins似乎并不像我认为的那样处理写输出。

所以我正在做的是创建一个布尔参数,允许我选择是否使用退出代码或返回消息终止我的函数。退出代码将由我的管道逻辑和其余的返回消息使用?

是否有一种替代方法我应该使用这似乎是一种黑客攻击。

function Get-ServerPowerState
{
[CmdletBinding()]
param
(
    [string[]]$ilo_ip,
    [ValidateSet('ON', 'OFF')]
    [string]$Status,
    [boolean]$fail
)

BEGIN
{
    $here = Split-Path -Parent $Script:MyInvocation.MyCommand.Path
    $Credentials = IMPORT-CLIXML "$($here)\Lib\iLOCred.xml"

}
PROCESS
{
    foreach ($ip in $ilo_ip)
    {
        New-LogEntry -Message ("Getting current powerstate " + $ip)
        If (Test-Connection -ComputerName $ip.ToString() -Count 1 -Quiet)
        {
            $hostPower = Get-HPiLOhostpower -Server $ip -Credential 
            $Credentials -DisableCertificateAuthentication
        }
    }

}
END
{

    If($fail){
        New-LogEntry -Message "Script been set to fail with exit code" -Log Verbose
        New-LogEntry -Message "The host is powered -  $($HostPower.Host_Power)" -Log Verbose

        If($hostPower.HOST_POWER -match $Status)
        {
            Exit 0
        }
        else {

            Exit 1
        }
    }
    else {
        New-LogEntry -Message "Script been set to NOT fail with exit code" -Log Verbose
        New-LogEntry -Message "The host is powered -  $($HostPower.Host_Power)" -Log Verbose
        If($hostPower.HOST_POWER -match $Status)
        {
            return 0
        }
        else {

            return 1
        }

    }


}
}

1 个答案:

答案 0 :(得分:0)

喜欢这个

function Get-Output {
  param ([switch]$asint)

  if ($asint) {
    return 1
  }
  else {
    write-output 'one'
  }
}

Get-Output

Get-Output -asint

如果您打算在管道中使用输出,请使用Write-Output。如果您只想将其发送到主机进程,请使用Write-Host。如果我想为变量赋值,我通常使用return关键字。

[int]$result = Get-Output -asint