powershell - labman2018.umd.edu · powershell is a natural replacement for your batch install...

40
Powershell: Introduction and Practical Uses Presentation URL: http://bit.ly/2ICK4Pt

Upload: others

Post on 06-Jan-2020

7 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Powershell - labman2018.umd.edu · Powershell is a natural replacement for your batch install scripts Benefits: Full control over install window Easy to check for existing software

Powershell:Introduction and Practical Uses

Presentation URL: http://bit.ly/2ICK4Pt

Page 2: Powershell - labman2018.umd.edu · Powershell is a natural replacement for your batch install scripts Benefits: Full control over install window Easy to check for existing software

HELLO!I am Chris WieringaCS Lab Manager for Calvin College

[email protected]

2

Page 3: Powershell - labman2018.umd.edu · Powershell is a natural replacement for your batch install scripts Benefits: Full control over install window Easy to check for existing software

1.Goals

What we will cover today...

Page 4: Powershell - labman2018.umd.edu · Powershell is a natural replacement for your batch install scripts Benefits: Full control over install window Easy to check for existing software

Goals

▪ Define and startup PowerShell (PS)▪ Basics of PS▪ Local system management▪ Remote Powershell▪ Software installations▪ Windows UpdateThe realgoal is to learn something together today.

4

Page 5: Powershell - labman2018.umd.edu · Powershell is a natural replacement for your batch install scripts Benefits: Full control over install window Easy to check for existing software

2.Define and Startup

Powershell

What is Powershell, and how to run it...

Page 6: Powershell - labman2018.umd.edu · Powershell is a natural replacement for your batch install scripts Benefits: Full control over install window Easy to check for existing software

“Windows PowerShell is an interactive object-oriented

command environment with scripting language features that utilizes small programs called cmdlets to simplify configuration,

administration, and management of heterogeneous environments in both standalone and networked typologies by

utilizing standards-based remoting protocols.- Ed Wilson, “The Scripting Guy”

6

Page 7: Powershell - labman2018.umd.edu · Powershell is a natural replacement for your batch install scripts Benefits: Full control over install window Easy to check for existing software

Define Powershell

▪ Interactive

7

Page 8: Powershell - labman2018.umd.edu · Powershell is a natural replacement for your batch install scripts Benefits: Full control over install window Easy to check for existing software

Define Powershell

▪ Scripts

8

Page 9: Powershell - labman2018.umd.edu · Powershell is a natural replacement for your batch install scripts Benefits: Full control over install window Easy to check for existing software

Startup

Today we’ll be working with the Powershell ISE :

Start -> Windows Powershell ISE -> Right-click -> Run as Administrator

9

Page 10: Powershell - labman2018.umd.edu · Powershell is a natural replacement for your batch install scripts Benefits: Full control over install window Easy to check for existing software

Security through Execution Policy

▪ Execution Policy▫ Default security settings to not run scripts.▫ http://bit.ly/2JRSYJN▫ Get-ExecutionPolicy ← if restricted, run:

▫ Set-ExecutionPolicy unrestricted▫ (if non-admin, Set-ExecutionPolicy -Scope

CurrentUser unrestricted)

10

Page 11: Powershell - labman2018.umd.edu · Powershell is a natural replacement for your batch install scripts Benefits: Full control over install window Easy to check for existing software

3.Basics

Start with the basics...

Page 12: Powershell - labman2018.umd.edu · Powershell is a natural replacement for your batch install scripts Benefits: Full control over install window Easy to check for existing software

Basic Cmdlets

▪ View processes:▫ Get-Process

▪ Directory listing:▫ Get-ChildItem -Path C:\▫ Aliased to ‘ls’

▪ Services list:▫ Get-Service

12

Page 13: Powershell - labman2018.umd.edu · Powershell is a natural replacement for your batch install scripts Benefits: Full control over install window Easy to check for existing software

Basic Cmdlets - More Information

