четверг, 18 июля 2019 г.

POWERSHELL FOR BEGINNERS

Powershell runs:
* Windows native commands:
> calc
> notepad

* aliases - shortcut for commands
> cd\
> ls
> clear
To see alias: Get-Alias dir

dir -> Get-ChildItem

* script
* cmdlets: verb-noun structure. Verb- what you gonne do and noun who do you what to?
> Set-Location C:\
> Get-ChildItem
> Get-Aliases

PS HELP_SYSTEM

Most Impotent thing in powershell is help.

You have update powershell help system before your work
> update-help
> get-help #searches all help systems in about_* help system too
> get-help *process # search by noun

Name                              Category  Module                    Synopsis
----                              --------  ------                    --------
Get-Process                       Cmdlet    Microsoft.PowerShell.M... Gets the processes that are running on the loc...
Start-Process                     Cmdlet    Microsoft.PowerShell.M... Starts one or more processes on the local comp...
Stop-Process                      Cmdlet    Microsoft.PowerShell.M... Stops one or more running processes.
Wait-Process                      Cmdlet    Microsoft.PowerShell.M... Waits for the processes to be

 > get-help Get-Process
> help *firewall
> get-help get-*service* # serach by verb & nouns
> get-help *verb*
> get-help *operators*
> get-verb

> get-help Get-Process -Detailed
> get-help Get-Service -Examples #go to directly to examples
> get-help Get-Process -full # additional information for pipelines usage
> get-help get-service -ShowWindow # open help in a window & easy find option
>


> man about_operators
    about_Arithmetic_Operators
    about_Assignment_Operators
    about_Comparison_Operators
    about_Logical_Operators
    about_Type_Operators
    about_Split
    about_Join
    about_Redirection

SYNTAX OF PS COMMANDS


> Get-Fake -param Arg
> Get-Service [[-Name] <String[]>] means  Get-Service -Name arg1,arg2

Powershell v5 dynamicly loads modules, so you can just find command you need and trigger it, PSv5 will automatically load command module
Add commands module to PS
> Get-Module
> Get-AdComputer -filter *
> Get-Module # now you can see that ActiveDirectory module uploaded

Example:
get-help Get-EventLog
SYNTAX
    Get-EventLog [-LogName] <String> [[-InstanceId] <Int64[]>] [-After <DateTime>] [-AsBaseObject] [-Before <DateTime>]
     [-ComputerName <String[]>] [-EntryType {Error | Information | FailureAudit | SuccessAudit | Warning}] [-Index <Int
    32[]>] [-Message <String>] [-Newest <Int32>] [-Source <String[]>] [-UserName <String[]>] [<CommonParameters>]


    Get-EventLog [-AsString] [-ComputerName <String[]>] [-List] [<CommonParameters>]
Get-EventLog has 2 variants to be used and difference is in argument -List
<String>  means its can take only 1 argument.
<String[]> means  its required and can take multiple arguments, like: param1,param2,param3.
[-ComputerName <String[]>] [] means optional setting.
If you see -Setting without [] means its required.

PIPELINES

Be careful with pipelines!

Add -WhatIf or -Confirm to check what this command will do

> Get-Service | stop-service -Whatif

CSV


If you want manipulate csv file in pipeline - use Convertto-csv
> # will get all services to a file Serv1.csv and create empty Server2.csv
> Get-Service | Export-csv -Path Serv1.csv | Out-File -FilePath Server2.csv

> # will get all services and put it to a Server2.csv without type information
>Get-Service | Convertto-csv -NoTypeInformation | Out-File -FilePath Server2.csv

OBJECTS


Objects has methods and properties.
Methods are the things object can do: Start, Stop etc.
Properties describe the objects, like what color is it, how tall is it?


> get-service | gm # gm is an alias of Get-Member to find Methods and Properties

TypeName: System.ServiceProcess.ServiceController

