Skip to content

Instantly share code, notes, and snippets.

@SteveL-MSFT
Created March 31, 2022 22:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SteveL-MSFT/4479d8d034549cbfe22731891d0fccfd to your computer and use it in GitHub Desktop.
Save SteveL-MSFT/4479d8d034549cbfe22731891d0fccfd to your computer and use it in GitHub Desktop.
param(
[Parameter(ValueFromPipeline)]
[string]$json
)
$obj = ConvertFrom-Json -InputObject $json -Depth 100 -AsHashtable
$location = "/"
function Test-Location ($path) {
$paths = $path.Split('/')
return $true
}
function Get-Json ($filter) {
$cur = $obj
foreach ($path in $location.Split('/', [StringSplitOptions]::RemoveEmptyEntries)) {
$cur = $cur.$path
}
return ($cur | Out-String)
}
while($true) {
$i = Read-Host -Prompt $location
$command = $i.Split(' ')
switch ($command[0]) {
'cd' {
$newLocation = $command[1]
if ($newLocation -eq '..') {
if ($location -eq '/') {
Write-Error "At root"
continue
}
$location = Split-Path $location -Parent
if ("" -eq $location) {
$location = '/'
}
}
elseif (Test-Location $newLocation) {
$location = Join-Path $location $newLocation
}
else {
Write-Error "Path does not exist"
}
}
'dir' {
$filter = $command[1]
Write-Host (Get-Json $filter)
}
'exit' {
return
}
default {
Write-Error "Command not found. Use 'cd', 'dir', or 'exit'"
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment