0

Asking for some help, as I have been getting nowhere with this all day...

I have a variable called $text, populated from a Sciencelogic API, which looks like:

echo $text

/api/device/9767/aligned_app/6B2C342727896B9BED64AC156E55B7E0 : @{app_name=VMware: VirtualMachine Configuration; thresholds=}
/api/device/9767/aligned_app/E28ED8DD41A89F3607DF8A204FF0475E : @{app_name=VMware: VirtualMachine CPU Performance; thresholds=}
/api/device/9767/aligned_app/660405AB28B347B968862DDF0D788C00 : @{app_name=VMware: VirtualMachine Datastore Performance; thresholds=}
/api/device/9767/aligned_app/96AD6BCF5EE58CC5CECC2DC17CE818AD : @{app_name=VMware: VirtualMachine Disk Performance; thresholds=}
/api/device/9767/aligned_app/E0326A53EFDCDD3BF395904B0F640D1B : @{app_name=VMware: VirtualMachine Memory Performance; thresholds=}
/api/device/9767/aligned_app/37AB770954C374322E321C93F8061662 : @{app_name=VMware: VirtualMachine Network Performance; thresholds=}
/api/device/9767/aligned_app/9B20E399FDCE3C4AD06769A7DA54047B : @{app_name=VMware: VirtualMachine Storage Performance; thresholds=}
/api/device/9767/aligned_app/3626E5EB5D83AF6EBAEF53F157E5904B : @{app_name=VMware: VirtualMachine Uptime Performance; thresholds=}

With the first number after device in the name being the device ID ($did) and the last string aligned_app ID ($appid). The variable $did is unique for every device and there are two $appid values.

To drill down into this data, I need to call a rather awkward variable name, such as:

echo $text.'/api/device/9767/aligned_app/E0326A53EFDCDD3BF395904B0F640D1B'

app_name                                  thresholds                     
--------                                  ----------                     
VMware: VirtualMachine Memory Performance @{100=; 201-5=; 201-8=; 201-6=}

And ultimately the threshold value I am after:

echo $text.'/api/device/9767/aligned_app/E0326A53EFDCDD3BF395904B0F640D1B'.thresholds.100

name                                 setting
----                                 -------
Physical Memory Utilization High     98.000 

Now the problem I have is that the $text is called in a foreach loop, where the IDs change. So what I really want to call is:

$did = 1234
$appid = "E0326A53EFDCDD3BF395904B0F640D1B"
$thresholds = $text.'/api/device/$did/aligned_app/$appid'.thresholds.100

But however I have tried I can't retrieve $text.'/api/device/$did/aligned_app/$appid'.thresholds.

I can as a string, but not as a 'call' to the data it holds, if you get my drift.

I am not sure how to describe this problem and better, to form a sentence which I can search the web for, so hoping someone her can assist.

yaquaholic
  • 152
  • 1
  • 11
  • 3
    Basic powershell: use doublequotes so the variables get interpreted. – js2010 Oct 14 '19 at 17:16
  • Please see the linked post and [this answer](https://stackoverflow.com/a/55614306/45375) for an overview of string literals in PowerShell. – mklement0 Oct 14 '19 at 18:55

1 Answers1

2
$did = '9767'
$appid = 'E0326A53EFDCDD3BF395904B0F640D1B'
$text.('/api/device/' + $did + '/aligned_app/' + $appid).thresholds.100
Patrick
  • 2,128
  • 16
  • 24
  • 1
    While your answer works, please note that you're not addressing the OP's fundamental misconception - expecting variables inside a single-quoted string to be expanded - and that simply using a double-quoted string instead is the simpler solution: `$text."/api/device/$did/aligned_app/$appid".thresholds.100` – mklement0 Oct 14 '19 at 20:14
  • totally true but I (my humble opinion ;-)) don't like variables inside the " " because it looks messy and might causes troubles if you don't take care... – Patrick Oct 14 '19 at 20:19
  • Understood, but your answer neither tells the OP what they did wrong, nor does it explain that and why you switched to a different approach. – mklement0 Oct 14 '19 at 20:22
  • Many thanks, to both of you – yaquaholic Oct 15 '19 at 09:19