powershell on linux - wmlug on linux - wmlug january 2017.pdfpowershell doesn’t support input...

40
WMLUG January 2017 PowerShell on Linux Patrick TenHoopen

Upload: others

Post on 24-Aug-2020

16 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: PowerShell on Linux - WMLUG on Linux - WMLUG January 2017.pdfPowerShell doesn’t support input redirection. Available Commands Bash has ~150 built-in commands and helper functions

WMLUG January 2017

PowerShell on Linux

Patrick TenHoopen

Page 2: PowerShell on Linux - WMLUG on Linux - WMLUG January 2017.pdfPowerShell doesn’t support input redirection. Available Commands Bash has ~150 built-in commands and helper functions

PowerShell on Linux

On August 18, 2016, Microsoft announced that it would make PowerShell open source and release it for Linux and Mac OS using one code base under the MIT license. It is still in an alpha stage though.

Page 3: PowerShell on Linux - WMLUG on Linux - WMLUG January 2017.pdfPowerShell doesn’t support input redirection. Available Commands Bash has ~150 built-in commands and helper functions

Why Run PowerShell on Linux?

● Language is easily readable and is self-documenting

● Optimized for structured data● Good at "gluing" systems together● Provides an interactive shell as well as being

scriptable●

Page 4: PowerShell on Linux - WMLUG on Linux - WMLUG January 2017.pdfPowerShell doesn’t support input redirection. Available Commands Bash has ~150 built-in commands and helper functions

Comparing PowerShell to Bash

Command Input/Output Piping

Bash uses text while PowerShell uses objects (does require that you know the object methods and properties). PowerShell doesn’t support input redirection.

Available Commands

Bash has ~150 built-in commands and helper functions (less to learn) compared to PowerShell's 300+ (on Windows) methods and cmdlets.

Windows System Integration

PowerShell has many cmdlets written for administering Microsoft products while bash has none.

Integrated Scripting Environment (ISE)

PowerShell on Windows provides an integrated scripting environment.

Page 5: PowerShell on Linux - WMLUG on Linux - WMLUG January 2017.pdfPowerShell doesn’t support input redirection. Available Commands Bash has ~150 built-in commands and helper functions

PowerShell's Use of Objects

The output of a PowerShell command is an object.

The object can be passed to the next command (object pipeline) for input.

Page 6: PowerShell on Linux - WMLUG on Linux - WMLUG January 2017.pdfPowerShell doesn’t support input redirection. Available Commands Bash has ~150 built-in commands and helper functions

Running on Linux

Not all of PowerShell's features work on Linux like in Windows due to bugs and the difference in frameworks (.NET Core on Linux vs full .NET on Windows).

Some examples of issues:● Remote management is not available● Case-sensitivity of file names● Aliases were removed for native commands (ls, cp,

etc.) so the native commands are used but they return text rather than objects

Page 7: PowerShell on Linux - WMLUG on Linux - WMLUG January 2017.pdfPowerShell doesn’t support input redirection. Available Commands Bash has ~150 built-in commands and helper functions

Running on Linux

● Output from native commands is sent in total after command is done rather than as it is generated

● Redirected output contains the UTF-8 byte order mark (BOM) unless ASCII encoding is specified

● Job control doesn’t work● Doesn’t do wildcard expansion (globbing) for the

native commands but does for built-in/internal commands

Page 8: PowerShell on Linux - WMLUG on Linux - WMLUG January 2017.pdfPowerShell doesn’t support input redirection. Available Commands Bash has ~150 built-in commands and helper functions

Installation

Supported distros: Ubuntu 16.04, Ubuntu 14.04, CentOS 7, Red Hat Enterprise Linux 7

https://github.com/PowerShell/PowerShell/releases

Installing on Ubuntu 16.04:sudo apt-get install libunwind8 libicu55

sudo dpkg -i /path/to/powershell_6.0.0-alpha.14-1ubuntu1.16.04.1_amd64.deb

