Powerhell para principiantes

Cuando trabajamos con PowerShell, lo primero que encontramos es el comando (Cmdlet).
La llamada de comando se ve así:


Verb-Noun -Parameter1 ValueType1 -Parameter2 ValueType2[]

Ayuda


La ayuda se llama en PowerShell utilizando el comando Get-Help. Puede especificar uno de los parámetros: ejemplo, detallado, completo, en línea, showWindow.

Get-Help Get-Service -full devolverá una descripción completa del comando Get-Service
Get-Help Get-S * mostrará todos los comandos y funciones disponibles a partir de Get-S *


También hay documentación detallada en el sitio web oficial de Microsoft.


Aquí hay un ejemplo de ayuda para el comando Get-Evenlog


imagen

[], .
, . , .


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.


imagen

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


Inicialización de matriz:


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

Inicializando una matriz vacía:


$ArrayExample = @()

Obteniendo el valor por índice:


$ArrayExample[0]

Obtenga toda la matriz:


$ArrayExample

Agregar un artículo:


$ArrayExample += “Third”

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

Clasificación:


$ArrayExample | Sort

$ArrayExample | Sort -Descending

Pero la matriz en sí con esta clasificación permanece sin cambios. Y si queremos datos ordenados en la matriz, entonces debemos asignar los valores ordenados:


$ArrayExample = $ArrayExample | Sort

De hecho, no hay ningún elemento eliminado de la matriz en PowerShell, pero puede hacerlo de esta manera:


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

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

Eliminar matriz:


$ArrayExample = $null

Bucles


Sintaxis de bucle:


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){}

Salir del break loop.


Saltar continuar.


Declaraciones condicionales


if () {} elseif () {} else

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

Función


Definición de funciones:


function Example () {
  echo &args
}

Lanzamiento de funciones:


Example “First argument” “Second argument”

Definiendo argumentos en una función:


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

function Example ($first, $second) {}

Lanzamiento de funciones:


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

Excepción


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

All Articles