Stashing Data for dbatools
While working on an enhancement to dbatools, I had a need to stash a local copy of a file downloaded from the internet, but in a safe place that I could reasonably expect to be safe from accidental deletion.
-
User’s home directory? Maybe, but it’ll be clutter, the user might see it appear and fear that they’ve got malware. And likely deleted ina “cleanup” effort.
-
Create my own directory somewhere on the file system? See above.
-
A temp directory fetched from
env:temp
,env:tmp
, or[System.IO.Path]::GetTempPath()
? Well, it wouldn’t be hidden, but by definition it’ll be prone to getting purged. Not great for potential medium-term storage. -
Let the user specify a location at runtime? I don’t know about you, but I’ll forget about 5 minutes later and I want the parameters for this to be simple.
No good solutions there. Fortunately, the dbatools team has it covered. The module has a system for storing its own configuration settings and data/files and has a few settings pre-set for you. You can see the full list with Get-DbaConfig
:
In this case, the setting I’m looking for is called Path.DbatoolsData
. Accessing it is easy. Get-DbaConfigValue -Name "Path.DbatoolsData"
gets me the value of that setting - C:\Users\andy\AppData\Roaming\PowerShell\dbatools
in this case.
Combine this with ‘Join-Path’ and I’ve got quick access to that file I tucked away for later. Join-Path -Path (Get-DbaConfigValue -Name "Path.DbatoolsData") -ChildPath "MyFile.zip"
returns C:\Users\andy\AppData\Roaming\PowerShell\dbatools\MyFile.zip
You can create your own configuration settings & values via Set-DbaConfig
but be warned: these do not persist across sessions. If you want to persist configuration values across sessions, you’ll need to write them out to a file, then read them in from that file in the new session.