PowerShell Core
Powershell Core is an open-source command-line interface (Scripting language ) developed on .NET framework and it can be installed on Windows, Linux, and Mac OS. It helps system administrators to automate day to day tasks.
PowerShell installation on Ubuntu 18.04
To install PowerShell on Ubuntu execute the below commands.
Download the Microsoft repository GPG keys and then register it.
wget -q https://packages.microsoft.com/config/ubuntu/18.04/packages-microsoft-prod.deb
sudo dpkg -i packages-microsoft-prod.deb
Update all packages and add “universe” repositories.
sudo apt-get update
techies@techiescorner:~$ sudo add-apt-repository universe 'universe' distribution component is already enabled for all sources.
Next, install PowerShell
sudo apt-get install -y powershell
To get installed version and the PowerShell terminal execute “pwsh” command.
techies@techiescorner:~$ pwsh PowerShell 6.2.4 Copyright (c) Microsoft Corporation. All rights reserved. https://aka.ms/pscore6-docs Type 'help' to get help. PS /home/techies>
PowerShell Installation on MAC OS 10.14.6
Powershell can be easily installed on MAC OS with a “brew” package installer.
brew cask install powershell
Once installed execute the “pwsh” command to get the version and shell terminal.
MacBook-Pro:~ $ pwsh PowerShell 6.2.3 Copyright (c) Microsoft Corporation. All rights reserved. https://aka.ms/pscore6-docs Type 'help' to get help. PS /Users/techies>
There is another method to access PowerShell which is from the Azure portal, log in to your account and on the top right corner, you can see a button named cloud shell. Click on this button to get a shell.
Powershell basic commands
Here we will discuss some basic PowerShell commands. Login to the Ubuntu machine and run “pwsh” to start.
To list files and folders present in the directory enter Get-ChildItem command.
Please note if you enter tab after command it will show suggestions as same as in Linux.
PS /home/techies> Get-C Get-ChildItem Get-ComplexResourceQualifier Get-Credential Get-Command Get-ConfigurationErrorCount Get-Culture Get-CompatibleVersionAddtionaPropertiesStr Get-Content
PS /home/techies> Get-ChildItem Directory: /home/techies Mode LastWriteTime Length Name ---- ------------- ------ ---- d----- 1/24/20 6:54 PM azcopy_linux_amd64_10.3.4 ------ 1/24/20 7:20 PM 8754718 azcopy.tar.gz ------ 1/2/19 11:49 PM 3132 packages-microsoft-prod.db
If we give -Path argument it shows all the files and folder present in the given path. Here I will list all content from the /tmp folder.
PS /home/techies> Get-ChildItem -Path /tmp Directory: /tmp Mode LastWriteTime Length Name ---- ------------- ------ ---- d-r--- 12/5/08 3:39 PM kSar-5.0.6 ------ 2/19/20 12:13 PM 6039985 kSar-5.0.6.zip
To know the usage of PowerShell command execute Get-Help command as follows.
Get-Help Get-Alias
From the output of the above command, we can understand how to use the command. It is similar to a man page in Linux.
The Get-Alias command is to list all Aliases set in the system. A few examples are given below.
PS /home/techies> Get-Alias CommandType Name Version Source ----------- ---- ------ ----- Alias ? -> Where-Object Alias % -> ForEach-Object Alias cd -> Set-Location Alias chdir -> Set-Location Alias pwd -> Get-Location
Pipe command usage
We can pipeline the output of one command with another, an example is given below.
Get-Process | Export-CSV process-lis.csv
Here, the Get-Process command list all running process in the system, the output of this command can be exported to csv file using another command “Export-CSV”
Get-ChildItem | Where-Object Length -gt 10kb
The above “Get-ChildItem” command list all files in the current directory and this output is passed to the next command, there we were given a condition which is the file size is greater than 10kb will be listed. The final output passed into a txt file as follows.
Get-ChildItem | Where-Object Length -gt 10kb | Out-File ./items.txt
To list the content of the “items.txt file use below command.
Get-Content ./items.txt
Powershell remote login
With the help of the “Enter-PSSession” command, we can log in to a remote machine PowerShell terminal.
Enter-PSSession -HostName 192.168.0.15 -UserName techies
Here, we are login to an Ubuntu machine from the MAC PowerShell terminal. The above command asks for the password to login to the machine. The complete output is below.
PS /Users/admin> Enter-PSSession -HostName 192.168.0.15 -UserName techies techies@192.168.0.15's password: [techies@192.168.0.15]: PS /home/techies> [techies@192.168.0.15]: PS /home/techies> hostname techiescorner
It is now connected to the remote terminal. To connect to the Ubuntu machine, we have to add the following line at the sshd_config file of the Ubuntu machine and then restart the sshd service.
Subsystem powershell /usr/bin/pwsh -sshs -NoLogo -NoProfile
AZ module with PowerShell
Azure CLI is a command-line tool (az tool) to manage Azure cloud resources. To know more about Azure az I recommend you to read the below article.
To install the AZ module for Powershell execute below command.
Install-Module -Name Az -AllowClobber -Scope CurrentUser
The AllowClobber flag helps to override the similar installed module. The CurrentUser flag will install it only for the current logged in user. To check the installed module execute below command
PS /home/techies> Get-InstalledModule -Name Az -AllVersions Version Name Repository Description ------- ---- ---------- ----------- 3.5.0 Az PSGallery Microsoft Azure PowerShell - Cmdlets to manage resources in Azure. This module is com…
Powershell core authentication methods
In this section, we will understand various methods to login to the Azure cloud from the PowerShell command line.
- Connect-AzAccount : This is the easy method to connect to the Azure cloud. This command gives a token to authenticate with the cloud. Execute the command in the PowerShell terminal and copy the URL and token mentioned in the output. Once you paste the token in the mentioned URL it prompts to choose an Azure account. Select your account and then come back to the terminal and you can see it listed the Resource group.
2. Service Principal: The second method to authenticate with Azure cloud is Service Principle. For this, we need to create a Service Principal from the Azure portal.
To know how to create a service principal and authenticate it with cloud, refer below URL.
How to create an Azure Service Principal
Provisioning a VM from the Powershell.
We have learned basic command of Powershell, next we will a provision a ubuntu virtual machine using Powershell codes.
Login to the PowerShell and execute the below command to connect to the cloud.
PS /home/techies> Connect-AzAccount WARNING: To sign in, use a web browser to open the page https://microsoft.com/devicelogin and enter the code GH5BQ6BZA to authenticate.
Once you add the token, you can see all the subscriptions under the account.
To create a Virtual machine execute below command.
PS /home/techies> New-AzVM ` >> -Name pshvm ` >> -ResourceGroupName Techies_devops ` >> -Location eastus ` >> -ImageName UbuntuLTS cmdlet New-AzVM at command pipeline position 1 Supply values for the following parameters: Credential User: techies Password for user techies: *************
Use backquote to write code on a separate line. Also, use the tab for command completion.
The above command creates a VM with default values. Once the job is completed it will display information about the VM in the terminal.
To get complete details about the Virtual machine, log in to the Azure portal and select the resource group.
We have completed the provisioning of a virtual machine from the PowerShell, on the next page we will learn how to deploy a resource from the PowerShell using Azure Resource Manager (ARM) template.