PowerShell V2 CTP3 teaser : list PST files on a list of computers using background jobs
At work I had to list al PST files on a list big of computers, as searching for PST files is a timeconsuming job it should be possible to run the job concurently, so I thought that it was a good opportunity to try in PowerShell V2.
as I have "The PDC build" of WIndows 7 running, that has a Post-CTP2 version of PowerShell included I prototyped this example., As I noted that he needed CTP2 for it to run, as seemed after he dowloaded the CTP2 build of PowerShell V2, that it was not possible in yet CTP2, As CPT3 is not yet released to general public (but will be sometime this Month) , he could not use it, but I still decided to post it on my blog as a teaser (sorry, but it was so Cool I could not resist) :
# start job, with max of 10 Job running at the same time
$job = get-wmiobject -query "Select * from CIM_DataFile Where Extension = 'pst'" -ComputerName (Get-Content C:\powershell\computers.txt) -asJob -ThrottleLimit 10
# check status
$job
# Display results on screen, preserving the data
Receive-Job -Keep $job | select CSName,FileName,Description,FileSize
# when all jobs are finished, we can export the results to a CSV file
Receive-Job $job | select CSName,FileName,Description,FileSize | Export-Csv PstFiles.csv –NoTypeInformation
You can see it is, when you have the needed WQL query sting, simple enough, so simple even that we do not even need a "Stinking Script" anymore, we can just do it it interactive in a console session and check the results inbetween before exporting it to a CSV file in another simple oneliner :
The result looks like this :
PS C:\PowerShell> get-wmiobject -query "Select * from CIM_DataFile Where Extension = 'pst'" -ComputerName (Get-Content
C:\powershell\computers.txt) -asJob -ThrottleLimit 10
PS C:\PowerShell>
PS C:\PowerShell> $job = get-wmiobject -query "Select * from CIM_DataFile Where Extension = 'pst'" -ComputerName (Get-C
ntent C:\powershell\computers.txt) -asJob -ThrottleLimit 10
PS C:\PowerShell>
PS C:\PowerShell>
PS C:\PowerShell>
PS C:\PowerShell> # check status
PS C:\PowerShell>
PS C:\PowerShell>
PS C:\PowerShell>
PS C:\PowerShell> $job
Id Name State HasMoreData Location Command
-- ---- ----- ----------- -------- -------
5 Job5 Running False localhost,foo,loc... Get-WMIObject
PS C:\PowerShell>
PS C:\PowerShell>
PS C:\PowerShell>
PS C:\PowerShell> # Display results on screen, preserving the data
PS C:\PowerShell>
PS C:\PowerShell>
PS C:\PowerShell>
PS C:\PowerShell> Receive-Job -Keep $job | select CSName,FileName,Description,FileSize
PS C:\PowerShell> Receive-Job -Keep $job | select CSName,FileName,Description,FileSize
Receive-Job : The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)
At line:1 char:12
+ Receive-Job <<<< -Keep $job | select CSName,FileName,Description,FileSize
+ CategoryInfo : InvalidResult: (:) [Receive-Job], COMException
+ FullyQualifiedErrorId : JobStateFailed,Microsoft.PowerShell.Commands.ReceiveJobCommand
PS C:\PowerShell> $job.ChildJobs
Id Name State HasMoreData Location Command
-- ---- ----- ----------- -------- -------
6 Job6 Completed True localhost
7 Job7 Failed False foo
8 Job8 Completed True localhost
PS C:\PowerShell> Receive-Job -Keep $job | select CSName,FileName,Description,FileSize
CSName FileName Description FileSize
------ -------- ----------- --------
MOW7 mow c:\users\mow\appdata\local... 271360
Receive-Job : The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)
At line:1 char:12
+ Receive-Job <<<< -Keep $job | select CSName,FileName,Description,FileSize
+ CategoryInfo : InvalidResult: (:) [Receive-Job], COMException
+ FullyQualifiedErrorId : JobStateFailed,Microsoft.PowerShell.Commands.ReceiveJobCommand
As we needed this in production we in the end used another solution, but you can see how easy this will become in PowerShell V2
Enjoy soon,
Greetings /\/\o\/\/