Valter Silva
Valter Silva

Reputation: 16656

How to reuse/extend functions on Powershell?

I'm trying to develop 2 functions with Powershell. The first, will check my database status (online/offline). The second function should loop on the first function until a certain state is achieve.

function Get-DBStatus
{
  <# .. removed help section for brevity .. #>
  [CmdletBinding()]
  [OutputType([System.Object])]
  param
  (
    [Parameter(Mandatory = $true)]
    [String]$ServerName,
    [Parameter(Mandatory = $true)]
    [String]$ServerUser,
    [Parameter(Mandatory = $true)]
    [String]$ServerPassword,
    [Parameter(Mandatory = $true)]
    [String]$DatabaseName,
  )

  try
  {
    $params = @{ ... }     
    $dbStatus = Invoke-SqlConnection @params | Where-Object {$_.Name -match $AltDBName }
  }
  catch
  {
    Write-Error -Message ('An error has occured while ...')
  }
  if ([String]::IsNullOrEmpty($dbStatus) -eq $false)
  {
    $dbStatus
  }

 # <<< function Get-DbStatusOnlyIf 
 # <<< same parameters as the function above
 # <<< get the desired status as a new parameter
 # <<< loop the function above until the desired status is achieved or a timeout is reached

}

I'm new to Powershell and I think I shouldn't repeat myself rewriting the same parameters from the first function into the second one since they're dependent. However, I might be wrong, thus the question.

Thank you for your assistance!

Upvotes: 2

Views: 825

Answers (1)

Adam Mnich
Adam Mnich

Reputation: 471

You have to rewrite this parameters on your second function and pass them through or add another paramter to your first function that will do the looping. I would go with the second solution.

Try something like that

function Get-DBStatus {
    <# .. removed help section for brevity .. #>
    [CmdletBinding()]
    [OutputType([System.Object])]
    param
    (
        [Parameter(Mandatory = $true)]
        [String]$ServerName,
        [Parameter(Mandatory = $true)]
        [String]$ServerUser,
        [Parameter(Mandatory = $true)]
        [String]$ServerPassword,
        [Parameter(Mandatory = $true)]
        [String]$DatabaseName,
        $WaitForStatus, #or something like that
        [int]$Timeout=10
    )
    do { 
        try {
            #$params = @{ ... }     
            $dbStatus = Invoke-SqlConnection @params | Where-Object {$_.Name -match $AltDBName }
        }
        catch {
            Write-Error -Message ('An error has occured while ...')
            return
        }
        if ([String]::IsNullOrEmpty($dbStatus) -eq $false) {
            if ($WaitForStatus){
                if ($dbStatus -eq $WaitForStatus) {
                    $dbStatus
                    $EndLoop = $true               
                }
                else {
                    Write-Host -NoNewline "." #only for test   
                    Start-Sleep -Seconds 1
                    $Timeout -= 1                  
                }
            }
            else{
                $dbStatus
                $EndLoop = $true
            }
        }
    }
    until ($EndLoop -or $Timeout -eq 0)              
}

or with recursion

function Get-DBStatus {
    <# .. removed help section for brevity .. #>
    [CmdletBinding()]
    [OutputType([System.Object])]
    param
    (
        [Parameter(Mandatory = $true)]
        [String]$ServerName,
        [Parameter(Mandatory = $true)]
        [String]$ServerUser,
        [Parameter(Mandatory = $true)]
        [String]$ServerPassword,
        [Parameter(Mandatory = $true)]
        [String]$DatabaseName,
        $WaitForStatus, #or something like that
        [int]$timeout = 3
    )
    if ($WaitForStatus) {    
        $start = Get-Date
        while (((get-date) - $start).TotalSeconds -lt $timeout) {
            $res = Get-DBStatus -ServerName $ServerName -ServerUser $ServerUser -ServerPassword $ServerPassword -DatabaseName $DatabaseName
            if ($WaitForStatus -eq $res) {
                return $res
            }
            Start-Sleep -Seconds 1        
        }

    }
    else {
        try {
            $params = @{ ... }     
            $dbStatus = Invoke-SqlConnection @params | Where-Object {$_.Name -match $AltDBName }
        }
        catch {
            Write-Error -Message ('An error has occured while ...')
        }
        if ([String]::IsNullOrEmpty($dbStatus) -eq $false) {
            $dbStatus
        }
    }

}

Upvotes: 2

Related Questions