# Demo: jak to działa? #region Obiekt w rurce ps & { $_.GetType(.FullName #region Begin -> Process -> End $Przed = $false $Po = $false 1,2,3 & { Begin { Write-Warning "Begin 1" Write-Warning "Tu nic nie ma: $_" $Global:Przed = $true Process { Write-Warning "Process 1" Write-Warning "jest: $_" $_ End { Write-Warning "End 1" Write-Warning "ostatni element: $_" $Global:Po = $true & { Begin { Write-Warning "Begin 2" Process { Write-Warning "Process 2" End { Write-Warning "End 2" #region Kim jesteś, co masz? notepad; notepad; notepad Get-Process -Name notepad Stop-Process -WhatIf @' Name notepad '@ ConvertFrom-Csv Tee-Object -Variable Lista $Lista Stop-Process -WhatIf $Lista Stop-Process Get-Service Stop-Process -WhatIf -ErrorAction SilentlyContinue Get-Process Get-Item -EA 0 select -First 10 Format-Table -AutoSize Name, FullName, Length Get-Help -Name Get-Process -Parameter InputObject Get-Help -Name Get-Process -Parameter Name Get-Help -Name Get-Item -Parameter Path Get-Process -Id $PID select Path
# Demo: Where i Foreach + bonusy... #region Where $true 'csrss','lsass' -contains $_.Name $_.Name -notmatch '.s.ss' Get-Process Where-Object { $_ Get-Item -EA 0 Where-Object { $_.Length -gt 1MB Write-Warning "Name $($_.Name: Size $($_.Length" #region foreach $ListaStringow = @' Pracownik "Nowak, Anna" "Powers, Austin" "Fasola, Jaś" "Groźny, Iwan" '@ ConvertFrom-Csv $ListaStringow ForEach-Object { $Nazwisko, $Imie = $_.Pracownik.Split(',' $Imie = $Imie.Trim( # Get-ADUser -LdapFilter "(&(givenname=$imie(sn=$nazwisko" Imie = $Imie Nazwisko = $Nazwisko #region opcjonalnie - inne *-Object # Co mamy...? Get-Command -Noun Object # sortujemy... ls *.txt Sort-Object -Property LastWriteTime ls *.txt Sort-Object -Property { [int]($_.basename -replace '.*?(\d+.*', '$1' # grupujemy ls Group-Object -Property Extension Get-Service Group-Object { $_.Name.Substring(0,3 Where-Object Count -gt 3
#region nowości Get-ChildItem Where-Object { $_.Name -Match 'ps1$' Show-Command Where-Object gps? Path -m System32 Get-ChildItem ForEach-Object -MemberName Name Show-Command ForEach-Object gps? Path -m System32 % Path calc; calc; calc gps calc % Id gps calc? Id -gt 7.KB % Kill gps calc kill gwmi win32_share -list gm -MemberType Method % Definition gwmi win32_share -list % Create $PSHOME Posh 0 gwmi win32_share? Name -m ^P gwmi win32_share? Name -m ^P % Delete
# Demo: trick-shots #region Po nazwie Get-ChildItem -Filter *.txt ForEach-Object { $NowaNazwa = $_.Name -replace '_(\d+', '-$1' Rename-Item -Path $_.FullName -NewName $NowaNazwa -WhatIf Get-Help -Name Rename-Item -Parameter NewName Get-Help -Name Rename-Item -Parameter LiteralPath Trace-Command -Name ParameterBinding -Expression { Get-ChildItem -Filter *.txt Rename-Item -NewName { $_.Name -replace '_(\d+', '-$1' -WhatIf -PSHost Get-ChildItem -Filter *.txt Get-Member LiteralPath (Get-Command Rename-Item.Parameters.LiteralPath.Aliases Get-ChildItem -Filter *.txt Get-Member PSPath Get-Help New-ADUser -Parameter * where pipelineinput -m ByPropertyName ft -a name, pipelineinput ipmo ActiveDirectory New-PSDrive -Name Monad -PSProvider ActiveDirectory -Root '' -Server 192.168.100.1 - Credential $MonadCredentials cd 'Monad:\OU=Users,OU=IT,OU=War,DC=monad,DC=ps1' @' Login,Nazwisko,Imie JasFasola,Fasola,Jas '@ ConvertFrom-Csv New-ADUser -SamAccountName { $_.Login -GivenName { $_.Imie -Surname { $_.Nazwisko -DisplayName { "{0, {1" -f $_.Nazwisko, $_.Imie -EmailAddress { "{0.{1@wguisw.org" -f $_.Imie, $_.Nazwisko -Name { "{0.{1" -f $_.Imie, $_.Nazwisko -UserPrincipalName { "{0{1@wguisw.org" -f $_.Imie, $_.Nazwisko ls -Properties * #region Nasze rurkowate function Read-PipelineZle { ValueFromPipeline = $true ]$Test $Test 1,2,3 Read-PipelineZle
function Read-PipelineDobrze { ValueFromPipeline = $true ]$Test $Test 1,2,3 Read-PipelineDobrze function Read-PipelineWiele { ValueFromPipelineByPropertyName = $true ] [Alias('Jas','Malgosia'] [string]$pierwszy, ValueFromPipelineByPropertyName = $true ] [Alias('Czarownica'] [string]$drugi "$Pierwszy piecze $Drugi" Pierwszy = 'Jas' Drugi = 'Czarownica' Read-PipelineWiele Jas = 'Jas' Czarownica = 'Czarownica' Read-PipelineWiele Pierwszy = 'Czarownica' Jas = 'Jas' Read-PipelineWiele -Drugi { $_.Jas #region podmianka $Komenda = [System.Management.Automation.ProxyCommand]::Create( (Get-Command Get-EventLog $Plik = $psise.currentpowershelltab.files.add( $Plik.Editor.Text = @" function Get-EventLog { $Komenda "@