Search code examples
powershellwmistart-job

Powershell - filtering WMIObject show two scripts result


im new beginner of powershell, now i have two script, one is get remote server's IPs, another one is get remote server's specific service start time, i need to show remote server's IP and specific service start time, can someone guide me how to merge these two script.

below is my two script.

$servers = gc -path D:\Ted\Computers.txt

$Job = get-wmiobject win32_networkadapterconfiguration -computer $servers -filter "IPEnabled='True'" -asjob

$results = $job | receive-job

$results


get-job | wait-job

receive-job job* | select IPAddress

and another one for get service start time is

$servers = gc -path D:\Ted\Computers.txt

$check = get-wmiobject win32_process -computer $servers -Filter "Name='aspnet_state.exe'" -asjob

$results = $check | receive-job

$results


get-job | wait-job

receive-job job* | Select-Object name, processId, @{Name="StartTime"; Expression={ $_.ConvertToDateTime( $_.CreationDate )}}

at last i need know one thing, If I use asjob to this script, that means it is multi-threaded execution?

sorry for my poor english, thank you for your kindly help.


Solution

  • There is probably a cleaner way to do this, but here is my take on your problem. It looks as if you need some way to correlate each computer to the output of the two WMI queries. If it is a requirement to run this in parallel using jobs, it will take a bit more work, but here is a serial version.

    Get-Content -Path D:\Ted\Computers.txt | ForEach-Object {
        $ip = Get-WmiObject Win32_NetworkAdapterConfiguration -Computer $_ -Filter "IPEnabled='True'" | Select-Object IPAddress
        $process = Get-WmiObject Win32_Process -Computer $_ -Filter "Name='aspnet_state.exe'" | Select-Object Name, ProcessId, @{ Name="StartTime"; Expression = { $_.ConvertToDateTime($_.CreationDate) } }
    
        @{
            Computer = $_
            Ip = $ip
            Name = $process.Name
            ProcessId = $process.ProcessId
            StartTime = $process.StartTime
        }
    }
    

    A parallel version would be something along the lines of this:

    # A collection that stores all the jobs
    $AllJobs = @()
    
    # A collection that stores jobs correlated with the computer
    $ComputerJobs = Get-Content -Path D:\Ted\Computers.txt | ForEach-Object {
        $ipJob = Get-WmiObject Win32_NetworkAdapterConfiguration -Computer $_ -Filter "IPEnabled='True'" -AsJob 
        $AllJobs += $ipJob
    
        $processJob = Get-WmiObject Win32_Process -Computer $_ -Filter "Name='aspnet_state.exe'" 
        $AllJobs += $processJob
    
        @{
            Computer = $_
            IpJob = $ipJob
            ProcessJob = $processJob
        }
    }
    
    # Wait for everything to complete
    Wait-Job -Job $AllJobs
    
    # Iterate the correlated collection and expand the results
    $ComputerJobs | ForEach-Object {
        $ip = Receive-Job -Job $_.IpJob | Select-Object IPAddress
        $process = Receive-Job -Job $_.ProcessJob | Select-Object Name, ProcessId, @{ Name="StartTime"; Expression = { $_.ConvertToDateTime($_.CreationDate) } }
    
        @{
            Computer = $_.Computer
            Ip = $ip
            Name = $process.Name
            ProcessId = $process.ProcessId
            StartTime = $process.StartTime
        }
    }