PowerShell – Useful Windows Bash commands to remember

This is for Linux Newbies when working with both Linux Ubuntu 16.04.01 and Windows Bash (which is based on Ubuntu 16.04.1). Please, get use the execute the following commands at least weekly as updates will be available frequently. And, specially before installing the next new release of PowerShell Open Source.

Look for PowerShell Open Source releases at: https://github.com/PowerShell/PowerShell

Follow these steps on a “Clean” Windows 10 Bash subsystem:

1. Run updates available:

[sourcecode language=”bash”]
$ sudo apt update
[/sourcecode]

2. After the update there might be upgrade available, so list all upgradable components:

[sourcecode language=”bash”]
$ apt list –upgradable
[/sourcecode]

3. If there’s a list of upgradable components then proceed with the upgrade step:

[sourcecode language=”bash”]
$ sudo apt upgrade
[/sourcecode]

When installing PowerShell Open Source after a clean Windows Bash installation. Follow the Github instructions as the installation will end in errors. You must run the following command:

[sourcecode language=”bash”]
$ sudo apt-get install -f
[/sourcecode]

But still, if it ends in error, try again the following commands on order:

[sourcecode language=”bash”]
$ sudo apt update
$ sudo apt upgrade
$ sudo apt install -f
[/sourcecode]

You’ll notice the error will be cleared.

Also, be patience when e executing ‘sudo apt upgrade’ as this command will take a little time to complete.

One important thing, pay close attention the either Linux and Windows Bash messages when executing these commands as they will give valuable information. These messages will provide the hint on what to do in case of errors.

Running PowerShell Open Source

Now you can proceed to run powershell from Windows Bash. But it’s not there yet, you’ll still experience the screen issues when executing commands. This has been already noted in Github and they are still working on it.

So, the workaround still applies: http://www.maxtblog.com/2016/10/running-powershell-in-windows-10-bash-workaround/

For a quick console test, try using ‘screen‘ but need to run as ‘root’ privilege :

[sourcecode language=”bash”]
$ sudo screen
[/sourcecode]

Here are some remediation to caveats with PowerShell when using ‘screen‘.

Important shortcut keys to remember:

1. Use CTRL-A & ESC – for PGUP/PGDN or ARROW UP/DOWN.
2. Use CRTL-L – to CLEAR the screen.

Also, caution, if you run any Linux command from PowerShell it will not terminate and you’ll need to close your session.

The workaround is the save the results of the Linux command to a PowerShell variable and the display the result.

For example, run in PowerShell prompt:

[sourcecode language=”PowerShell”]
PS c:\Windows\system32> $x = dh -h; $x;
[/sourcecode]

Now, if you run the linux command in PowerShell without saving to a variable, then you’ll get the session hung. To force PowerShell to get out of the hung state use CRTL-A and then CRTL-C. This will return to the Linux prompt(#).

Remember to exit ‘screen‘ by typing ‘exit‘ a few times until you get to the user ($) bash prompt.

So, with these few shortcuts, at least you should be able to work with PowerShell Open Source in Windows Bash console. I know its a little tedious but it will be rewarding at the end.

Just give it a shot, and keep learning PowerShell!

PowerShell Open Source Alpha14 and SQL Server for Linux CTP 1.1 are out!

This is Great! Microsoft keep delivering updates before Christmas just to keep us busy and excited.

To download the latest PowerShell Open Source just go to the link below:

https://github.com/PowerShell/PowerShell

Just remember to remove the previous version, and any existing folders as this will be resolved later.

To download the latest SQL Server vNext just check the following Microsoft blog post as the new CTP 1.1 includes version both Windows and Linux:

SQL Server next version Community Technology Preview 1.1 now available

And, don’t forget the check out the release notes as Microsoft SQL Server team has done an excellent job in providing documentation:

https://docs.microsoft.com/en-us/sql/linux/sql-server-linux-release-notes

For those interested in how SQL Server became to Linux, check the following Microsoft Blog posts:

SQL Server on Linux: How? Introduction

http://arstechnica.com/information-technology/2016/12/how-an-old-drawbridge-helped-microsoft-bring-sql-server-to-linux/

Additional Tools in the Horizon

Just an FYI on some tools that recently caught my attention from Jet Brain:

1. PyCharm Community Edition (free) – Python Develoment IDE Tool available for both Windows and Linux.
2. DataGrip – SQL Developer IDE Tools for working with Databases (including SQL Server). Also available for both Windows and Linux.

And, of course, we can’t forget of Microsoft VS Code. This lightweight coding IDE works great in both Windows and Linux:

https://code.visualstudio.com/

 

PowerShell Open Source – Windows PSRemoting to Linux with OpenSSH

I have to admit that I was waiting for this for some time. Finally, PowerShell 6.0.0-Alpha_13 can do remoting between Windows to Linux. This is Awesome!

microsoft-loves-linux

I’m going to give you the top-to-bottom scope in order to avoid going back-and-forth between link. So, it’s all here and including tips and print-screens.

Instructions for Windows OpenSSH installation

[sourcecode language=”dos”]
cd ‘C:\Program Files\OpenSSH’
[/sourcecode]

  • Install sshd and ssh-agent services by using the following command line:

[sourcecode language=”powershell”]
powershell -executionpolicy bypass -file install-sshd.ps1
[/sourcecode]

  • Setup SSH host keys (this will generate all the ‘host’ keys that sshd expects when its starts).

[sourcecode language=”powershell”]
.\ssh-keygen.exe -A
[/sourcecode]

winlinuxremoting_07_2016-12-02_11-23-28

  • Open Firewall (Windows PowerShell 5.x) Server: (This command works in Windows 10)

[sourcecode language=”powershell”]
New-NetFirewallRule -Protocol TCP -LocalPort 22 -Direction Inbound -Action Allow -DisplayName SSH
[/sourcecode]

winlinuxremoting_08_2016-12-02_11-23-28

  • Or for a Workstation:

[sourcecode language=”powershell”]
netsh advfirewall firewall add rule name=’SSH Port’ dir=in action=allow protocol=TCP localport=22
[/sourcecode]

winlinuxremoting_10_2016-12-02_11-23-28

  • Then, to set sshd in auto-start mode and open up firewall:

[sourcecode language=”powershell”]
Set-Service sshd -StartupType Automatic
Set-Service ssh-agent -StartupType Automatic
[/sourcecode]

winlinuxremoting_06_2016-12-02_11-23-28

  • Edit the sshd_config file at the location where you installed Win32 Open SSH and make sure to enabled the following lines:
    Port 22
    PasswordAuthentication yes
    RSAAuthentication yes
    PubkeyAuthentication yes

winlinuxremoting_02_2016-12-02_11-23-28

Also, add the following line to the Subsystem section:
Subsystem powershell C:/Program Files/PowerShell/6.0.0.13/powershell.exe -sshs -NoLogo -NoProfile

Finally, you must add the following line in the system path: “C:\Program Files\OpenSSH”

winlinuxremoting_09_2016-12-02_11-23-28

Failure to add this path line will cause the following error because it can’t execute sshd.exe:

winlinuxremoting_03_2016-12-02_11-23-28

If there’s a need to restart Windows SSHD services, just use the following PowerShell commands:

[sourcecode language=”powershell”]
Stop-Service sshd
Start-Service sshd
[/sourcecode]

Instructions for Ubuntu SSH installation:

  • Download using Ubuntu apt repository using sudo:

[sourcecode language=”powershell”]
$ sudo apt install openssh-client
$ sudo apt install openssh-server
[/sourcecode]

  • Make changed to Ubuntu sshd_config file to enabled some lines below:

[sourcecode language=”powershell”]
$ sudo gedit /etc/ssh/sshd_config
[/sourcecode]

  • Again, make sure to enabled the following lines:
    PasswordAuthentication yes
    RSAAuthentication yes
    PubkeyAuthentication yes
  • Add the following line to the Subsystem section:
    Subsystem powershell powershell -sshs -NoLogo -NoProfile

When done, in Ubuntu, just restart sshd service :

[sourcecode language=”bash”]
sudo service sshd restart
[/sourcecode]

If you want to verify Ubuntu SSH is working, just type the following command:

[sourcecode language=”bash”]
ps -A | grep sshd
[/sourcecode]

This should display a line showing sshd. If is blank, then is not on.

Connecting Linux to Windows

This is strange but, more likely, you will get an error the first time you try to connect. Just try the second try and it will work.

[sourcecode language=”powershell”]
Enter-PSSession -Hostname earth2 -Username max_t
[/sourcecode]

winlinuxremoting_11_2016-12-02_11-23-28

Connecting Windows to Linux

Again, I’ve seen the same behavior. It will fail the first time and the it will work.

[sourcecode language=”powershell”]
Enter-PSSession -Hostname orion -Username maxt
[/sourcecode]

winlinuxremoting_05_2016-12-02_11-23-28

What’s next?

After all have been tested, you can now connect from Windows to a Linux system. Then, you can execute your Linux commands from the Linux PowerShell session at your Windows Desktop. Is that amazing!!

Keeping things Simple!

Additional tips

This tips may not hurt to do:
1. winrm quickconfig
2. Run the ./InstallPsRemoting.ps1

winlinuxremoting_04_2016-12-02_11-23-28

Also, if you don’t have a domain set up, then set a workgroup environment, and adding the IP-Addresses, computer names for either Windows and Linux host file:
In Windows the *hosts file can be found under “C:\Windows\System32\Drivers\etc“, and in Linux Ubuntu can be found under “/etc/hosts“.

winlinuxremoting_12_2016-12-02_11-23-28

The editors need to be open “Run as Administrator” in order to make changes to this file. In Windows, open Powershell “Run as Administrator” the execute notepad:

[sourcecode language=”powershell”]
notepad C:\Windows\System32\Drivers\etc\hosts
[/sourcecode]

In Linux, use the sudo then the editor of choice;

[sourcecode language=”bash”]
sudo gedit /etc/hosts
[/sourcecode]

I’m hoping this information will get you all going in doing PowerShell Remoting between Windows and Linux.

Additional Resource Information

OpenSSH Instructions at:
https://github.com/PowerShell/Win32-OpenSSH/wiki/Install-Win32-OpenSSH
https://github.com/PowerShell/PowerShell/blob/309bf603f9aff9009dce7e725d42b2d4f99f58c9/demos/SSHRemoting/README.md

For Ubuntu questions:
https://help.ubuntu.com/community