▪ Aliases of popular cmdlets are available:▫ http://bit.ly/2H7iBV5

▪ Most cmdlets have arguments you can pass to them▫ -ArgumentNamevalue

▪ Get-Help cmdlet▫ Get-Help cmdlet -examples

▪ Everything is treated as an objects, which allows objects to be passed to each other through...

13

Page 14: Powershell - labman2018.umd.edu · Powershell is a natural replacement for your batch install scripts Benefits: Full control over install window Easy to check for existing software

PipelinePiping in Powershell is significant. PS will pass objects between commands virtually everywhere. http://bit.ly/2vjH5Jx

14

|

Page 15: Powershell - labman2018.umd.edu · Powershell is a natural replacement for your batch install scripts Benefits: Full control over install window Easy to check for existing software

Basic Cmdlets - Pipeline

▪ Services list - Pipelining!▫ Get-Service | Format-List (or Get-Service | FL)

▫ Get-Service | Select-Object Name,DisplayName▫ Get-Service | Select-Object Name,DisplayName |

Export-CSV -Path “$env:temp\servicelist.csv”▪ In File Explorer, check your %TEMP% directory for a

“servicelist.csv” file.

15

Page 16: Powershell - labman2018.umd.edu · Powershell is a natural replacement for your batch install scripts Benefits: Full control over install window Easy to check for existing software

Basic Cmdlets - Pipeline example continued...

▪ Import-CSV -Path “$env:temp\servicelist.csv” | Foreach-Object {Get-Service -Name $_.Name |

Where-Object { $_.Status -eq “Running” } |Select-Object Name,Status

}▪ $_ is a special variable, representing the current

object in the pipeline. “Fill in the blank” variable.http://bit.ly/2kfuIWu 16

Page 17: Powershell - labman2018.umd.edu · Powershell is a natural replacement for your batch install scripts Benefits: Full control over install window Easy to check for existing software

4.System Management and Administration

How to do something useful on your system...

Page 18: Powershell - labman2018.umd.edu · Powershell is a natural replacement for your batch install scripts Benefits: Full control over install window Easy to check for existing software

System Management and Administration - Event Log

▪ Event Viewer / Log▫ Get-EventLog -LogName System-Source “*GroupPolicy*”▫ Useful filters:

▫ -EntryType { Error | Information | FailureAudit | SuccessAudit | Warning }▫ -LogName: { Application, Security, Setup, System, … }▫ -After or -Before

▫ Get-Help Get-Event -examples

18

Page 19: Powershell - labman2018.umd.edu · Powershell is a natural replacement for your batch install scripts Benefits: Full control over install window Easy to check for existing software

System Management and Administration - Start / Stop Processes

▪ Get-Process - shows current processes▪ Start-Process - starts a new process

▫ Start-Process -FilePath “notepad” -WindowStyle Maximized▫ Add “-Wait” to wait until the process ends.

▪ Stop-Process - stops/ends a process▫ Stop-Process -Name notepad▫ Add “-Force” to force-kill it

19

Page 20: Powershell - labman2018.umd.edu · Powershell is a natural replacement for your batch install scripts Benefits: Full control over install window Easy to check for existing software

System Management and Administration - ACLs

▪ Get-Acl -Path “C:\Windows” | FL▫ $myGoodAcl = Get-Acl -Path “C:\Fish.txt”

▪ Set-Acl -Path “C:\Bird.txt” -AclObject $myGoodAcl

▪ Get-Acl -Path “C:\Fish.txt” | Set-Acl -Path “C:\Bird.txt”

20

Page 21: Powershell - labman2018.umd.edu · Powershell is a natural replacement for your batch install scripts Benefits: Full control over install window Easy to check for existing software

System Management and Administration - Registry

Registry modification potential - Danger Will Robinson!▪ Get-ItemProperty -Path “HKLM:\Software\Microsoft\Windows\CurrentVersion”▪ Get-ItemProperty -Path “HKLM:\Software\Microsoft\Windows\CurrentVersion” -

