четверг, 5 декабря 2019 г.

POWERSHELL BASICS


PowerShell runs in objects

COMMON NOTES

> get-help
> get-Verb
# find out powershell version
> $PSVersionTable

PS Path
> cd "C:\Windows\System32\WindowsPowerShell"

#upgrade ps
Download Windows Management Framework 5.1

# install PowerShell Core
Download source from https://github.com/PowerShell/PowerShell

Analog of wget in PS is:
Invoke-WebRequest -Uri https://github.com/PowerShell/PowerShell/releases/download/v6.2.3/PowerShell-6.2.3-win-x64.msi -OutFile ./PSCore6.2.msi

If you got an error: Could not create SSl/TLS Secure channel set:
> [net.ServicePointMannager]::SecurityProtocol = "tls12, tls11, tls"
run Invoke-WebRequest again

#check commendlets, aliases and functions
> Get-Command

FUNCTIONS
> Function Get-Time {
> Get-Date -Format hh:mm
> }

> Get-Time
> 5:21

ALIASES

#get all aliases list
> Get-Alias
> ls  = Get-ChildItem

#Create your own alias
> New-Alias -Name Web -Value mkdir

#change existing alies
> Set-Alias -Name Web -Value Invoke-WebRequest

Aliases created with such way will disappear after you close current command prompt.

Execution policies

> Get-ExecutionPolicy -List
> Set-ExecutionPolicy -Scope LocalMachine -ExecutionPolicy AllSigned 

PROFILE
if you need to make aliases and functions permanent at any command prompt you will need to add it to profile.

#check if profile exists
> Test-Path $Profile 

#create profile
> New-Item -Path $Profile -Type File -Force

Edit created file " ThisPC\Documents\WindowsPowerShell\MicrosoftPowerShell_profile.ps1" with your aliases and functions like
> Set-Alias -Name Web -Value Invoke-WebRequest

MODULES

# get list of all modules available on your machine
> Get-Module -ListAvailable

You can find more modules at https://www.powershellgallery.com/

> Install-Module -Name Azure -Repository PSGallery -Force
# to make use this modules after installation you need to import them
> Import-Module AzureRm.Profile
> Import-Module AzureRm.Storage
> Import-Module Azure
# check added from module commands
> Get-Command 

PIPELINES

# pipleline is when you put command output to next command
> Get-Module | Where-Object {$_.Name -Match "^Azure.*"}
#removing all azure modules
> Get-Module | Where-Object {$_.Name -Match "^Azure.*"} | Remove-Module

Managing Outputs

| Out-Host
| Out-Null
| Out-String
| Out-Printer

SHOW PROPERTIES

You can select to show specified object property or few properties.
> (Get-Command).Name   # Shows column Name
> Get-Command | Select-Object Name, Module # Shows column Name & Moduel

Example
Write-Host "Current user:" $env:USERNAME
Write-Host "Computer Name:" $PCName
Write-Host "Operating System:" $((Get-WMIObject win32_operatingsystem).Caption)
Write-Host "IP Address:"
Get-NetIPAddress -AddressFamily IPv4 | Select-Object InterfaceAlias, IpAddress | Out-Host
Write-Host "Last PowerShell Commands:"
Get-History -Count 5


Sources:
https://www.linkedin.com/learning/learning-powershell-for-windows-server-administration/

Комментариев нет: