Writing my first powershell script - running Powershell on a linux machine

Hi,

I am a complete PS novice! The task to be coded/automated using powershell is: install a package on a linux VM remotely from a Windows VM. This is the first in many steps that will follow and will be automated using powershell.

The flow is something like this:

Input params for the script (either on command line or via an input file) will be:

  • Hostname of target server
  • IP of target server
  • Login credentials
Flow:
  • Find out the OS of the machine. If Windows, report that it is Windows and exit.
  • If it is Linux, find out the flavour - RedHat, SUSE, Debian, etc
  • Check for DNS forward and reverse resolution. Report error if it is not working
  • Based on flavour, download the appropriate package from a https link
  • Install the package using yum, zypper, etc as based on linux flavour.
There should be error handling/reporting throughtout.

Considering we are talking of running the script from a Windows on Linux machines, I had the following doubts:

  1. Can we directly login to a remote Linux machine using ssh from PS (since Linux will anyway have OpenSSH by default) and run commands? Or will it need Powershell Core to be installed on both the source Windows machine and destination Linux machine?
  2. Is there a simpler way to figure out the OS of the remote machine prior to logging in?
And of course, I actually need help with the whole code? Would that be asking too much?!

 

Thanks/PV

Windows has a built-in SSH client that you can use from PowerShell. Typically, Linux installs have an SSH server pre-configured. You can directly log in to a Linux system with a running SSH server and an open port (any system really, not just Linux). When you log in, what you will get is a prompt for whichever shell is configured as the default for the user that you logged in as on the remote system (probably Bash). At this point, if PowerShell is installed on the remote system then you could launch it from the Bash session and use it that way. Or, you could simply use the native shell. Basically, the SSH session is shell-agnostic; as long as there is something configured to handle the SSH session the system will use that.

This article has a good walkthrough for setting up cross-platform SSH remoting with PowerShell: Enable PowerShell SSH Remoting in PowerShell 7

You should be aware of the differences between Windows PowerShell and PowerShell Core. Windows PowerShell (up to version 5.1) is based on the .NET Framework and is not cross-platform. Really, PowerShell cmdlets are more user-friendly abstractions of .NET Framework classes - thus “cmdlet” rather than “command” because they send instructions that are interpreted by the PowerShell runtime and by .NET, rather than calling executables that address the operating system directly (like the GNU coreutils). PowerShell Core (version 5.1+) is built on .NET Core, which is an open source, cross-platform implementation of .NET. There are differences between .NET Core and .NET Framework.

If you are working on a Windows 10 system, the default shell is Windows PowerShell 5.1 (not PowerShell Core). PowerShell (Core) 7 is intended to reproduce the functionality of Windows PowerShell 5.1, but the reality is that it’s not complete yet and so Windows PowerShell 5.1 is still the default for Windows installations.

The upshot of all this is that you should choose an environment to work in and configure it properly before you start trying to write scripts. Probably, you’ll want to use PowerShell 7 on both Windows and Linux so that it’s consistent. You could work from the default Windows PowerShell 5.1 on Windows and send cmdlets to PowerShell 7 on Linux, but there will be differences in which cmdlets are available and how they function.
Alternatively, you could write a PowerShell script on Windows that delivers Bash script to Linux - you don’t necessarily have to have PowerShell on the Linux system in order to work with it or automate it remotely.

Generally speaking, this is a troubleshooting and teaching forum. Most will be happy to help fix broken code, answer questions or point you to useful resources - but not do your work for you.

Hi @Grokkit, thanks for the detailed reply. Incidentally I had read through most of what you have mentioned in your reply. I needed a starting point or an outline in terms of the code since it is literally my first big powershell script and prior to this I have just used a few PS commands in scripts. Hence the last question. But I understand that it is not the objective of the forum to write a script for others!

Hmmm, there isn’t really a template for PowerShell scripts because there aren’t really specific requirements for the structure of a script or for specific things that need to be included in it. In general, variables and functions need to be defined before they can be used (it’s a script - it’s more or less linear).

If you want examples of working scripts to use for guidance you can pretty much download anything from the TechNet gallery and look at how it works.

If you’re building a more complex script, and especially if you’re building something that other people might use, I recommend putting comment-based help information at the beginning of your script. This will allow the use of Get-Help with your script - basically, you can embed documentation in the script itself. It should look something like this:

<#
.Synopsis
Short description of your script

.DESCRIPTION
Long description of your script

.NOTES   
script name/author/version/date/etc

.LINK
more information/download source

.PARAMETER
Description of parameter

.EXAMPLE
Example syntax for using your script
#>

And of course, you should practice good commenting of your code. Document document document, because when you come back to it six months later you won’t remember what you did or why.

You’ll probably want to implement try-catch-finally for error handling, and I like to use Set-PSDebug for troubleshooting. The -Trace parameter gives similar functionality to set -x in Bash.