Name “ProgramFilesDir”

▪ New-Item -Path “HKCU:\Software\Labman”▪ New-ItemProperty -Path “HKCU:\Software\Labman” -Name “NoAttendees” -Value 0▪ Set-ItemProperty -Path “HKCU:\Software\Labman” -Name “NoAttendees” -Value 40▪ Get-ItemProperty -Path “HKCU:\Software\Labman” -Name “NoAttendees”▪ Remove-Item “HKCU:\Software\Labman” -Force -Recurse

Same commands for files! 21

Page 22: Powershell - labman2018.umd.edu · Powershell is a natural replacement for your batch install scripts Benefits: Full control over install window Easy to check for existing software

System Management and Administration - Restart!

Restart the computer:▪ Restart-Computer ▪ Restart-Computer -ComputerName “labmachine01” -Wait -For

PowerShell -Timeout 300 -Delay 2Stop / Shutdown the computer:▪ Stop-Computer

22

Page 23: Powershell - labman2018.umd.edu · Powershell is a natural replacement for your batch install scripts Benefits: Full control over install window Easy to check for existing software

5.Remote Powershell

Now do it all on remote machines.....

Page 24: Powershell - labman2018.umd.edu · Powershell is a natural replacement for your batch install scripts Benefits: Full control over install window Easy to check for existing software

PS Remoting - Initial Setup

▪ Powershell can be used in both interactive and scripted modes. Cmdlets may also work on remote computers.

▪ Requires setup… https://tek.io/2wH6Zr9▫ Windows Remote Management - On/Allowed IPs▫ Windows Remote Management Service - Started▫ Firewall settings▫ Fairly in depth… I’ll leave this up to you.

24

Page 25: Powershell - labman2018.umd.edu · Powershell is a natural replacement for your batch install scripts Benefits: Full control over install window Easy to check for existing software

PS Remoting - PSSession

Interactive Mode:▪ Enter-PSSession -ComputerName “syslab01”▪ [syslab01] PS C:\ > Get-ChildItem▪ [syslab01] PS C:\ > Exit

25

Page 26: Powershell - labman2018.umd.edu · Powershell is a natural replacement for your batch install scripts Benefits: Full control over install window Easy to check for existing software

PS Remoting - PSSession Remote Scripting

$s = New-PSSession -ComputerName “syslab01”

Invoke-Command -Session $s -ScriptBlock {

Get-Process | Export-CSV -Path “C:\processlist.txt”

}

Copy-Item -FromSession $s -Path “C:\processlist.txt” -Destination ‘C:\reports\’

26

Page 27: Powershell - labman2018.umd.edu · Powershell is a natural replacement for your batch install scripts Benefits: Full control over install window Easy to check for existing software

PS Remoting - PSSession Multiple Hosts Scripting

# syslab.txt is a file with a list of computer names, one per-line

$s = New-PSSession -ComputerName (Get-Content syslab.txt)

Invoke-Command -Session $s -ScriptBlock {

Get-ComputerInfo

}

27

Page 28: Powershell - labman2018.umd.edu · Powershell is a natural replacement for your batch install scripts Benefits: Full control over install window Easy to check for existing software

PS Remoting - Cmdlet “ComputerName”

# syslab.txt is a file with a list of computer names, one per-line

Invoke-Command -ComputerName (Get-Content syslab.txt) -FilePath C:\Scripts\myScript.ps1 -ArgumentList Process, Service

https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/invoke-command?view=powershell-6

28

Page 29: Powershell - labman2018.umd.edu · Powershell is a natural replacement for your batch install scripts Benefits: Full control over install window Easy to check for existing software

5.Software Installations

Get away from the batch scripts...

Page 30: Powershell - labman2018.umd.edu · Powershell is a natural replacement for your batch install scripts Benefits: Full control over install window Easy to check for existing software

Software Installations - Get Rid of Batch

