PowerShell : slow data in a file

Say you have a script that gets data from a lot of different places, it’s slow to run when you are developing but you probably want to connect to them all when you run for real. If that’s you, then you can save the results of your slow connections in a file with just one line of script and keep using that file unless you have to re-get fresh data.

I’ve found using PoSh’s Export-CliXml and Import-CliXml to store the results from a slow operation nice and easy, but it can output any PoSh object.

Param
(
 $resultFile
 = (join-path $PSScriptRoot
 "resultsFile.xml"),
 $resultsFileRedo = $false
)
$bigListOfThings = get-ListOfThings
if($resultsFileRedo)
{
 write-host "about to get the slow data from $($bigListOfThings.count) things"
 foreach($eachThing in $bigListOfThings)
 {
 write-host "doing $eachThing"
 $eachThingData = Get-SlowDataThatTakesAges $eachThing
 $slowData.add($eachThing , $eachThingData)
 }
 del $resultFile
 $slowData | export-clixml $resultFile
}
else
{
 $slowData = import-clixml $resultFile
}
 


Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s