Difference between revisions of "PowerShell/Core7"

From Ever changing code
Jump to navigation Jump to search
Line 146: Line 146:
</syntaxhighlightjs>
</syntaxhighlightjs>


= Ssh remote access =
= [https://github.com/PowerShell/Win32-OpenSSH/wiki/TTY-PTY-support-in-Windows-OpenSSH Ssh remote access] =
* [https://docs.microsoft.com/en-us/powershell/scripting/learn/remoting/ssh-remoting-in-powershell-core?view=powershell-7 PowerShell remoting over SSH]
<syntaxhighlightjs lang="powershell">
<syntaxhighlightjs lang="powershell">
# Help on syntax
# Help on syntax
Line 160: Line 161:
New-PSSession [-HostName] <string[]> [-Name <string[]>] [-Port <int>] [-UserName <string>] [-KeyFilePath <string>] [-SSHTransport] [-Subsystem <string>] [<CommonParameters>]
New-PSSession [-HostName] <string[]> [-Name <string[]>] [-Port <int>] [-UserName <string>] [-KeyFilePath <string>] [-SSHTransport] [-Subsystem <string>] [<CommonParameters>]
New-PSSession -SSHConnection <hashtable[]> [-Name <string[]>] [<CommonParameters>]
New-PSSession -SSHConnection <hashtable[]> [-Name <string[]>] [<CommonParameters>]
</syntaxhighlightjs>
Ssh session
<syntaxhighlightjs lang="powershell">
# Install packages
sudo apt install openssh-server openssh-client
# Ssh-server 'sshd_config' adjustments for 'pssession' to make it work
sudo vi /etc/ssh/sshd_config
PasswordAuthentication yes
PubkeyAuthentication yes
Subsystem      powershell /usr/bin/pwsh -sshs -NoLogo -NoProfile
# PSSession
New-PSSession -HostName 127.0.0.1 -UserName piotr # create an object
Entry-PSSession -HostName 127.0.0.1 -UserName piotr # get into ssh session
</syntaxhighlightjs>
= PowerShell Remoting =
<syntaxhighlightjs lang="powershell">
</syntaxhighlightjs>
</syntaxhighlightjs>


=References=
=References=
*[https://github.com/MicrosoftDocs/PowerShell-Docs/blob/staging/reference/docs-conceptual/whats-new/known-issues-ps6.md Known Issues for PowerShell on Non-Windows Platforms] - read about case sensitivity Linux/Windows
*[https://github.com/MicrosoftDocs/PowerShell-Docs/blob/staging/reference/docs-conceptual/whats-new/known-issues-ps6.md Known Issues for PowerShell on Non-Windows Platforms] - read about case sensitivity Linux/Windows

Revision as of 12:31, 5 July 2020

Installing PowerShell on Linux

Note: Ubuntu 20.04 is an LTS release. PowerShell does not currently support this version. Support for this version is being considered for the PowerShell 7.1 release. PowerShell can only support the distributions that are supported by .NET. See the Core release notes for a list of supported distributions.

snap install powershell --classic
powershell 7.0.2 from Microsoft PowerShell✓ installed

$ powershell # run or use 'pwsh' to start it up
PowerShell 7.0.2
Copyright (c) Microsoft Corporation. All rights reserved.

https://aka.ms/powershell
Type 'help' to get help.

PS /home/piotr>

Concepts

  • module - set of related Windows PowerShell functionalities grouped together as convenient units
  • Scripts should end with .PS1 so they are loaded and run in the current process and it's expected behaviour of PowerShell. The magic number #! can be used, but script will be run in a new PowerShell instance preventing accessing to current process objects. This might be desirable when executing PowerShell from bash or another shell.

Environment profiles

Any commands included in the profile file will be executed during terminal/shell initialization. Note: This is not working while switching the shell and is executed on the new terminal initiation <syntaxhighlightjs lang="powershell"> > $profile /home/piotr/.config/powershell/Microsoft.VSCode_profile.ps1 </syntaxhighlightjs>

Pipeline

Everything in PS is an object, although we use '|' piping this is not the same concept as STDIN and STDOUT in Linux. <syntaxhighlightjs lang="powershell"> PS> Get-Process -Name whoopsie | Get-Member -Name CPU,Company PS> Get-Process -Name whoopsie | Select-Object -Property name,CPU

Name CPU


---

whoopsie 0.24

  1. When assigning to a variable we assign an object not just a string

$variable = Get-Process S> $variable | Select-Object -Property name,CPU | Sort-Object -Descending CPU | Select-Object -First 5

Name CPU


---

gnome-shell 11796.69 firefox 11012.4599999 Web Content 7573.22 Web Content 5085.47 Xorg 3593.26 </syntaxhighlightjs>

Variables

These are objects. <syntaxhighlightjs lang="powershell"> S /home/piotr> Get-Help *variable* Name Category Module Synopsis


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

Clear-Variable Cmdlet Microsoft.PowerShell.Uti… … Get-Variable Cmdlet Microsoft.PowerShell.Uti… … New-Variable Cmdlet Microsoft.PowerShell.Uti… … Remove-Variable Cmdlet Microsoft.PowerShell.Uti… … Set-Variable Cmdlet Microsoft.PowerShell.Uti… … </syntaxhighlightjs>

if

<syntaxhighlightjs lang="powershell"> if ($var -gt 5) {

 write-host "true"

} elseif {

 write host "false"

} </syntaxhighlightjs>

do/while

<syntaxhighlightjs lang="powershell"> Do {

   $number = $number + 1 
   write-host "current nuber is: $number"

} while ($number -lt 10) </syntaxhighlightjs>

for each

<syntaxhighlightjs lang="powershell"> $names = "Bob","Jamse","Tom" $collection = $names $count = 0 foreach ($item in $collection) {

   $count += 1
   Write-Host "$item"

} Write-Host "$count" </syntaxhighlightjs>

Convert

Working with Powershell pipelines it means workign with objects, so we format output but not modifying it. This is power full combination allowing for object/output convertn at any time. <syntaxhighlightjs lang="powershell"> Get-Process | Sort-Object -Descending CPU | Select-Object -First 5 | ConvertTo-Json

  1. more convert formats

Convert-AzSqlDatabase... ConvertFrom-Json ConvertFrom-StringData ConvertTo-Json Convert-AzSqlInstance... ConvertFrom-Markdown ConvertTo-AzVMManagedDisk ConvertTo-ScriptExtent Convert-Path ConvertFrom-ScriptExtent ConvertTo-Csv ConvertTo-SecureString ConvertFrom-Csv ConvertFrom-SecureString ConvertTo-Html ConvertTo-Xml </syntaxhighlightjs>

Add calculated property field

<syntaxhighlightjs lang="powershell">

  1. Syntax: @{ Name = ; Expression = {}}

Get-Process | Select-Object -Property Name, @{ Name = 'MGB'; Expression = {$_.VM / 1gb -as [int]}} | Sort-Object -Property MGB -Descending | Select-Object -First 3

 # MGB - property name; $_  - current object

Name MGB


---

code 12 gnome-shell 4 pwsh 3 </syntaxhighlightjs>


Add a new property to an object by utilising 'ScriptProperty' to enrich an object with extra TypeSet (aka data, column etc.). <syntaxhighlightjs lang="powershell"> Update-TypeData -TypeName System.Diagnostics.process -MemberType ScriptProperty -MemberName MGB -Value {$this.VM/1gb -as [int]} Get-Process | Get-Member M* PS /home/piotr> Get-Process | Get-Member M*

  TypeName: System.Diagnostics.Process

Name MemberType Definition


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

MachineName Property string MachineName {get;} MainModule Property System.Diagnostics.ProcessModule MainModule {get;} (...) Modules Property System.Diagnostics.ProcessModuleCollection Modules {get;} MGB ScriptProperty System.Object MGB {get=$this.VM/1gb -as [int];} </syntaxhighlightjs>

Function

<syntaxhighlightjs lang="powershell"> function func-name {

 set-location /opt/

} </syntaxhighlightjs>

Multiple scripting languages within Powershell

Using hash doc(aka hash string, hash notastion) we can execute multiple scripting languages within Powershell to bounce between multiple languages. Each output is an object with all PowerShell properties. <syntaxhighlightjs lang="powershell"> @'script.py'@ </syntaxhighlightjs>

Ssh remote access

<syntaxhighlightjs lang="powershell">

  1. Help on syntax

Get-Command New-PSSession -Syntax

New-PSSession [[-ComputerName] <string[]>] [-Credential <pscredential>] [-Name <string[]>] [-EnableNetworkAccess] [-ConfigurationName <string>] [-Port <int>] [-UseSSL] [-ApplicationName <string>] [-ThrottleLimit <int>] [-SessionOption <PSSessionOption>] [-Authentication <AuthenticationMechanism>] [-CertificateThumbprint <string>] [<CommonParameters>] New-PSSession [-ConnectionUri] <uri[]> [-Credential <pscredential>] [-Name <string[]>] [-EnableNetworkAccess] [-ConfigurationName <string>] [-ThrottleLimit <int>] [-AllowRedirection] [-SessionOption <PSSessionOption>] [-Authentication <AuthenticationMechanism>] [-CertificateThumbprint <string>] [<CommonParameters>] New-PSSession [-VMId] <guid[]> -Credential <pscredential> [-Name <string[]>] [-ConfigurationName <string>] [-ThrottleLimit <int>] [<CommonParameters>] New-PSSession -Credential <pscredential> -VMName <string[]> [-Name <string[]>] [-ConfigurationName <string>] [-ThrottleLimit <int>] [<CommonParameters>] New-PSSession [[-Session] <PSSession[]>] [-Name <string[]>] [-EnableNetworkAccess] [-ThrottleLimit <int>] [<CommonParameters>] New-PSSession -ContainerId <string[]> [-Name <string[]>] [-ConfigurationName <string>] [-RunAsAdministrator] [-ThrottleLimit <int>] [<CommonParameters>] New-PSSession -UseWindowsPowerShell [-Name <string[]>] [<CommonParameters>] New-PSSession [-HostName] <string[]> [-Name <string[]>] [-Port <int>] [-UserName <string>] [-KeyFilePath <string>] [-SSHTransport] [-Subsystem <string>] [<CommonParameters>] New-PSSession -SSHConnection <hashtable[]> [-Name <string[]>] [<CommonParameters>] </syntaxhighlightjs>


Ssh session <syntaxhighlightjs lang="powershell">

  1. Install packages

sudo apt install openssh-server openssh-client

  1. Ssh-server 'sshd_config' adjustments for 'pssession' to make it work

sudo vi /etc/ssh/sshd_config PasswordAuthentication yes PubkeyAuthentication yes Subsystem powershell /usr/bin/pwsh -sshs -NoLogo -NoProfile

  1. PSSession

New-PSSession -HostName 127.0.0.1 -UserName piotr # create an object Entry-PSSession -HostName 127.0.0.1 -UserName piotr # get into ssh session </syntaxhighlightjs>

PowerShell Remoting

<syntaxhighlightjs lang="powershell"> </syntaxhighlightjs>

References