Powerhell para iniciantes

Ao trabalhar com o PowerShell, a primeira coisa que encontramos é o comando (Cmdlet).
A chamada de comando é assim:


Verb-Noun -Parameter1 ValueType1 -Parameter2 ValueType2[]

Socorro


A ajuda é chamada no PowerShell usando o comando Get-Help. Você pode especificar um dos parâmetros: exemplo, detalhado, completo, online, showWindow.

Get-Help Get-Service -full retornará uma descrição completa do comando Get-Service
Get-Help Get-S * mostrará todos os comandos e funções disponíveis, começando com Get-S *


Também há documentação detalhada no site oficial da Microsoft.


Aqui está um exemplo de ajuda para o comando Get-Evenlog


imagem

[], .
, . , .


EntryType, . .


Required. After , Required false. Position Named. , :


Get-EventLog -LogName Application -After 2020.04.26

LogName Named 0 , :


Get-EventLog Application -After 2020.04.26

:


Get-EventLog -Newest 5 Application

Alias


PowerShell (Alias).


Set-Location cd.



Set-Location “D:\”


cd “D:\”

History


Get-History


Invoke-History 1; Invoke-History 2


Clear-History


Pipeline


powershell . :


Get-Verb | Measure-Object

.


Get-Verb "get"

Get-Help Get-Verb -Full, Verb pipline input ByValue.


imagem

Get-Verb «get» «get» | Get-Verb.
Verb Get-Verb pipline input .
pipline input ByPropertyName. Verb.


Variables


$


$example = 4

>
, $example > File.txt
$example
Set-Content -Value $example -Path File.txt


Arrays


Inicialização da matriz:


$ArrayExample = @(“First”, “Second”)

Inicializando uma matriz vazia:


$ArrayExample = @()

Obtendo o valor por índice:


$ArrayExample[0]

Obtenha toda a matriz:


$ArrayExample

Adicionando um item:


$ArrayExample += “Third”

$ArrayExample += @(“Fourth”, “Fifth”)

Ordenação:


$ArrayExample | Sort

$ArrayExample | Sort -Descending

Mas a matriz em si com essa classificação permanece inalterada. E se queremos dados classificados na matriz, precisamos atribuir os valores classificados:


$ArrayExample = $ArrayExample | Sort

De fato, não há elemento removido da matriz no PowerShell, mas você pode fazer o seguinte:


$ArrayExample = $ArrayExample | where { $_ -ne “First” }

$ArrayExample = $ArrayExample | where { $_ -ne $ArrayExample[0] }

Excluir matriz:


$ArrayExample = $null

rotações


Sintaxe do loop:


for($i = 0; $i -lt 5; $i++){}

$i = 0
while($i -lt 5){}

$i = 0
do{} while($i -lt 5)

$i = 0
do{} until($i -lt 5)

ForEach($item in $items){}

Saindo do loop de interrupção.


Pular continuar.


Instruções condicionais


if () {} elseif () {} else

switch($someIntValue){
  1 { “Option 1” }
  2 { “Option 2” }
  default { “Not set” }
}

Função


Definição de Função:


function Example () {
  echo &args
}

Lançamento da função:


Example “First argument” “Second argument”

Definindo argumentos em uma função:


function Example () {
  param($first, $second)
}

function Example ($first, $second) {}

Lançamento da função:


Example -first “First argument” -second “Second argument”

Exceção


try{
} catch [System.Net.WebException],[System.IO.IOException]{
} catch {
} finally{
}

All Articles