Public/Common/common.ps1


function GetDSEPMs {
    # query DSE's PMs from AD.
    $OU = "OU=VUTDCs,OU=pMachines,OU=Servers,DC=chn,DC=sponetwork,DC=com"
    $ADPMs = Get-ADComputer -SearchBase $OU -Filter *
    return $ADPMs.Name
}

function FindOutDSEPM {
# to find out DSE's PM from list.
    param ([String[]]$PMachines)

    $Result = @()
    $DSEPMs = GetDSEPMs
    foreach ($pm in $PMachines) {
        if ($pm -in $DSEPMs) { 
            $Result += $pm 
            if ($DSEPMs.Count -eq $Result.Count) { break }
        }
    }

    return $Result
}

function Get-Servers
{
  param(
    $Search
   )
  return Get-ADComputer -Filter {Name -Like $Search -AND OperatingSystem -notlike "*Standard*"} | select -expand name
}


function Test-Pingable {
    param([string[]] $PMachines)

    $Result = @()



    $PMachines | ForEach-Object {
        if (!(Test-Connection -ComputerName $_ -Count 1 -Quiet)) {
            Write-Verbose "PMs: $_ cannot be reachable.`n"
            $Result += $_
        }

    }

    $Result
}


Function Watch-Jobs {
    <#
    .SYNOPSIS
    Watch PowerShell jobs to finish and return results.
     
    .DESCRIPTION
    This function is used to monitor PS backgroud job running.
    It will treate the jobs in stats "Finished", "failed" and "Blocked".
    "Finished" jobs will be receive returned results and delete jobs.
    "Failed" jobs will be receicve jobs and show failed computer name and delete jobs.
    Just show warning but don't delete for "Blocked".
     
    .PARAMETER Id
    PowerShell job ID.
     
    .PARAMETER Activity
    Display in progress bar.
     
    .PARAMETER TimeOut
    The limited time for PowerShell job running. Default is 120 seconds.
    If time end up, all unfinished jobs will be terminated.
     
    .PARAMETER NoWait
    This function will return result immediately once any job finished or failed.
     
    .EXAMPLE
    Watch-Jobs -Activity "Watching jobs" -Status "Executing" -TimeOut 3600
    Watch all background jobs in 1 hour.
     
    .EXAMPLE
    Watch-Jobs -NoWait
    Watch all background jobs in 120 seconds. It will end up if one of jobs finished.
     
    .EXAMPLE
    Watch-Jobs -Id 2
    Just watch job 2 in 120 seconds.
    #>

        Param (
            [Parameter(ValueFromPipelineByPropertyName=$true)]
            [Int[]] $Id,
            [String] $Activity = "Getting PowerShell Job(s) info",
            [String] $Status = "Querying",
            [Int] $TimeOut = 120,
            [Switch] $NoWait
        )
    
        $results = @()
        $FailedComputers = $()
    
        If (!$Id) { $command = "Get-Job" }
        Else { $command = "Get-Job -Id $Id -ErrorAction Ignore" }
    
        while ($jobs = Invoke-Expression $command) {
    
            $RunningJobs = $jobs | ? State -EQ "Running"
            $CompletedJobs = $jobs | ? State -EQ "Completed"
            $FailedJobs = $jobs | ? State -EQ "Failed"
            $blockedJobs = $jobs | ? State -EQ "Blocked"
    
            if ($RunningJobs.Count -eq $jobs.Count) {
                $timeout --
                Write-Progress -Activity $Activity -Status "$Status" -CurrentOperation "$($jobs | ? State -EQ "Running" | % {$_.Name})"
                Start-Sleep 1 
                Write-Progress -Activity $Activity -Status "$Status ." -CurrentOperation "$($jobs | ? State -EQ "Running" | % {$_.Name})"
                Start-Sleep 1 
                Write-Progress -Activity $Activity -Status "$Status .." -CurrentOperation "$($jobs | ? State -EQ "Running" | % {$_.Name})"
                Start-Sleep 1 
                Write-Progress -Activity $Activity -Status "$Status ..." -CurrentOperation "$($jobs | ? State -EQ "Running" | % {$_.Name})"
                Start-Sleep 1
            }
    
            if ($CompletedJobs) {
                $results += Receive-Job $CompletedJobs
                Remove-Job $CompletedJobs
            }
    
            if ($FailedJobs) {
                $results += Receive-Job $FailedJobs -ErrorAction Ignore
                If ($FailedJobs.ChildJobs) {
                    $ChildJobs = $FailedJobs.ChildJobs
                    $FailedComputers = $ChildJobs | ? State -EQ "Failed" | Select-Object -ExpandProperty Location
                }
                Remove-Job $FailedJobs
            }
    
            # Blocked job will not be treated.
            If ($blockedJobs.Count -eq $jobs.Count) {
                Write-Warning "There are some blocked jobs that will not be clean!"
                $blockedJobs
                Break
            }
    
            # Should be end up if 'NoWait' is present.
            if ($NoWait.IsPresent -and ($CompletedJobs -or $FailedJobs)) { break }
    
            If ($timeout -lt 1) {
                Write-Progress -Activity "Jobs running time out." -Status "Terminating ..."
                get-job | Remove-Job -Force
                Write-Host "Jobs running time out. Terminated!" -ForegroundColor Red
            }
        }
    
        If ($FailedComputers) {
            Write-Host "Job can't be run for below machines:"
            $FailedComputers | %{Write-Host $_}
        }
    
        Return $results
    }