PowerShell on Linux

When working in a heterogeneous envrionment with different types of OS (Windows, Linux etc.), you probably want to manage systems from a central place. People usually have a RDP session of Windows, then using a multi connection manager like mRemoteNG to connect those systems.

To automate your tasks with scripts, you want them fired from one place as well, either from a Linux box or from a Windows box. PowerShell on Linux from Microsoft provides the possibility to run PowerShell script from a Linux server.

To install PowerShell on your Linux system, follow the link below to install it for your distribution:

https://docs.microsoft.com/en-us/powershell/scripting/install/installing-powershell-on-linux?view=powershell-7.2

Below is the example which I installed PowerShell 7.2 on my Oracle Linux 8. I still used the command yum which is just a symbolic link of dnf on OL8. First intall the powershell repository with curl. Then install powershell itself.

curl https://packages.microsoft.com/config/rhel/8/prod.repo |tee /etc/yum.repos.d/microsoft.repo
yum clean all
yum repolist
yum repoinfo
yum install powershell
root@joeol8test:~# curl https://packages.microsoft.com/config/rhel/8/prod.repo |tee /etc/yum.repos.d/microsoft.repo
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   192  100   192    0     0    888      0 --:--:-- --:--:-- --:--:--   888

[packages-microsoft-com-prod]
name=packages-microsoft-com-prod
baseurl=https://packages.microsoft.com/rhel/8/prod/
enabled=1
gpgcheck=1
gpgkey=https://packages.microsoft.com/keys/microsoft.asc

root@joeol8test:~# cat /etc/yum.repos.d/microsoft.repo
[packages-microsoft-com-prod]
name=packages-microsoft-com-prod
baseurl=https://packages.microsoft.com/rhel/8/prod/
enabled=1
gpgcheck=1
gpgkey=https://packages.microsoft.com/keys/microsoft.asc

yum clean all
yum repolist
yum repoinfo

root@joeol8test:/etc/yum.repos.d# yum install powershell
Last metadata expiration check: 0:01:11 ago on Fri 20 May 2022 08:45:10 AM CDT.
Dependencies resolved.
===================================================================================================================================================================================================================
 Package                                        Architecture                               Version                                           Repository                                                       Size
===================================================================================================================================================================================================================
Installing:
 powershell                                     x86_64                                     7.2.4-1.rh                                        packages-microsoft-com-prod                                      66 M

Transaction Summary
===================================================================================================================================================================================================================
Install  1 Package

Total download size: 66 M
Installed size: 183 M
Is this ok [y/N]: y
Downloading Packages:
powershell-7.2.4-1.rh.x86_64.rpm                                                                                                                                                    16 MB/s |  66 MB     00:04
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Total                                                                                                                                                                               16 MB/s |  66 MB     00:04
packages-microsoft-com-prod                                                                                                                                                        5.0 kB/s | 983  B     00:00
Importing GPG key 0xBE1229CF:
 Userid     : "Microsoft (Release signing) <gpgsecurity@microsoft.com>"
 Fingerprint: BC52 8686 B50D 79E3 39D3 721C EB3E 94AD BE12 29CF
 From       : https://packages.microsoft.com/keys/microsoft.asc
Is this ok [y/N]: y
Key imported successfully
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Preparing        :                                                                                                                                                                                           1/1
  Installing       : powershell-7.2.4-1.rh.x86_64                                                                                                                                                              1/1
  Running scriptlet: powershell-7.2.4-1.rh.x86_64                                                                                                                                                              1/1
  Verifying        : powershell-7.2.4-1.rh.x86_64                                                                                                                                                              1/1

Installed:
  powershell-7.2.4-1.rh.x86_64

Complete!

