Automating tasks in SDL Tridion Docs using PowerShell - Getting started with Microsoft PowerShell

In this first follow-up blog post of the Tridion Docs webinar 'Automating tasks in SDL Tridion Docs using PowerShell', I will share a short summary of the 'How to get started with Microsoft PowerShell' part of the webinar, the resources and the script used during this webinar.

https://youtu.be/3ItM2LDwh20

Summary

What is PowerShell

  • A task-based command-line shell and scripting language from Microsoft
  • Harnesses the power of the .NET Framework, providing rich objects and a massive set of built-in functionality for taking control of your environments and applications

Cmdlets

A cmdlet (pronounced "command-let"):

  • is a lightweight PowerShell script that performs a single function,
  • is expressed as a Verb-(Prefix)Noun pair (e.g. Get-Help, Get-AdUser),
  • has a .ps1 extension.   

PowerShell modules and packages

  • A collection of cmdlets can be grouped and made available over a versioned  module/package.
  • These modules can be managed as packages using e.g. PowerShellGet (a package manager for Windows PowerShell)
  • The packages can be made available on a public repository (e.g. https://powershellgallery.com) or a private repository (e.g. Nexus).

Installing PowerShell

Using PowerShell

  • Most of the time needs to be run as Administrator
  • PowerShell ISE (Integrated Scripting Environment)
    Run commands and write, test, and debug scripts in a single Windows-based graphic user interface with multi-line editing, tab completion, syntax coloring, selective execution, context-sensitive help, ...
  • Execution (Security) Policy
    The execution policies let you determine the conditions under which PowerShell scripts are run (and configuration files are loaded)

Online resources

Webinar script

Inside the script, you will also find some comments and additional links to specific online resources.

#
# 0. PowerShell?
#
#    (*) https://docs.microsoft.com/en-us/powershell/scripting/powershell-scripting
 
#
# 1. Installing PowerShell (version 5.1)
#    PowerShell 5.1 comes with Windows 10/Windows Server 2016 out-of-the-box.
#    Needs to be installed/upgraded on other Operating
#        (*) https://docs.microsoft.com/en-us/powershell/scripting/setup/installing-windows-powershell?view=powershell-6#upgrading-existing-windows-powershell
 
$PSVersionTable # $ -> variable
 

#
# 2. PowerShell/PowerShell ISE (Integrated Scripting Environment)
#    .ps1
#    Intellisense/tab-completion/...
#    Command Add-on
#    Breakpoints

Write-
 

#
# 3. Execution (Security) Policy
#    (*) http://powershelltutorial.net/V3/Powershell-Security-Policy
#    (*) https://docs.microsoft.com/nl-be/powershell/module/microsoft.powershell.core/about/about_execution_policies?view=powershell-6
 
Get-ExecutionPolicy -List
Get-ExecutionPolicy
 
Set-ExecutionPolicy -ExecutionPolicy Unrestricted
Set-Executionpolicy -Scope CurrentUser -ExecutionPolicy Unrestricted​

#
# 4. Verb-Noun Principle
#    The Philosophy Verb-Noun - (*) https://sid-500.com/2017/12/28/powershell-for-beginners-part-2-the-philosophy-verb-noun/
#    Approved Verbs for Windows PowerShell Commands - (*)https://msdn.microsoft.com/en-us/library/ms714428(v=vs.85).aspx
#    Cmdlets Use Verb-Noun Names to Reduce Command Memorization
 
Start-Process # Start-Process notepad.exe
 
Get-Process   # Get-Process -Name notepad
 
Stop-Process  # Stop-Process -Name notepad
 
#    You can list all commands that include a particular noun/verb with the -Noun/-Verb parameter for Get-Command
Get-Command -Noun Process
Get-Command -Verb Get
 
#    Simular for e.g. services...
Get-Command -Noun Service
Start-Service
Get-Service
Stop-Service
#...

#    Get-Help/-? command #Message to download/update help
#    Update-Help
Get-Help Write-Host # Write-Host -?
Get-Help Write-Host -Detailed
Get-Help Write-Host -ShowWindow
 

#
# 5. Parameters
#    Cmdlets Use Standard Parameters
#    It is a developer guidance/encouragement to standardize parameter names (not guaranteed).
#    Intellisense/tab-completion (parameters names and values)
#
Get-Process -Name explorer
Get-Service -Name dhcp
 
 
#
# 6. Piping
#     Combining commands into pipelines in the PowerShell
#     (*) https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_pipelines?view=powershell-6
Start-Process notepad.exe
Get-Process -Name notepad | Stop-Process
 
 
#
# 7. Package Management/Modules
#    Packages are served up by package providers, which are created from various sources.
#    By default, PowerShell comes with two package sources: nuget.org and PSGallery.
#    Public: E.g. PowerShell Gallery vs Private repositories
Get-PackageProvider -? #PowerShellGet # Get-Help Get-PackageProvider -ShowWindow
Get-PackageSource -?                  # Get-Help Get-PackageSource -ShowWindow

Get-PSRepository -?                   # Get-Help Get-PSRepository -ShowWindow
 
#    Installed modules
Get-Module -ListAvailable
Get-Module -ListAvailable -Name WindowsUpdate
Get-Module -ListAvailable -Name ISHRemote

# Available modules (from defined PSRepositories) -> Nuget provider !
Find-Module -Name "ISH*" #Find-Module -Name "ISHRemote" -Repository  PSGallery

Install-Module -Name ISHRemote -Repository PSGallery -Scope CurrentUser
# NuGet provider is required to continue
# PowerShellGet requires NuGet provider version '2.8.5.201' or newer to interact with NuGet-based repositories.