Create hashtable variable within a hashtable

I’m trying to resolve a value named TargetPathPath from a hashtable that’s in a hashtable, but can’t get it to work. Can someone see why it’s not working?

$prog=@(New-Object -TypeName psobject -Property @{WD="C:\windows\system32\"
TargetPathPath="C:\cms\Support\"
File="cmd.exe"
App="/c `$`_.TargetPathPath + Desktopinfo.exe"
})

$prog | ForEach-Object {
$pop=$_.path
Start-Process -WorkingDirectory $_.WD -FilePath $_.File -ArgumentList $_.App
}

I tried Invoke-Expression, but couldn’t get that to work either.

If I’ve got you right you’re looking for something like this:

$prog = @(
@{
WD = ‘C:\windows\system32’;
TargetPathPath = ‘C:\cms\Support’;
File = ‘cmd.exe’;
App = ‘/c `$`_.TargetPathPath + Desktopinfo.exe’
}
)

$prog |
ForEach-Object {
$pop = $.TargetPathPath
Start-Process -WorkingDirectory $
.WD -FilePath $.File -ArgumentList $.App
}


You create a variabel $pop without using it. You might read about_Splatting.

To format your code as code here in this forum you should use the code tag buttons “pre” right above the edit window when you create a post. Thanks … it’s even easier to see what’s wrong when you format your code nicely.

[quote quote=143745]If I’ve got you right you’re looking for something like this:

PowerShell
14 lines
<textarea class="ace_text-input" style="opacity: 0; height: 18px; width: 6.59781px; left: 83.9891px; top: 108px;" spellcheck="false" wrap="off"></textarea>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
$prog = @(
@{
WD = 'C:\windows\system32';
TargetPathPath = 'C:\cms\Support';
File = 'cmd.exe';
App = '/c `$`_.TargetPathPath + Desktopinfo.exe'
}
)
$prog |
ForEach-Object {
$pop = $_.TargetPathPath
Start-Process -WorkingDirectory $_.WD -FilePath $_.File -ArgumentList $_.App
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
You create a variabel $pop without using it. You might read about_Splatting.

To format your code as code here in this forum you should use the code tag buttons “pre” right above the edit window when you create a post. Thanks … it’s even easier to see what’s wrong when you format your code nicely.

[/quote]

Yea, I was messing around with different variables and rearranging values when I created the pop variable. i just forgot to remove it when I posted. I was in a rush to get home when I posted earlier and didn’t review all of the code.

I still can’t get that app to open with the code you posted. I know the reason why it’s not working, but don’t know how to get TargetPath to be resolved correctly when put in as a value like that.

Ahh … I didn’t pay attention to that part of your snippet. When you know the particular path of the executable you want to run with Start-Process in advance you could simply provide it …

$prog = @(    
    @{
        WD = 'C:\windows\system32';
        File = 'cmd.exe';
        App = '/c "C:\cms\Support\Desktopinfo.exe"'
    }
)

$prog | 
    ForEach-Object {
        Start-Process -WorkingDirectory $_.WD -FilePath $_.File -ArgumentList $_.App
    }

I still don’t know why you choose this particular way to launch a process. Anyhow I think it would be easier to use splatting … like this:

$params = @{
    WorkingDirectory = 'C:\windows\system32';
    FilePath         = 'cmd.exe';
    ArgumentList     = '/c "C:\cms\Support\Desktopinfo.exe"'
}
Start-Process @params

I was able to figure it out:

$prog=@(New-Object -TypeName psobject -Property @{WD="C:\windows\system32\"
TargetPathPath="C:\cms\Support\"
File="cmd.exe"
App=" /c `$(`$`_.TargetPathPath)" + "Desktopinfo.exe"
})

$prog | ForEach-Object {
Start-Process -WorkingDirectory $_.WD -FilePath $_.File -ArgumentList $ExecutionContext.InvokeCommand.ExpandString($_.App)
}

OK, cool. and just out of curiosity - could you please tell me what’s the advantage of your approach?

Check out splatting:

$prog = @{
    WorkingDirectory = "C:\windows\system32\"
    FilePath = "cmd.exe"
    ArgumentList = "/c","C:\cms\Support\Desktopinfo.exe"
}

Start-Process @prog