Name                      MemberType    Definition
----                      ----------    ----------
Name                      AliasProperty Name = ServiceName
RequiredServices          AliasProperty RequiredServices = ServicesDependedOn
Disposed                  Event         System.EventHandler Disposed(System.Object, System.EventArgs)
Close                     Method        void Close()
Continue                  Method        void Continue()
CreateObjRef              Method        System.Runtime.Remoting.ObjRef CreateObjRef(type requestedType)
Dispose                   Method        void Dispose(), void IDisposable.Dispose()
Equals                    Method        bool Equals(System.Object obj)
ExecuteCommand            Method        void ExecuteCommand(int command)
GetHashCode               Method        int GetHashCode()
GetLifetimeService        Method        System.Object GetLifetimeService()
GetType                   Method        type GetType()
InitializeLifetimeService Method        System.Object Init
anPauseAndContinue       Property      bool CanPauseAndContinue {get;}
CanShutdown               Property      bool CanShutdown {get;}
CanStop                   Property      bool CanStop {get;}
Container                 Property      System.ComponentModel.IContainer Container {get;}
DependentServices         Property      System.ServiceProcess.ServiceController[] DependentServices {get;}
DisplayName               Property      string DisplayName 

What for we need to use 

What for we need to use properties? Because default command shows only a part of properties by default
For example,
> get-service # shows us only a few properties like

Status   Name               DisplayName
------   ----               -----------
Stopped  AJRouter           Служба маршрутизатора AllJoyn
Stopped  ALG                Служба шлюза уровня приложения


But it can also show next properties:
CanPauseAndContinue       Property 
CanShutdown               Property
CanStop                   Property
Container                 Property
DependentServices         Property
DisplayName               Property
MachineName               Property
ServiceHandle             Property
ServiceName               Property
ServicesDependedOn        Property
ServiceType               Property
Site                      Property
StartType                 Property
Status                    Property


USE PROPERTIES


> get-service -name bits | Select-Object -Property Name,Status,MachineName
> get-service -name bits | Select-Object -Property Name,Status,MachineName | format-table -AutoSize
>Gsv bits | Select name, status # short command using aliases

SORTING INFORMATION

> get-service |select-object -Property name, status | Sort-Object -Property status -Descending

NB!! Always sort BEFORE you select.
Because Select-Object selects only 1 property you set, after it you cant sort by other properties.

Customizing your own properties

> get-service | Select-Object -Property qwe, qwe23
PS will create this properties -its a feature
>> get-service | Select-Object -Property qwe, qwe23 |gm


> get-service -name bits | select-object -Property @{name="ServiceName";expression={$_.Name}}, status

ServiceName  Status
-----------  ------
bits        Stopped

$_ means current object coming across pipeline

Example:
> Get-WmiObject -Class win32_logicaldisk -filter "DeviceID='c:'" | Select-Object -Property DeviceId, @{n="FreeGB";e={$_.FreeSpace /1gb -as [int]}}



FILTERING DATA

Operators:

-gt # Greater than
-lt #Lower Than
-ne # Not equal
-le #less than or equal
-eq # Equel "Hello" -eq "HELLO"  #True
-ceq # Case Sensitive equal "Hello" -eq "HELLO" # False

> get-service | Where-Object {$_.Status -eq "Stopped" -and $_.name -like "b*"}
> get-service -name b*  # you can also filter by name


METHODS

Each time you need to find object you need to trigger gm

> Get-Service | gm
Anything that says Method is something that the object can do

> Get-Service -name bits | foreach-Object {$_.start()}
> Get-Service -name bits | foreach-Object {$_.stopt()}
> Get-Service -name bits | foreach-Object {$_.status}

PS AUTOMATION SECURITY

> get-help *execution*
> get-help about_Execution_Policies
Use RemoteSigned (digitally signed) and AllSigned
> Get-ExecutionPolicy
> Set-ExecutionPolicy remotesigned (Run locally created scripts and signed scripts)


VARIABLES FOR STORAGE

> get-help *variable*
> $var= "Hello" # create variable
> write-output $var
> $var
> $var= get-Object -Name bits
> $var
> $var.Status #Stopped    Reference properties and methods
> $var.name  #bits
> $var.Start()
> $var.Status # Stopped Because variable is not updated
> $var.refresh() # Update variable after command execution
> $var.Status #Running

