Yes! It is possible to execute PowerShell Core in Windows PowerShell in a very creative way. Of course, this is just a Proof-Of-Concept and fun to experiment.
In this scenario, the goal is to be able to rebuild the PowerShell Core PSObject in Windows PowerShell. and see the results.
What’s required?
First, if PowerShell Core is already installed, then that’s all we needed. The ingredient are there and we are the chef to create the recipe.
There are four main cmdlets to make receipe works:
1. Start-Process – This cmdlet will run PowerShell Core non-interactive.
2. Export-Clixml – This cmdlet will export to an XML file the PSObject from PowerShell Core session.
3. Import-Clixml – This cmdlet will import from an XML file to recreate the PowerShell Core PSObject in Windows PowerShell.
4. Select-Object – This cmdlet is use to display the output of that object.
Think of the *-Clixml cmdlets as the disconnected way to reuse PSObject in a disconnected way, is like saving the state of the object for testing.
The Recipe
Basically, Start-Process is where the PowerShell Core is run to execute the script. Now, within the PowerShell Core script, it is required to include the Export-Clixml cmdlet. This is where the PowerShell Core Object created in the script will export to an XML file. Finally, importing the same XML file in Windows PowerShell to be worked on and the displaying the PowerShell Core results in Windows PowerShell session.
Now, it’s very important to study and understand the PowerShell PSObject. Also, do all necessary test to see if there are any limitations in this process.
The Code
Here’s the basic code to accomplish this Proof-Of-Concept:
[sourcecode language=”powershell”]
## ————— Proof of concept ————— ##
## – Build PWSH process for Windows PowerShell Form:
## – Create Scriptblock to execute in PowerShell Core:
$pwshScriptBlock = @’
(Get-Variable PSVersionTable).value.GetEnumerator() `
| Where-Object{ $_.Name -eq ‘GitCommitId’ } `
| Select-Object Name, Value `
| Export-Clixml -Path C:\Temp\PwshCoreObject.xml;
‘@;
## – Set file path to PowerShell Core Executable:
$FilePath = "C:\Program Files\PowerShell\6.0.2\pwsh.exe";
## – Set executable parameters to run with Scriptblock:
$ArgumentList = ‘-noprofile -nologo -NonInteractive -w hidden -command "& {‘ + $pwshScriptBlock + ‘}"’;
## – Execute PowerShell Core process:
$Process = Start-Process -FilePath $FilePath -ArgumentList $ArgumentList -PassThru;
## – Import pwsh CliXml object back to Windows PowerShell PSObject:
$pwshPSObject = Import-Clixml -Path "C:\Temp\PwshCoreObject.xml";
## – Display results from PowerShell Core CliXml file:
$pwshPSObject | Select-Object -Property $_;
[/sourcecode]
The code is executed from a Windows PowerShell session. Try it, experiment, and test. See if it’s fit your need in any way.
Conclusion
Please, I’m not saying that this the way to go. But, it just showing one way to handle sharing results between Windows PowerShell and PowerShell Core. Again this is just for fun, and at the same time learning more about PowerShell.
The possibilities are endless! (Teaser)
Be Bold!! Learn PowerShell Core!!