Once installed, you can use the command “pwsh” to enter the interactive mode. However to manage a remote Windows server, usually you still need to do a few extra things:

  1. install WSMan module or you will see the error like “Invoke-Command: This parameter set requires WSMan, and no supported WSMan client library was found. WSMan is either not installed or unavailable for this system.
  2. install the gssntlmssp package which enables GSSAPI (Generic Security Services API) NTLMSSP (NT LAN Manager Security Support Provider for Microsoft Windows) — NTLM authentication or you will see the error like “Connecting to remote server xxxxx failed with the following error message : acquiring creds with username only failed Unspecified GSS failure. Minor code may provide more information SPNEGO cannot find mechanisms to negotiate For more information, see the about_Remote_Troubleshooting Help topic.

To install WSMan:

Install-Module -Name PowerShellGet
Install-Module -Name PSWSMan
Install-WSMan
root@joeol8test:~# pwsh
PowerShell 7.2.4
Copyright (c) Microsoft Corporation.

https://aka.ms/powershell
Type 'help' to get help.

PS /root> Install-Module -Name PowerShellGet

Untrusted repository
You are installing the modules from an untrusted repository. If you trust this repository, change its InstallationPolicy value by running the Set-PSRepository cmdlet. Are you sure you want to install the
modules from 'PSGallery'?
[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help (default is "N"): A
PS /root> Install-Module -Name PSWSMan

Untrusted repository
You are installing the modules from an untrusted repository. If you trust this repository, change its InstallationPolicy value by running the Set-PSRepository cmdlet. Are you sure you want to install the
modules from 'PSGallery'?
[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help (default is "N"): A
PS /root> Install-WSMan
WARNING: WSMan libs have been installed, please restart your PowerShell session to enable it in PowerShell
PS /root> exit

Note, to get WSMan working, you will need to run pwsh with sudo.

To install the gssntlmssp package, first install the epel repository.

yum install -y epel-release
yum install gssntlmssp
root@joeol8test:~# yum search gssntlmssp
Last metadata expiration check: 0:34:21 ago on Fri 20 May 2022 09:51:16 AM CDT.
======================================================================================== Name Exactly Matched: gssntlmssp =========================================================================================
gssntlmssp.src : GSSAPI NTLMSSP Mechanism
gssntlmssp.x86_64 : GSSAPI NTLMSSP Mechanism
root@joeol8test:~# yum install gssntlmssp
Last metadata expiration check: 0:34:32 ago on Fri 20 May 2022 09:51:16 AM CDT.
Dependencies resolved.
===================================================================================================================================================================================================================
 Package                                            Architecture                                   Version                                             Repository                                             Size
===================================================================================================================================================================================================================
Installing:
 gssntlmssp                                         x86_64                                         0.7.0-6.el8                                         ol8_appstream                                          65 k

Transaction Summary
===================================================================================================================================================================================================================
Install  1 Package

Total download size: 65 k
Installed size: 130 k
Is this ok [y/N]: y
Downloading Packages:
gssntlmssp-0.7.0-6.el8.x86_64.rpm                                                                                                                                                  189 kB/s |  65 kB     00:00
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Total                                                                                                                                                                              187 kB/s |  65 kB     00:00
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Preparing        :                                                                                                                                                                                           1/1
  Installing       : gssntlmssp-0.7.0-6.el8.x86_64                                                                                                                                                             1/1
  Running scriptlet: gssntlmssp-0.7.0-6.el8.x86_64                                                                                                                                                             1/1
  Verifying        : gssntlmssp-0.7.0-6.el8.x86_64                                                                                                                                                             1/1

Installed:
  gssntlmssp-0.7.0-6.el8.x86_64

Complete!

Now you can test your script, for example,

root@joeol8test:~# cat b.ps1
$User = "DOMAIN\user1"
$PWord = ConvertTo-SecureString -String "passwd1" -AsPlainText -Force
$Credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $User, $PWord
invoke-command -ComputerName joewindows {Get-Service "W32*"} -Credential $Credential |sort-object status,PSComputerName|Format-table -AutoSize

root@joeol8test:~# pwsh b.ps1

Status  Name    DisplayName  PSComputerName
------  ----    -----------  --------------
Running W32Time Windows Time joewindows

Have fun with PowerShell scripting on your favorite Linux box!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s