Variables as an array
> $var=1,2,3,4
       0,1,2,3  #Start count arguments from 0
> $var
1
2
3
4
>$var[1]=9
1
9
3
4

No good reason to use notepad for PS scripts. Use PowerShell ISE.
It shows help, IntelliSense, Debugger.

PARAMETERS SCRIPT

> Get-CimInstance -ClassName Win32_logocaldisk -filter "DeviceID='C:'" -ComputerName Afesh | Select @{n="ComputerName";e={$_.PSComputername}}, @{n="FreeGB";e={$_.Freespace / 1gb -as [int]}}


Divide scripts lines
After | add Enter and tab at next row
After , insert Enter

USING PARAMETER ATTRIBUTES
diskinfo.ps1:

param ([Parameter (Mandatory=$true)][string[]]$computername='DC', $NotForUse)

ClassName -ComputerName $computername Win32_logocaldisk -filter "DeviceID='C:'"  | Select @{n="ComputerName";e={$_.PSComputername}}, @{n="FreeGB";e={$_.Freespace / 1gb -as [int]}}

[Parameter (Mandatory=$true)] - means parameter required

now run it with
> .\diskinfo.ps1 -computername pc1,pc2
> get-help .\diskinfo.ps1

COMMENT_BASED HELP

<#
this is block of comments in Powershell
#>

#single row comment


Create your own help:

<#
.Synopsis
This is brief comments
.Description
This is the long comments
.Parameter ComputerName
This is the name of a remote computer
.Example
Connecting to local computer
DiskInfo -computername localhost
.Example
Connecting to remote computer
DiskInfo -computername DC

# make sure that there is only none or one space after it. If you will add 2 spaces # help will not work.
#>
param ([Parameter (Mandatory=$true)][string[]]$computername='DC', $NotForUse)

ClassName -ComputerName $computername Win32_logocaldisk -filter "DeviceID='C:'"  | Select @{n="ComputerName";e={$_.PSComputername}}, @{n="FreeGB";e={$_.Freespace / 1gb -as [int]}}

>get-help test.ps1

Always create help! Its a professional way to do it.

>get-help about_Comment_Based_Help

POWERSHELL REMOTING

Enable Powershell Remoting

* Enabled on Server 2012 and above
* Enable-PSRemoting
* Firewall requirement: TCP port 5985

#Connect to remote pc one-to one
>Enter-PSSession -ComputerName DC

#Manage multiple servers
>Invoke-Command -ComputerName dc,pc1,pc2 -ScriptBlock {Get-Service -name bits}

or
>$sessions=New-PsSession -ComputerName pc2,pc3
>Invoke-Command -Session $sessions {$var=2}
>Invoke-Command -Session $sessions {write-output $var}

>Enter-PSSession -ComputerName pc3
> Get-WindowsFeature # Works on Servers, not on clients
> $servers = 'pc1', 'ps2'
> $servers | ForEach-Object {Start iexplore http://$_}
> $sessions=New-PSSession -ComputerName $servers
> Invoke-Command -Session $sessions {install-windowsfeature web-server}

> $servers | Foreach-Object {Copy-Item .\default.htm -Destination \\$_\c$\inetpub\wwwroot}
$servers | Foreach-Object {Start iexplore http://$_}


> Get-WindowsFeature *gui* | Install-WindowsFeature

Implicit Remoting
> get-adcomputer -filter *
> $adSession=New-PSSession -ComputerName dc
> Import-PSSession -Session $adSession -Module ActiveDirectory
> get-help *ad*
> get-adcomputer -filter *

>$profile
>new-item $profile -ItemType file -Force
# you can add any command that will start up evrytime you start ps session
>ise $profile



Sources:
https://www.linkedin.com/learning/powershell-5-essential-training/
https://github.com/PoshCode/PowerShellPracticeAndStyle/tree/master/Best-Practices
http://powershell.org/wp/ebooks









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