Page 9: PowerShell on Linux - WMLUG on Linux - WMLUG January 2017.pdfPowerShell doesn’t support input redirection. Available Commands Bash has ~150 built-in commands and helper functions

Running PowerShell

Starting the interactive PowerShell shell:

powershell

The PowerShell prompt is PS followed by the current directory:

PS /home/user>

Page 10: PowerShell on Linux - WMLUG on Linux - WMLUG January 2017.pdfPowerShell doesn’t support input redirection. Available Commands Bash has ~150 built-in commands and helper functions

PowerShell Basics

● Commenting Code● Variables● Some String Methods and Properties● Casting● Arrays● Getting Help● Getting Members and Properties● User Input● Screen Output● Functions● Conditional Branching● Loops● XML Parsing● Running Scripts

Page 11: PowerShell on Linux - WMLUG on Linux - WMLUG January 2017.pdfPowerShell doesn’t support input redirection. Available Commands Bash has ~150 built-in commands and helper functions

Commenting Code

Comments use #

# This is a comment

"Hello" # This is an inline comment

<#

This is a

multi-line comment.

#>

Page 12: PowerShell on Linux - WMLUG on Linux - WMLUG January 2017.pdfPowerShell doesn’t support input redirection. Available Commands Bash has ~150 built-in commands and helper functions

Variables

Common Data Types: string, int, long, bool

Page 13: PowerShell on Linux - WMLUG on Linux - WMLUG January 2017.pdfPowerShell doesn’t support input redirection. Available Commands Bash has ~150 built-in commands and helper functions

Variables - Integer

# Integer

$Item1 = 5

$Item2 = 2

$Total = $Item1 + $Item2

$Total

7

Page 14: PowerShell on Linux - WMLUG on Linux - WMLUG January 2017.pdfPowerShell doesn’t support input redirection. Available Commands Bash has ~150 built-in commands and helper functions

Variables - String

# String

$MyString = "Hello"

$MyString

Hello

Page 15: PowerShell on Linux - WMLUG on Linux - WMLUG January 2017.pdfPowerShell doesn’t support input redirection. Available Commands Bash has ~150 built-in commands and helper functions

Variables - Boolean

#Boolean uses $True or $False

$Done = $False

$TrueOrFalse = 1 -gt 2

Page 16: PowerShell on Linux - WMLUG on Linux - WMLUG January 2017.pdfPowerShell doesn’t support input redirection. Available Commands Bash has ~150 built-in commands and helper functions

Variable Scope

Scopes: Global vs Local

Page 17: PowerShell on Linux - WMLUG on Linux - WMLUG January 2017.pdfPowerShell doesn’t support input redirection. Available Commands Bash has ~150 built-in commands and helper functions

Some String Methods and Properties

$Alphabet = "abcdefghijklmnopqrstuvwxyz"

$Alphabet.Length #26

$Alphabet[0] # a

$Alphabet.IndexOf("c") # 2

$Alphabet.Substring(2,3) # cde

$Alphabet.ToUpper() # ABCDEFGHIJKLMNOPQRSTUVWXYZ

Page 18: PowerShell on Linux - WMLUG on Linux - WMLUG January 2017.pdfPowerShell doesn’t support input redirection. Available Commands Bash has ~150 built-in commands and helper functions

Splitting Strings

$phone = "616-867-5309"

$phone.Split("-")

616

867

5309

Page 19: PowerShell on Linux - WMLUG on Linux - WMLUG January 2017.pdfPowerShell doesn’t support input redirection. Available Commands Bash has ~150 built-in commands and helper functions

Casting

# Cast a string as an integer

[Int] $Number = "3"

$Number.GetType().Name

Int32

Page 20: PowerShell on Linux - WMLUG on Linux - WMLUG January 2017.pdfPowerShell doesn’t support input redirection. Available Commands Bash has ~150 built-in commands and helper functions

Arrays

$array1 = @() # Create an empty array

$array1 += 1

$array1 += “two”

# Create an array with 3 elements

$array2 = @("One", "Two", "Three")