▪ Powershell is a natural replacement for your batch install scripts▪ Benefits:

▫ Full control over install window▫ Easy to check for existing software installs▫ Native ability to remote copy installer and start install▫ Built-In Provider Packages (aka Chocolatey)

30

Page 31: Powershell - labman2018.umd.edu · Powershell is a natural replacement for your batch install scripts Benefits: Full control over install window Easy to check for existing software

Software Installations - Run those installers...

Basic installation of a file: http://bit.ly/2IDfCVi▪ Start-Process -NoNewWindow -Wait -FilePath

“C:\installers\7zip_x64.exe” -ArgumentWindow ‘/S’Got a not-so-silent installer? Control your window…▪ Start-Process -WindowStyle

{Normal|Hidden|Minimized|Maximized} ...

31

Page 32: Powershell - labman2018.umd.edu · Powershell is a natural replacement for your batch install scripts Benefits: Full control over install window Easy to check for existing software

Software Installations - Check Existing Software Installs

How to check for existing software installs: http://bit.ly/2rWfxFJ1. (Bad/slow way) Get-WmiObject -Class Win32_Product2. (Better) Get-ItemProperty

HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* | Select-Object DisplayName, DisplayVersion, Publisher, InstallDate | Format-Table -AutoSize

Detect if the install is needed, only continue if it is old/not-existent...

32

Page 33: Powershell - labman2018.umd.edu · Powershell is a natural replacement for your batch install scripts Benefits: Full control over install window Easy to check for existing software

Software Installations - Chocolatey integration

Use chocolatey? Easy Powershell integration: http://bit.ly/2J0tsol▪ Find-Package -ProviderName Chocolatey -Name 7zip | Install-

Package

33

Page 34: Powershell - labman2018.umd.edu · Powershell is a natural replacement for your batch install scripts Benefits: Full control over install window Easy to check for existing software

6.Windows Update

How to run your Windows Update...

Page 35: Powershell - labman2018.umd.edu · Powershell is a natural replacement for your batch install scripts Benefits: Full control over install window Easy to check for existing software

Windows Update

▪ PSWindowsUpdate module from PowerShell Gallery▫ https://www.powershellgallery.com

▪ Install via the nuget system▪ Install once, then load on-use

35

Page 36: Powershell - labman2018.umd.edu · Powershell is a natural replacement for your batch install scripts Benefits: Full control over install window Easy to check for existing software

Windows Update - PSWindowsUpdate install

▪ Install-PackageProvider NuGet -Force▪ Import-PackageProvider NuGet -Force▪ Set-PSRepository -Name PSGallery -InstallationPolicy Trusted▪ Install-Module PSWindowsUpdate

36

Page 37: Powershell - labman2018.umd.edu · Powershell is a natural replacement for your batch install scripts Benefits: Full control over install window Easy to check for existing software

Windows Update - PSWindowsUpdate Operations

▪ Get-Command -module PSWindowsUpdate▪ Get updates:

▫ Get-WUInstall -AcceptAll -AutoReboot▪ List updates:

▫ Get-WUList ▪ Results from last run:

▫ Get-WULastResults*The cmdlets may take a bit to run.

37

Page 38: Powershell - labman2018.umd.edu · Powershell is a natural replacement for your batch install scripts Benefits: Full control over install window Easy to check for existing software

38

THANKS!ANY QUESTIONS?▪ [email protected]▪ 616-526-6785▪ https://cs.calvin.edu

Presentation URL: http://bit.ly/2ICK4Pt

Page 39: Powershell - labman2018.umd.edu · Powershell is a natural replacement for your batch install scripts Benefits: Full control over install window Easy to check for existing software

CREDITS

Special thanks to all the people who made and released these awesome resources for free:▪ Presentation template by SlidesCarnival▪ Photographs by Unsplash

39

Page 40: Powershell - labman2018.umd.edu · Powershell is a natural replacement for your batch install scripts Benefits: Full control over install window Easy to check for existing software

40