在Powershell中调用scriptblock中的用户定义函数

时间:2017-08-24 12:56:14

标签: multithreading powershell start-job

我想在PowerShell中使用多线程功能,因此我冒险尝试使用Start-Job模块。

下面是我用来在多个服务器上执行代码块的脚本

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

上面的代码检查服务器是否已启动并且正在运行且get-isSystemUp是用户定义的函数,以下是我收到的错误,表示get-isSystemUp函数不存在。

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

请告诉我如何在scritptblock中调用用户定义的函数。

0 个答案:

没有答案