Page 21: PowerShell on Linux - WMLUG on Linux - WMLUG January 2017.pdfPowerShell doesn’t support input redirection. Available Commands Bash has ~150 built-in commands and helper functions

Iterating an Array with Foreach

$ColorList = @("Red", "Green", "Blue")

ForEach ($Color in $ColorList) {

Write-Host $Color -ForeGroundColor $Color

}

Page 22: PowerShell on Linux - WMLUG on Linux - WMLUG January 2017.pdfPowerShell doesn’t support input redirection. Available Commands Bash has ~150 built-in commands and helper functions

Getting Help

Get-Help date

Get-Help Get-Date

Get-Help Get-Date -examples

Get-Help Get-Process

Page 23: PowerShell on Linux - WMLUG on Linux - WMLUG January 2017.pdfPowerShell doesn’t support input redirection. Available Commands Bash has ~150 built-in commands and helper functions

Find Object Members and Properties

Get-ChildItem | Get-Member

Get-Process | Get-Member

Get-Date | Get-Member

$TestString = "Test"

$TestString | Get-Member

Page 24: PowerShell on Linux - WMLUG on Linux - WMLUG January 2017.pdfPowerShell doesn’t support input redirection. Available Commands Bash has ~150 built-in commands and helper functions

Acting on Objects with ForEach-Object

Get-ChildItem | ForEach-Object {

Write-Host "Name is $_.Name, size $_.Length"

}

Note: $_ is the current object in the pipeline

Page 25: PowerShell on Linux - WMLUG on Linux - WMLUG January 2017.pdfPowerShell doesn’t support input redirection. Available Commands Bash has ~150 built-in commands and helper functions

Filtering with Where-Object

Get-ChildItem | Where-Object {$_ -Like "*.txt"} | ForEach-Object {

Write-Host "Text file name is $_.”

Write-Host “Last updated on $($_.LastWriteTime)."

}

Page 26: PowerShell on Linux - WMLUG on Linux - WMLUG January 2017.pdfPowerShell doesn’t support input redirection. Available Commands Bash has ~150 built-in commands and helper functions

Listing Processes

Example raw output of Get-ProcessNPM(K) PM(K) WS(K) CPU(s) Id SI ProcessName

------ ----- ----- ------ -- -- -----------

0 0 0 0.000 1326 324 (sd-pam)

0 0 2 1.100 925 925 accounts-daemon

0 0 0 0.000 64 0 acpi_thermal_pm

0 0 0 4.640 942 942 acpid

0 0 0 0.000 1095 095 agetty

0 0 2 0.000 1671 574 at-spi-bus-laun

0 0 1 3.280 1678 574 at-spi2-registr

0 0 0 0.000 37 0 ata_sff

0 0 1 0.830 911 911 avahi-daemon

0 0 0 0.000 950 911 avahi-daemon

0 0 10 32.860 1692 692 bamfdaemon

0 0 1 0.040 4009 009 bash

0 0 0 0.000 35 0 bioset

0 0 0 0.000 70 0 bioset

Page 27: PowerShell on Linux - WMLUG on Linux - WMLUG January 2017.pdfPowerShell doesn’t support input redirection. Available Commands Bash has ~150 built-in commands and helper functions

Filtering Processes with Where-Object

Get-Process | Where-Object {$_.Name -Like “fire*”}

Page 28: PowerShell on Linux - WMLUG on Linux - WMLUG January 2017.pdfPowerShell doesn’t support input redirection. Available Commands Bash has ~150 built-in commands and helper functions

User Input

$Name = Read-Host "What is your name?"

Page 29: PowerShell on Linux - WMLUG on Linux - WMLUG January 2017.pdfPowerShell doesn’t support input redirection. Available Commands Bash has ~150 built-in commands and helper functions

Screen Output

Write-Host "Hello $Name"

Hello Pat

$Greeting = "Hello"

Write-Host "$Greeting my friend, $Name!"

Page 30: PowerShell on Linux - WMLUG on Linux - WMLUG January 2017.pdfPowerShell doesn’t support input redirection. Available Commands Bash has ~150 built-in commands and helper functions

