Wednesday, 8 February 2017

Powershell Doesn't Run Scripts "Out of the Box"

Most people think that Powershell is a "scripting language" but when you install the current version the first thing you notice is that you can't run scripts.

In fact you are more likely to see errors like this.

"Install.ps1 cannot be loaded because the execution of scripts is disabled on this system."

The first reaction to this could be something less polite than "Hey I thought this thing did scripts". However scripting has a history in Microsoft that makes this completely normal.

In the beginning Microsoft was a languages company. It wrote computer programming languages for operating systems. It got pushed into operating systems with the launch of the IBM PC and DOS (Disk Operating System). With this the first 'batch language' came into play. You could put a few commands into a file with the extension 'bat' and it would run. The 'autoexec.bat' ran automatically if it was present when a PC booted. The command processor 'command.com' loaded and ran the batch file.

Your 'hello world' announcement in batch would look something like this.

echo off
cls
echo "Hello World"

This was scripting 1980s style. You can still use batch today. Even Windows 10 will run a batch file.

Third parties wrote enhancements to this. One of the most well known in the 1980s was 4DOS from JP Software. You can still get a freeware copy here. I know a little about JP Software because I worked for a firm that sold their products in the UK.

Microsoft introduced two major enhancements to scripting. The first was the'cmd.exe'' command processor introduced with Windows NT. The second was VB Script, a variation on their Basic language product.

Both of these enhancements were created in a world of standalone PCs rarely connected to the outside world. Both assumed the person running the script was the PC's owner, primary user, and knew what they were doing. So they just ran. Anything with the file name ending in .bat, .cmd or .vbs would just run. These scripts ran commands that immediately made changes and, in the case of vbs, quickly were used in Microsoft Office products like Excel, Word, Powerpoint and Outlook.

Outlook was the most dangerous. You could receive an email with a vbs attached and just by clicking on it could run a massively distructive script. Microsoft added approved file extensions into Outlook so criminals just embedded their scripts in Word or Excel documents. The war was on.

On 15th January 2002 Bill Gates sent his "Trustworthy Computing" memo. Microsoft was under massive pressure from it's customers in the new connected world of the Internet that Windows was not sufficiently secure. This was true. Unlike Unix based operating systems that were built to be connected to the Internet the Microsoft world had been a world of standalone unconnected devices. Once these were attached to networks then fundamental design issues could not be dealt with by patches. Gates announced that from 2002 Microsoft's priorities would be; Security, Privacy, Reliability, and Business Integrity,

After the memo the world changed for Microsoft. Every product now had to be secure by default. Windows XP got service pack 2 and Windows Server began to be delivered with services switched off by default and ports blocked and then administrators had to switch on features.

In 2003 project monad was first revealled to developers. This project eventually became Powershell As a product devised in the new "switched off by default world" scripts dont run by default. 

To run a script you need to devise an "execution policy" to make the script secure by default.  A comandlet called Set-ExecutionPolicy is used to decide whether a script should run or not. This does not effect the command line just scripts. 

Microsoft recommend you dont set the policy to "unrestricted" but use signed scripts to protect your system. 

This is why Powershell doesn't run scripts "out of the box".