# $ExecutionContext.InvokeCommand.ExpandString

**URL:** <https://forums.powershell.org/t/executioncontext-invokecommand-expandstring/5497>\
**Category:** PowerShell Help\
**Created:** [January 11, 2016, 3:19am UTC](https://forums.powershell.org/t/executioncontext-invokecommand-expandstring/5497 "2016-01-11T03:19:13Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![aaron-miller](https://avatars.discourse-cdn.com/v4/letter/a/a6a055/32.png) [@aaron-miller](https://forums.powershell.org/u/aaron-miller)\
**Post date:** [January 11, 2016, 3:19am UTC](https://forums.powershell.org/t/executioncontext-invokecommand-expandstring/5497/1 "2016-01-11T03:19:13Z")

</div>

I have a xml file that holds all the values for a script I’m writing, the below works fine on 2008, but on 2012 it gives me the following error:

```
Exception calling "ExpandString" with "1" argument(s): "Object reference not set to an instance of an object
At line:1 char:1
+ $ExecutionContext.InvokeCommand.ExpandString(($isecspec.TechSpec.Section | ? {$_ ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : NullReferenceException
```

The xml entry is (cut off the leading \< as the forum treats it as real code):

```
$($folder.name)
```

```
$folders = @{
		$ExecutionContext.InvokeCommand.ExpandString(($isecspec.TechSpec.Section | ? {$_.name -eq "yo.1.8.1"}).systemvalueparameter) = ($isecspec.TechSpec.Section | ? {$_.name -eq "yo.1.8.1"}).name;
}

foreach($folder in $folders.GetEnumerator())
{
	$notes = $ExecutionContext.InvokeCommand.ExpandString(($isecspec.TechSpec.Section | ? {$_.name -eq $folder.value}).notes."#cdata-section")
$perms = ($isecspec.TechSpec.Section | ? {$_.name -eq $folder.value}).agreedtovalue

$notes
$perms
}
```

I’m not sure why it works on 2008, but not 2012

---

<div class="post-metadata">

**Author:** ![aaron-miller](https://avatars.discourse-cdn.com/v4/letter/a/a6a055/32.png) [@aaron-miller](https://forums.powershell.org/u/aaron-miller)\
**Post date:** [January 11, 2016, 3:22am UTC](https://forums.powershell.org/t/executioncontext-invokecommand-expandstring/5497/2 "2016-01-11T03:22:44Z")

</div>

Well the xml part came out all wrong…wish there was a preview button! and it wont let me edit as well…odd…

Replaced all with -'s

```
-Section Name="YO.1.8.1"-
    -RunMode-S-/RunMode-
    -Foundation-N-/Foundation-
    -SectionNumber-YO.1.8.1-/SectionNumber-
	-notes--![CDATA[This fixlet checks the permissions for General Users on the following:-br--br--code-$($folder.name)-/code--br--br-The maximum authority granted to general users must be set to the Desired Value for -b-Special Permissions-/b-.-br-General Users are users who are not members of the groups listed in the Desired Value for -b-Windows System Privilege Groups-/b-.-br--br--b-Note:-/b- Please refer to the documentation for a detailed matrix of the relationship between Basic and Special Permissions.]]--/notes-
    -SectionHeading-Protecting Resources - OSRs-/SectionHeading-
    -SystemValueParameter-$env:SystemRoot-/SystemValueParameter-
    -Description-OSRs-/Description-
    -RecommendedValue-Read and Execute:::List Folder Contents:::Read-/RecommendedValue-
    -InitialValue-TBD-/InitialValue-
    -AgreedToValue-^(Read?(Execute|Data|Extended|Attributes|Permissions|And)+?|ExecuteFile|Synchronize|\s|,|-1610612736)+$-/AgreedToValue-
   -/Section-
```

---

<div class="post-metadata">

**Author:** ![aaron-miller](https://avatars.discourse-cdn.com/v4/letter/a/a6a055/32.png) [@aaron-miller](https://forums.powershell.org/u/aaron-miller)\
**Post date:** [January 11, 2016, 6:46am UTC](https://forums.powershell.org/t/executioncontext-invokecommand-expandstring/5497/3 "2016-01-11T06:46:56Z")

</div>

\<![CDATA[This fixlet checks the permissions for General Users on the following:-br–br–code-$($folder.name)-/code–br–br-The maximum authority granted to general users must be set to the Desired Value for -b-Special Permissions-/b-.-br-General Users are users who are not members of the groups listed in the Desired Value for -b-Windows System Privilege Groups-/b-.-br–br–b-Note:-/b- Please refer to the documentation for a detailed matrix of the relationship between Basic and Special Permissions.]]

The problems seems to be the $($folder.name)

If i just make it $folder.name it runs correctly but shows

```
System.Collections.DictionaryEntry.name
```

instead of

```
c:\Windows
```

Is there a way to have the value of the variable appear as a string without doing $($folder.name) that will parse correctly on 2012? It’s odd this works on 2008/win7 without issue.

---

<div class="post-metadata">

**Author:** ![\_timpringle](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/_timpringle/32/276_2.png) [@\_timpringle](https://forums.powershell.org/u/_timpringle)\
**Post date:** [January 12, 2016, 12:46am UTC](https://forums.powershell.org/t/executioncontext-invokecommand-expandstring/5497/4 "2016-01-12T00:46:32Z")

</div>

Hi ertuu85,

It’s possible to attach text files to forum posts. Can you do this for the code and the XML, as it’s a bit difficult to work out otherwise?

---

<div class="post-metadata">

**Author:** ![aaron-miller](https://avatars.discourse-cdn.com/v4/letter/a/a6a055/32.png) [@aaron-miller](https://forums.powershell.org/u/aaron-miller)\
**Post date:** [January 12, 2016, 1:17am UTC](https://forums.powershell.org/t/executioncontext-invokecommand-expandstring/5497/5 "2016-01-12T01:17:47Z")

</div>

The xml is located the below URL…i dont see how to attach files…

> **[S](https://pastebin.com/pWgPVduA)**
>
> Pastebin.com is the number one paste tool since 2002. Pastebin is a website where you can store text online for a set period of time.

The issues seems to be when I’m doing a $ExecutionContext.InvokeCommand.ExpandString on an xml entry that has $($variable) but i’m not sure how to otherwise display the variable as a string

---

<div class="post-metadata">

**Author:** ![aaron-miller](https://avatars.discourse-cdn.com/v4/letter/a/a6a055/32.png) [@aaron-miller](https://forums.powershell.org/u/aaron-miller)\
**Post date:** [January 12, 2016, 2:41am UTC](https://forums.powershell.org/t/executioncontext-invokecommand-expandstring/5497/6 "2016-01-12T02:41:41Z")

</div>

Resolved it by just changing the value in the XML.

Sorry for this whole mess an thank you Tim!

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:43pm UTC](https://forums.powershell.org/t/executioncontext-invokecommand-expandstring/5497/7 "2024-05-16T20:43:11Z")

</div>


