# Demo: jak to działa?
Transkrypt
# Demo: jak to działa?
# Demo: jak to działa?
#region Obiekt w rurce
ps | & {
process {
$_.GetType().FullName
}
}
#endregion
#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"
"Przed: $Przed"
"Po: $Po"
"`$_: $_"
} Process {
Write-Warning "Process 2"
"Przed: $Przed"
"Po: $Po"
"`$_: $_"
} End {
Write-Warning "End 2"
"Przed: $Przed"
"Po: $Po"
"`$_: $_"
}}
#endregion
#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
#endregion
# Demo: Where i Foreach + bonusy...
#region Where
Get-Process *ss | Where-Object {
$true
}
Get-Process *ss | Where-Object {
'csrss','lsass' -contains $_.Name
}
Get-Process *ss | Where-Object {
$_.Name -notmatch '.s.ss'
}
Get-Process | Where-Object {
$_ | Get-Item -EA 0 | Where-Object {
$_.Length -gt 1MB
Write-Warning "Name $($_.Name): Size $($_.Length)"
}
}
#endregion
#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))"
[PSCustomObject]@{
Imie = $Imie
Nazwisko = $Nazwisko
}
}
#endregion
#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
#endregion
#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
gwmi win32_share
gwmi win32_share
gwmi win32_share
#endregion
-list | gm -MemberType Method | % Definition
-list | % Create $PSHOME Posh 0
| ? Name -m ^P
| ? 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 *
#endregion
#region Nasze rurkowate
function Read-PipelineZle {
param (
[Parameter(
ValueFromPipeline = $true
)]$Test
)
$Test
}
1,2,3 | Read-PipelineZle
function Read-PipelineDobrze {
param (
[Parameter(
ValueFromPipeline = $true
)]$Test
)
process {
$Test
}
}
1,2,3 | Read-PipelineDobrze
function Read-PipelineWiele {
param (
[Parameter(
ValueFromPipelineByPropertyName = $true
)]
[Alias('Jas','Malgosia')]
[string]$Pierwszy,
[Parameter(
ValueFromPipelineByPropertyName = $true
)]
[Alias('Czarownica')]
[string]$Drugi
)
process {
"$Pierwszy piecze $Drugi"
}
}
[PSCustomObject]@{
Pierwszy = 'Jas'
Drugi = 'Czarownica'
} | Read-PipelineWiele
[PSCustomObject]@{
Jas = 'Jas'
Czarownica = 'Czarownica'
} | Read-PipelineWiele
[PSCustomObject]@{
Pierwszy = 'Czarownica'
Jas = 'Jas'
} | Read-PipelineWiele -Drugi { $_.Jas }
#endregion
#region podmianka
$Komenda = [System.Management.Automation.ProxyCommand]::Create(
(Get-Command Get-EventLog)
)
$Plik = $psISE.CurrentPowerShellTab.Files.Add()
$Plik.Editor.Text = @"
function Get-EventLog {
$Komenda
}
"@
#endregion