Simple Function

Function SayHello {

Write-Host "Hello"

}

PS> SayHello

Hello

Page 31: PowerShell on Linux - WMLUG on Linux - WMLUG January 2017.pdfPowerShell doesn’t support input redirection. Available Commands Bash has ~150 built-in commands and helper functions

Function with Parameter

Function SayHelloTo ($Name) {

Write-Host "Hello $Name"

}

PS> SayHelloTo "Sheldon"

Hello Sheldon

Page 32: PowerShell on Linux - WMLUG on Linux - WMLUG January 2017.pdfPowerShell doesn’t support input redirection. Available Commands Bash has ~150 built-in commands and helper functions

Comparison Operators

-lt -- Less than

-le -- Less than or equal to

-gt -- Greater than

-ge -- Greater than or equal to

-eq -- Equal to

-ne -- Not equal to

-like - Like; uses wildcards for pattern matching

Page 33: PowerShell on Linux - WMLUG on Linux - WMLUG January 2017.pdfPowerShell doesn’t support input redirection. Available Commands Bash has ~150 built-in commands and helper functions

Logical operators

-and -- Logical And

-or -- Logical Or

-not -- Logical Not

! -- Logical Not

Page 34: PowerShell on Linux - WMLUG on Linux - WMLUG January 2017.pdfPowerShell doesn’t support input redirection. Available Commands Bash has ~150 built-in commands and helper functions

Conditional Branching with If

$Dollars = 5

$TacoPrice = 3

If ($Dollars -ge $TacoPrice) { Write-Host "Tacos for dinner!" }

$Day = (Get-Date).DayOfWeek

If ($Day -eq "Friday") {

Write-Host "TGIF!"

}

Else {

Write-Host "Sorry, it's not Friday yet :("

}

Page 35: PowerShell on Linux - WMLUG on Linux - WMLUG January 2017.pdfPowerShell doesn’t support input redirection. Available Commands Bash has ~150 built-in commands and helper functions

While Loop

$Done = $False

$Count = 3

While (!$Done) {

Write-Host $Count

$Count--

If ($Count -eq 0) {

$Done = $True

Write-Host "Done!"

}

}

Page 36: PowerShell on Linux - WMLUG on Linux - WMLUG January 2017.pdfPowerShell doesn’t support input redirection. Available Commands Bash has ~150 built-in commands and helper functions

For Loop

For ($Count = 3; $Count -ne 0; $Count--) {

Write-Host $Count

}

Write-Host "Done!"

Page 37: PowerShell on Linux - WMLUG on Linux - WMLUG January 2017.pdfPowerShell doesn’t support input redirection. Available Commands Bash has ~150 built-in commands and helper functions

XML Parsing

[xml] $MovieList = Get-Content sample.xml

$MovieList.movies.movie

$MovieList.movies.movie.title

Page 38: PowerShell on Linux - WMLUG on Linux - WMLUG January 2017.pdfPowerShell doesn’t support input redirection. Available Commands Bash has ~150 built-in commands and helper functions

CSV Output

$MovieList.movies.movie | Export-CSV sample.csv

Page 39: PowerShell on Linux - WMLUG on Linux - WMLUG January 2017.pdfPowerShell doesn’t support input redirection. Available Commands Bash has ~150 built-in commands and helper functions

Running Scripts

If PowerShell is not running:

powershell -file /path/to/ScriptName.ps1

If PowerShell is running:

/path/to/ScriptName.ps1

Page 40: PowerShell on Linux - WMLUG on Linux - WMLUG January 2017.pdfPowerShell doesn’t support input redirection. Available Commands Bash has ~150 built-in commands and helper functions

Additional Resources

● Daily PowerShell tips– http://community.idera.com/powershell/powertips/

● Windows PowerShell Survival Guide– https://social.technet.microsoft.com/wiki/contents/a

rticles/183.windows-powershell-survival-guide.aspx● Scripting with Windows PowerShell

– https://technet.microsoft.com/en-us/library/bb978526.aspx