2

I want to use multi-threading feature in powershell , hence I ventured into using Start-Job module.

Below is the script that I am using to execute the block of code on multiple servers

function get-isSystemUp($server,$serviceName)
    {
        $state=$null;
        Write-Host "Server Info:" $server    $serviceName;
        if(Test-Connection -BufferSize 2 -Count 1 -ComputerName $server -Quiet ) {
            $services=Get-Service -ComputerName $server -Name $serviceName -EA Stop |Select-Object Name, Status
            if($services.Status -ieq "running"){
                $state=$true;
            }else{
                Write-Host "Service not running";
                $state=$false;
            }
        }else{
            Write-Host "Test connection not working";
            $state=$false;
        }


    return $state;
}  
$SQLDetail={
        Param([String]$FarmInstanceName,[String]$serverName,[String]$sqlInstanceName,[String]$serviceName) 
        Write-host "##########################################################"
        Write-host "###############Server parsed:$serverName##################"
        Write-host "##########################################################"
        #Creating SQL OBject for the server

        $systemState=get-isSystemUp -server $serverName -serviceName $serviceName
}
ForEach($dataread in $ServerList)  
{ 
    #$sqlInstanceName = $computername1
    $dataread = $dataread -split (",")
    $FarmInstanceName = $dataread[0]
    $serverName=$dataread[1]
    $sqlInstanceName = $dataread[2]
    $farms += @(  
            $FarmInstanceName
    )
    Start-Job -scriptblock $SQLDetail  -ArgumentList $FarmInstanceName,$serverName,$sqlInstanceName,$serviceName
}
Get-Job | Wait-Job 
$out = Get-Job | Receive-Job 
$out
Get-Job|Remove-Job

Above code checks if the server is up and running and get-isSystemUp is a user defined function and below is the error I receive which indicates the get-isSystemUp function does not exist.

The term 'get-isSystemUp' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify 
that the path is correct and try again.
    + CategoryInfo          : ObjectNotFound: (get-isSystemUp:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
    + PSComputerName        : localhost

Can you please let me know on how to call a user defined function inside a scritptblock.

SQLDoctor
  • 343
  • 7
  • 16

0 Answers0