PowerShell Core – Collecting Information Cross-platform

Now, PowerShell Core has a new look with the new release version Beta.9. At the same time, the executable name has change from “powershell.exe” to “pwsh.exe” (or “pwsh in Linux).

The following example was shown by Microsoft Joey Aiello during the PowerShell Asia Conference in Singapore.  I, kind of, duplicated the steps showing how-to execute one command against two open PowerShell session: one on Linux, and another in Windows.

In my case, I’m on a workgroup environment. So, I have to enable my WinRM and PowerShell Remoting with the following easy steps (just accepting default options):

[sourcecode language=”powershell”]
## – Run quick setup:
winrm quickconfig -force
Enable-PSRemoting -force
.\Install-PowerShellRemoting.ps1

[/sourcecode]

Next, I got to add my trusted hosts in WinRm: adding both IP-Addresses, and system-names. By the way, I didn’t have top manual restart WinRm services.

[sourcecode language=”powershell”]
Set-Item wsman:\localhost\Client\TrustedHosts -Value 10.0.0.19
$curvalue = Get-Item wsman:\localhost\Client\TrustedHosts
Set-Item wsman:\localhost\Client\TrustedHosts -Value “$($curvalue.value), 10.0.0.40”
Get-Item wsman:\localhost\Client\TrustedHosts

Set-Item wsman:\localhost\Client\TrustedHosts -Value “$($curvalue.value), sapien01”
$curvalue = Get-Item wsman:\localhost\Client\TrustedHosts
Set-Item wsman:\localhost\Client\TrustedHosts -Value “$($curvalue.value), earth”
Get-Item wsman:\localhost\Client\TrustedHosts

[/sourcecode]

Next step is to create two open sessions: one on a Linux System and the other to a Windows System. Then verify that both system sessions are opened.

[sourcecode language=”powershell”]
$linuxSession = New-PSSession -HostName venus -UserName maxt
$windowsSession = New-PSSession -ComputerName earth -Credential max_t

Get-PSSession

[/sourcecode]

Please, notice that I’m using the “ssh” protocol to connect to my Linux system, and for Windows I’m using WinRm.

Finally, we can proceed to execute one command against these opened sessions, and see the merged results.

[sourcecode language=”powershell”]
Invoke-Command -Session $linuxSession, $windowsSession -ScriptBlock {Get-Process}

[sourcecode]

Thanks to Joey Aiello for this “Great!” demo showing the possibilities for cross-platform sample in collecting information between different systems.

Go Bold! With PowerShell Core!!