Check Existence of server with search progress and time took to retrieve the server (if present in the domain)

  • Hi,

    I am trying to search if a server is present, it will show the progress (using write-progress activity in a loop) and tell me the time it took to fetch the details. Unable to compose the script to check the time takes to fetch the server record and progress, Please help/suggest.

    clear-host

    function checkserver

    {

    $Global:instance= Read-Host "Enter the SQL Service Instance Name [Dont use "" "" in the servername]:`n"

    write-host "You entered server name as:" $instance

    $serverObject = New-Object Microsoft.SqlServer.Management.Smo.Server($instance)

    $instance = $instance.toupper()

    $serverName = $serverObject.ComputerNamePhysicalNetBIOS

    $serverName = $serverName +"\"+$serverObject.InstanceName

    #$serverName

    IF ( $serverName -ne "\")

    {

    $sess = new-pssession $serverName

    Print $serverName

    invoke-command -session $sess -scriptblock {start-job -name Date1 -scriptblock {get-date}}

    write-host "Server:" "'$serverName'" "exist" -ForegroundColor Green

    Write-Progress -Activity "Working..." `

    $done = invoke-command -session $sess -command {wait-job -name Date1}

    $done.count

    }

    ELSEIF ( $serverName -eq "\")

    {

    #$sess = new-pssession $serverName

    #invoke-command -session $sess -scriptblock {start-job -name Date1 -scriptblock {get-date}}

    write-host "Wrong Server name" -ForegroundColor Red

    #$done = invoke-command -session $sess -command {wait-job -name Date1}

    #$done.count

    }

    }

    Thanks.

  • Here you go:

    $begin = Get-Date

    $end = Get-Date

    $timeTaken = $end - $begin

    Am I your PowerShell Guardian Angel now??? 😉

    Gaz

    -- Stop your grinnin' and drop your linen...they're everywhere!!!

  • Thanks Gary.

    I am learning Powershell and trying to automate few of the stuff. Hence, needed expert advice.

    **** This one I re-modified. *****

    clear-host

    $instance =""

    function checkserver

    {

    $instance= Read-Host "Enter the SQL Service Instance Name [Dont use "" "" in the servername]:"

    write-host "`n You entered server name as:["$instance.trim() "]" -ForegroundColor Yellow

    $serverObject = New-Object Microsoft.SqlServer.Management.Smo.Server($instance)

    $instance1 = $instance.toupper()

    $instance = $instance1.trim();

    $serverName = $serverObject.ComputerNamePhysicalNetBIOS

    $serverName1 = $serverName +"\"+$serverObject.InstanceName

    IF ( $serverName1 -NE "\")

    {

    write-host " `n Server: *****" "'$serverName1'" "**** exist `n" -ForegroundColor Green

    $serverName10 = $serverName1

    IF ($serverName10 -notcontains "\*"){

    write-host "'$serverName10'" is a Named Instance -ForegroundColor Magenta

    }

    ELSE {

    write-host "'$serverName10'" is a Default Instance -ForegroundColor DarkGray

    }

    }

    ELSEIF ( $serverName1 -EQ "\")

    {

    write-host "`n Wrong Server name entered:[" $instance "]" -ForegroundColor Red

    }

    }

    ## EXECUTE ##

    checkserver

    Thanks.

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply