Forum Discussion
extract a string from this @{Name=WEBHOST001-OI3w}
Hi rmerritt​,
Looking at your second post, it's just a string variable. It's purely coincidental that the value of the string happens to look similar to a valid PowerShell [hashtable] declaration.
The reason it's not a valid PowerShell [hashtable] declaration is the assigned value of "WEBHOST001-OI3w" is not surrounded by quotes (single or double will suffice for this example). If it were surrounded by quotes, you could call Invoke-Expression to treat the string as a parsable command as shown below in the first two examples (the first example shows the conversion to a [hashtable] while the second goes one step further to list the value of the "Name" key):
The third example from above (noting your value underlined in red which has no quotes) is how you can extract the string using string manipulation. But this is specific to the format of the value held in your $vcentername variable (which per your second post shows it to be a [string]) and won't work for different formats.
If you apply the third example to your variable, you get this as the code line you want to use to extract just the host name:
$vcentername.Split("=")[1].Replace("}", "");
Cheers,
Lain