TLDR
I'm trying to create a function that will take a Multi-Level [PSCustomObject]
, extract the Key/Value
pairs (strings
only), and use them to declare Individual Global Variables using Set-Variable
.
Current Code
Set-Variable -Name 'NSOneDrive' -Value "D:\OneDrive - New Spectrum"
$StrykerDirs = [PSCustomObject]@{
'OneDrive' = [PSCustomObject]@{
'NSOneDrive' = "D:\OneDrive - New Spectrum"
'MyOneDrive' = "D:\OneDrive"
}
'Dev' = [PSCustomObject]@{
'DevDir' = "${NSOneDrive}\Dev"
'DevToolsDir' = [PSCustomObject]@{
'DevTools' = "${NSOneDrive}\Dev\_DevTools"
'Terminals' = [PSCustomObject]@{
'DT_Terminals' = "${NSOneDrive}\Dev\_DevTools\terminals"
'DT_PowerShell' = "${NSOneDrive}\Dev\_DevTools\terminals\PowerShell"
}
'Editors' = [PSCustomObject]@{
'DT_Editors' = "${NSOneDrive}\Dev\_DevTools\.editors"
}
}
'ProjectsDir' = [PSCustomObject]@{
'NSProjects' = "${NSOneDrive}\Projects\NewSpectrum"
'MyProjects' = "${NSOneDrive}\Projects\Personal"
}
}
}
$StrykerDirs |
ConvertTo-JSON -Depth 25 |
Tee-Object -FilePath ".\JSON\Stryker-Paths.json"
function Set-DirAliases {
[CmdletBinding()]
# I might add parameters after I know how to make the 'Process' work
Begin {
# Begin Process Block
}
Process {
ForEach ( $dir in $StrykerDirs ) {
where ( $_.GetType() -eq 'String' ) |
Set-Variable -Name "${key}" -Value "${value}"
# I know ${key} and ${value} won't work, but I'm not sure how to properly fill them
}
}
End {
# End Process Block
}
}
Goals
Simplifying Set-Location
Navigation
First and foremost I obviously need to figure out how to make the above Process
block work. Once I do, I'll be able to easily declare Directory Variables for use with Set-Location
. This is only for streamlining variable declarations so I don't have to repeatedly declare them with a messy barrage of individual Set-Variable
commands while also avoiding the use of long (sometimes very long) $Object.PropertyName
'variables'.
After I get a handle on this script, I'll be able to finish several other scripts and functions that use (more or less) the same basic process.
Add to $PROFILE
This particular script is going to be part of a 'Startups' section in my default $PROFILE
(Microsoft.PowerShell_profile.ps1
) so I can set the Directory Variables in-bulk and keep the $PROFILE
script itself nice and clean.
The other scripts that I mentioned anbove are also going to be included in my $PROFILE
Startups.
JSON Output
The script also exports a .json
file so that, among other things, I can (hopefully) repeat the process down the road in my WSL Bash Profiles.
Param()
Functionality
Eventually I want to add a Param()
block so the function can be used outside of the script as well.