Powershell 6 -Getting Started with Automating System Administration



Windows PowerShell is a task-based command-line shell and scripting language designed specifically for system administration.

Built on the .NET Framework, Windows PowerShell helps IT professionals and power users control and automate the administration of the Windows operating system and applications that run on Windows.

This post is to help you get up and running with PowerShell, taking you from the basics of installation to writing scripts and web server automation.

This will act as an introduction to the central topics of PowerShell, from finding and understanding PowerShell commands and packaging code for reusability right through to a practical example of automating IIS.

You will explore the PowerShell environment and discover how to use cmdlets, functions, and scripts to automate Windows systems.






Accessing the PowerShell Console and PowerShell ISE

We will see how to access the PowerShell console and PowerShell ISE Console and work with PowerShell 6 module.

We will cover how to :

  1. The PowerShell Console and x86 event
  2. The elevated Console
  3. The PowerShell ISE
  4. How to get help with get-help cmdlets
  5. Write a small script in PowerShell 





The standard PowerShell console helps you to install the module and run PowerShell cmdlets. 


While we can use the standard console to perform most of the task but we have an ISE environment to write scripts, modules and test those.


The below fig shows the standard console.





The fig also gives an idea of how to execute the cmdlets get-date which helps you see the current date or the system date.


Working with elevated (means run as administrator) will be needed to perform a certain task which needs admin task.


Consider the below example where we are trying to install a 
msolonline module. This module needs elevated access to install. To perform this task we need to use run as admin of  Standard PowerShell Console.


PS C:\Users\venka> install-module msolonline
install-module : Administrator rights are required to install modules in 'C:\Program Files\WindowsPowerShell\Modules'. Log on to the computer with an account that has Administrator rights, and then try again, or install 'C:\Users\venka\OneDrive\Documents\WindowsPowerShell\Modules' by adding "-Scope CurrentUser" to your command. You can also try running the Windows PowerShell session with elevated rights (Run as Administrator). At line:1 char:1 + install-module msolonline + ~~~~~~~~~~~~~~~~~~~~~~~~~     + CategoryInfo          : InvalidArgument: (:) [Install-Module], ArgumentException     + FullyQualifiedErrorId : InstallModuleNeedsCurrentUserScopeParameterForNonAdminUser,Install-Module 




Accessing the help system and updating it

Let us get started to get hands dirty to perform some basic commands to help us learn PowerShell.


The first things to learn how to get help whenever you need the help of any cmdlets. 

First, we need to update the Help documentation.


PS C:\Users\venka> update-help


To get help with any command first type this below command


PS C:\Users\venka> get-help   


Running the cmdlets and Finding it!

The functions in PowerShell are called cmdlets that help to perform various task.

The key feature of the cmdlets are:

The name pattern of PowerShell is the verb-noun type. For example, consider get-date is of the form of the verb (get) and noun (date)

The shortcut to complete any cmdlets by just pressing the TAB BUTTON

If you need any help with knowing more about any cmdlet you can use the get-help cmdlets (tip!).


But if you want to help with the list of command that already present or installed can be performed by using the get-command cmdlet


PS C:\Users\venka> get-Command get-date

But if you want to work with GUI version of get-command. I would suggest you to work with show-command  cmdlets.


PS C:\Users\venka> show-Command get-date

This works ! and shows a popup of that command. If you are a beginner with cmdlets I would suggest you go through these wonderful features.

Measuring the performance of cmdlets


PS C:\Users\venka> Measure-Command {get-date}

Introducing Scripts in PowerShell

Scripts in PowerShell are basically just text files with a special filename extension, ps1. 

To create a script, you would enter a bunch of PowerShell commands in a sequence in a new Notepad file (or you could use any text editor you like), and then save that file as NAME.ps1, where NAME is a friendly description of your script—with no spaces, of course.


To run a PowerShell script you already have, you either enter at a PowerShell window:
–the full path (folder and filename) of the script, like c:\powershell\myscripthere.ps1
or
–if your script is in the current directory the console is looking at, a period and then a backslash, like .\myscripthere.ps1

Other than that, there is nothing special to create a script in PowerShell. You simply add the commands you like.


Example of a sample PowerShell script.

$date = (Get-Date -format "yyyyMMddmmss")
$compname = $env:COMPUTERNAME
$logname = $compname + "_" + $date + "_ServerScanScript.log"
$scanlog = "c:\temp\logs\" + $logname 
new-item -path $scanlog -ItemType File -Force 

The Last Word
As I mentioned, my goal for this post, was to show you how to get started using PowerShell and put together some simple scripts. We’ve covered a lot in this post: setting up PowerShell environment, how to make scripts, and how to add some logic to your scripting. Then I took a very real issue on how to get help with documentation whenever you are trying with new cmdlets. Congratulations! Onward!


In the next part of this post, we will cover depth knowledge and explanation of understanding the above example and steps to write your own scripts.


Alll the world is a laboratory to the inquiring mind! Join Us

Comments

Popular posts from this blog

How to work with Variables, arrays, hash tables, and script blocks in Powershell

How to write basic scripts and functions in powershell 6

How to work with Credentials Parameter in powershell 6