header1   header
header
header Register : : Login header
header
connector   connector
menuleft menuright
submenu   submenu
left
PS2.0: Help with Start-Job using Advanced Functions.
Last Post 22 Nov 2010 08:59 AM by get-james. 3 Replies.
Printer Friendly
  •  
  •  
  •  
  •  
  •  
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages
get-jamesUser is Offline
New Member
New Member
Posts:59
Avatar

--
20 Nov 2010 02:19 AM
    Hi,

    I have recently upgrade to version 2.0 of powershell and am trying to take advantage of the background jobs to increase the performance of my scripts.

    I have manage to figure out of to get a background job working with a function, by using splatting, invoke-command and start-job as per below:
     
    $iDiskJobDetails = @{
      Name = "Idisks"
      ScriptBlock = {
        invoke-command -scriptblock {
        Function GetRes($ComputerName)
        {
        #Using Splatting to combine parameters for Get-WmiObject
        $StrParams = @{
        ComputerName = $ComputerName;
        Class = "Win32_LogicalDisk";
        Namespace = "ROOT\CIMV2"
        }
            Get-wmiobject @StrParams
        }
     
        "LocalHost","Server01" | %{GetRes $_}
        }
    }
    }
     
    Start-Job @iDiskJobDetails
    Get-Job -Name Idisks
    $Results = Receive-Job -Name Idisks
    $Results | select SystemName,Name


    But looking at the above example, there must be way of making it more simpler and I would like to achieve something like below:

    #Create function:
    Function GetRes($ComputerName)
    {
        #Using Splatting to combine parameters for Get-WmiObject
        $StrParams = @{
        ComputerName = $ComputerName;
        Class = "Win32_LogicalDisk";
        Namespace = "ROOT\CIMV2"
        }
        Get-wmiobject @StrParams
    }

    #Run Start-Job
    Start-Job -scriptblock {”Server01”,”Sever02” | Foreach{GetRes $_}}

    I have tried the same above command with the following parameters but am still having some issues:

    start-job -InitializationScript {Function GetRes($ComputerName)
    {
         #Using Splatting to combine parameters for Get-WmiObject
         $StrParams = @{
         ComputerName = $ComputerName;
         Class = "Win32_LogicalDisk";
         Namespace = "ROOT\CIMV2"
         }
         Get-wmiobject @StrParams
    }
    } -scriptblock {”Server01”,”Sever02” | Foreach{GetRes $_}}

    Error Reported:

    Start-Job : Cannot bind parameter 'ScriptBlock'. Cannot convert the "GETRES ." value of type "System.String" to type "System.Management.Automation.ScriptBlock".



    Does anyone have of know where I can get a good example of Starting a “Start-Job” without loading the .ps1 file?

    Cheers
    James

    get-jamesUser is Offline
    New Member
    New Member
    Posts:59
    Avatar

    --
    21 Nov 2010 10:50 PM
    All,

    Managed to find another way, using Splatting and using -initializationScript:


    $StrParams = @{
    name = "GetDiskInfo";
    initializationScript =
    {
    Function GetRes($ComputerName)
    {
    #Using Splatting to combine parameters for Get-WmiObject
    $StrParams = @{
    ComputerName = $ComputerName;
    Class = "Win32_LogicalDisk";
    Namespace = "ROOT\CIMV2"
    }
    Get-wmiobject @StrParams
    Write-Host "Hello4"
    }
    };
    scriptblock = {"LocalHost","Server01" | %{GetRes $_}}
    }

    start-job @StrParams
    Get-Job -Name GetDiskInfo | Receive-Job -Keep

    Any comments to improve would be great.

    Cheers
    James
    get-jamesUser is Offline
    New Member
    New Member
    Posts:59
    Avatar

    --
    22 Nov 2010 08:14 AM
    Another question.

    When i run this command on an XP SP3 machine pointing at Server 2003 servers, the job hangs at the "Running" state:


    State : Running
    HasMoreData : True
    StatusMessage :
    Location : localhost
    Command : "Server01","LocalHost" | %{GetRes $_}
    JobStateInfo : Running
    Finished : System.Threading.ManualResetEvent
    InstanceId :
    Id : 1
    Name : GetDiskInfo
    ChildJobs : {Job2}
    Output : {}
    Error : {}
    Progress : {}
    Verbose : {}
    Debug : {}
    Warning : {}

    Any ideas?
    get-jamesUser is Offline
    New Member
    New Member
    Posts:59
    Avatar

    --
    22 Nov 2010 08:59 AM
    Looking at this URL: http://social.technet.microsoft.com...hell+Forum


    This works:

    Start-Job -ScriptBlock {powershell.exe -NoProfile -command {
    Function GetRes($ComputerName)
    {
    $StrActiveClientLogons = get-wmiobject -Class Win32_PerfFormattedData_MSExchangeIS_MSExchangeISMailbox -Namespace ROOT\CIMV2 -ComputerName $Computername | Select-Object __SERVER,Name,ActiveClientLogons
    $StrActiveClientLogons | Select-Object `
    @{Label='ComputerName';Expression={$_.__SERVER}},
    @{Label='SG Name';Expression={$_.Name}},
    @{Label='Active Logons';Expression={$_.ActiveClientLogons}}
    }
    "Server01","Server02","Server03" | %{GetRes $_}
    }
    }
    You are not authorized to post a reply.


    Active Forums 4.3
    right
    footer   footer
    footer Sponsored by Quest Software • SAPIEN Technologies • Compellent • Microsoft Windows Server 2008 R2 footer
    footer   footer