0

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.

  • What is the question? Generally, I don't think [what you are trying to do](https://stackoverflow.com/a/65250334/1701026) is a good idea, I would at least keep your custom variables separated in a different (flat) hashtable and address them as e.g.: `$My.NSOneDrive`. – iRon Feb 02 '23 at 07:07
  • Sorry, I forgot to add this to the "Goals" I'm trying to declare variables for the current session to simplify navigation using `Set-Location` – Richard Joubert Feb 02 '23 at 07:59
  • Also as far as setting a 'flat' `[PSCustomObject]` or Hash Table, I will resort to that if I can't find a way make it work with a multi-level `[PSCustomObject]` (but I know there has to be lol) – Richard Joubert Feb 02 '23 at 08:13
  • You probably want to create a [recursive function](https://en.wikipedia.org/wiki/Recursive_function) for this, see e.g.: [Convert nested JSON array into separate columns in CSV file](https://stackoverflow.com/a/46081131/1701026). – iRon Feb 02 '23 at 08:20
  • @iRon the `Flatten-Object` function definitely looks promising. Thanks! I'll play with that and we'll see where it goes. – Richard Joubert Feb 02 '23 at 08:25

0 Answers0