Quantcast
Channel: PowerShell.org » All Posts
Viewing all articles
Browse latest Browse all 13067

Reply To: Invoke-Command Question on Remote File Server

$
0
0

Well, I’m back at it. This time, trying to optimize this as best as I can from a timeframe aspect. It seems that waiting on each Invoke-Command to finish on the current file server, output the results to Excel line-by-line, then proceed on to the remaining four servers one-by-one made the script finish in ~5 days.

Brainstorming a little bit, I thought of trying to setup an array to run the invoke-commands in parallel on each server as a separate job. But, I’m still back to my original question of how to store the job’s objects as a local variable.

Maybe I’m way off in left field on this – is there something from an optimization standpoint that would work better?

#region Setting Array of jobs and checking on their status
$arrayJobs = @()
$arrayJobs += $colFS10Files = Invoke-Command -ComputerName fileserver10 -AsJob -JobName fileserver10_Job -ScriptBlock {Get-ChildItem d:\home -Recurse -File | where {$_.CreationTime -gt ((Get-Date).AddDays(-7))} | Select-Object fullname,length,creationtime,@{n="owner";e={(Get-Acl $_.fullname).owner}}}

$arrayJobs += $colFS11Files = Invoke-Command -ComputerName fileserver11 -AsJob -JobName fileserver11_Job -ScriptBlock {Get-ChildItem d:\ -Recurse -File | where {$_.CreationTime -gt ((Get-Date).AddDays(-7))} | Select-Object fullname,length,creationtime,@{n="owner";e={(Get-Acl $_.fullname).owner}}}

$arrayJobs += $colFS12Files = Invoke-Command -ComputerName fileserver12 -AsJob -JobName fileserver12_Job -ScriptBlock {Get-ChildItem e:\home -Recurse -File | where {$_.CreationTime -gt ((Get-Date).AddDays(-7))} | Select-Object fullname,length,creationtime,@{n="owner";e={(Get-Acl $_.fullname).owner}}}

$arrayJobs += $colFS13Files = Invoke-Command -ComputerName fileserver13 -AsJob -JobName fileserver13_Job -ScriptBlock {Get-ChildItem d:\ -Recurse -File | where {$_.CreationTime -gt ((Get-Date).AddDays(-7))} | Select-Object fullname,length,creationtime,@{n="owner";e={(Get-Acl $_.fullname).owner}}}

$arrayJobs += $colFS14Files = Invoke-Command -ComputerName fileserver14 -AsJob -JobName fileserver14_Job -ScriptBlock {Get-ChildItem e:\ -Recurse -File | where {$_.CreationTime -gt ((Get-Date).AddDays(-7))} | Select-Object fullname,length,creationtime,@{n="owner";e={(Get-Acl $_.fullname).owner}}}

#monitor to see when all jobs are completed
$Complete = $false #$complete will be used to determine when all the jobs are completed
while (-not $complete) { #checks array to see if the job status is 'running'
    $arrayJobsInProgress = $arrayJobs | 
        Where-Object { $_.State -match 'running' }
    if (-not $arrayJobsInProgress) { "All Jobs Have Completed" ; $complete = $true } 
}

$arrayJobs = Receive-Job #return all jobs in their respective local variables containing the collection of new files


Viewing all articles
Browse latest Browse all 13067

Trending Articles