powershell basics. background powershell is a task automation and scripting language based off...

14
POWERSHELL BASICS

Upload: helen-carroll

Post on 19-Jan-2016

213 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: POWERSHELL BASICS. BACKGROUND  Powershell is a task automation and scripting language based off the.NET framework  It provides the user full access

POWERSHELL BASICS

Page 2: POWERSHELL BASICS. BACKGROUND  Powershell is a task automation and scripting language based off the.NET framework  It provides the user full access

BACKGROUND

Powershell is a task automation and scripting language based off the .NET framework

It provides the user full access to the COM (Component Object Model) and WMI (Windows Management Instrumentation)

The functions (lightweight commands) that PowerShell utilizes to complete certain tasks are called cmdlets (command-lets)

Page 3: POWERSHELL BASICS. BACKGROUND  Powershell is a task automation and scripting language based off the.NET framework  It provides the user full access

BACKGROUND (CONTINUED)

Cmdlets are not standalone executables; they are instances of .NET framework classes

PowerShell syntax is case insensitive

Page 4: POWERSHELL BASICS. BACKGROUND  Powershell is a task automation and scripting language based off the.NET framework  It provides the user full access

FIRST SCRIPT

Suppose you wanted to query a list of currently running processes on a system. The following command will do just that:

Get-Process

To sort the list of processes, use the pipeline `|` to pass the output to the Sort-Object cmdlet (or Sort)

You can select certain fields of the output by piping the output and passing it using select <field name>

Page 5: POWERSHELL BASICS. BACKGROUND  Powershell is a task automation and scripting language based off the.NET framework  It provides the user full access

FIRST SCRIPT (RUNNING THE SCRIPT)

There are various ways a PowerShell script can be run

Command line Type in the path of the script

If the current directory is the same as the script, the filename must be preceded with “.\” or “./”

File Explorer

Page 6: POWERSHELL BASICS. BACKGROUND  Powershell is a task automation and scripting language based off the.NET framework  It provides the user full access

FIRST SCRIPT (RUNNING THE SCRIPT) Based on the ExecutionPolicy setting on the system, this may

need to be overridden in order to be able to run scripts

To change this setting, you must be an administrator

You can bypass this setting without actually changing the setting by writing a one-line batch file that calls PowerShell to run the script using certain arguments

Page 7: POWERSHELL BASICS. BACKGROUND  Powershell is a task automation and scripting language based off the.NET framework  It provides the user full access

ACTIVE DIRECTORY

PowerShell, being native to the Windows environment, is a powerful language to use when interacting with AD objects

The user can process information more quickly than some GUI utilities

Page 8: POWERSHELL BASICS. BACKGROUND  Powershell is a task automation and scripting language based off the.NET framework  It provides the user full access

ACTIVE DIRECTORY (EXAMPLES) Query all AD users in the domain:

Get-ADUser –Filter *

Get AD users based on a filter: Get-ADUser -Filter 'name -like "parsa*“’

Notice the wildcard at the end of the filter

Filter users based on department: Get-ADUser –Filter ‘office –like “*<department name string>*”’

Page 9: POWERSHELL BASICS. BACKGROUND  Powershell is a task automation and scripting language based off the.NET framework  It provides the user full access

ACTIVE DIRECTORY (EXAMPLES, CONTINUED)

You can query AD computer objects that have been created or modified by a certain date

$d = (Get-Date).addDays(-1) Variable $d is assigned the value of one day before the current

date

Page 10: POWERSHELL BASICS. BACKGROUND  Powershell is a task automation and scripting language based off the.NET framework  It provides the user full access

UPDATING COMPUTER SCRIPT

./updateWorkstation.bat

./updateWorkstation.ps1

Page 11: POWERSHELL BASICS. BACKGROUND  Powershell is a task automation and scripting language based off the.NET framework  It provides the user full access

GET AD COMPUTERS WHEN CREATED SCRIPT

./getComputerModBy.bat

./getComputerModBy.ps1

Page 12: POWERSHELL BASICS. BACKGROUND  Powershell is a task automation and scripting language based off the.NET framework  It provides the user full access

BACKUP USERS SCRIPT

./backup.bat

./backup.ps1

Page 13: POWERSHELL BASICS. BACKGROUND  Powershell is a task automation and scripting language based off the.NET framework  It provides the user full access

GET COMPUTER NAME AND RAM SCRIPT

./getComputerName.bat

./getComputerName.ps1