Reputation: 3643
Here is a barebones code of what I am trying to achieve..
$destinationDir = "subdir1"
#creating tab
$newTab = $psise.PowerShellTabs.Add()
Do
{sleep -m 100}
While (!$newTab.CanInvoke)
#running required script in tab
$newTab.Invoke({ cd $destinationDir})
Since $destinationDir is initialized in the parent tab, its scope is limited to it and I get the following error in the child tab
cd : Cannot process argument because the value of argument "path" is null. Change the value of argument "path" to a non-null value.
How do I overcome this and use the value in the child tab?
Upvotes: 3
Views: 1212
Reputation: 4454
Short answer: You can't. Each tab in PowerShell ISE is created with a new runspace. There is no method provided, for injecting variables into this runspace.
Long answer: There are always workarounds. Here are two.
1. Use the invoke script-block to transfer the variable to the new runspace:
$destinationDir = "subdir1"
#creating tab
$newTab = $psise.PowerShellTabs.Add()
Do
{sleep -m 100}
While (!$newTab.CanInvoke)
$scriptblock = "`$destinationDir = `"$($destinationDir)`"
cd `$destinationDir"
#running required script in tab
$newTab.Invoke($scriptblock)
2. Use en environmental variable:
$env:destinationDir = "subdir1"
#creating tab
$newTab = $psise.PowerShellTabs.Add()
Do
{sleep -m 100}
While (!$newTab.CanInvoke)
#running required script in tab
$newTab.Invoke({ cd $env:destinationDir})
Upvotes: 2