Received: from mail-ua1-f41.google.com (mail-ua1-f41.google.com [209.85.222.41])
(using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits))
(No client certificate requested)
by xxx.xxx.ru (Postfix) with ESMTPS id 537297EC5E
for <xxx@xxx.ru>; Fri, 15 May 2020 11:58:38 +0300 (MSK)
Received: by mail-ua1-f41.google.com with SMTP id c17so510696uaq.13
for <xxx@xxx.ru>; Fri, 15 May 2020 01:58:38 -0700 (PDT)
$config1.Custom.Property, $config2.AnyProperty | sort-object -unique
#если без фильтрации чтобы сделать массив плоским вместо sort
... foreach-object { $_ }
Get-ChildItem -Recurse | Where-Object {$_.PSIsContainer}
--> Get-ChildItem -Directory -Recurse
$params =@{}
Get-Content d:\params.ini | foreach-object {
If $_ -match '\[param:(^]+)\]\s*=\s*(.*)' { $params[$matches[1]}=$matches[2] }
}
$filelist = get-childitem d:\work *json,*sql
Foreach $file in$filelist {
Get-content $file | foreach-object {
If $_ -match 'Тут нужный regex для файла' -and $params.contains($matches[1]){
$_=$_ - replace 'тут тоже регекс', $params[$atches[1])
}
} |set-content $file
}
$myscriptblock = { mysupercommand $args }
& $myscriptblock $myparams
$scriptblock = { param($fg,$bg, $text) Write-Host -Foreground $fg -Background $bg $text }
& $scriptblock yellow blue text
PS> $text = 'first'
PS> $sb = { "my Text - $text" }
PS> & $sb
my Text - first
PS> $text = 'second'
PS> &$sb
my Text - second
function Invoke-MyTool {
param(
[ValidateSet('ping','write')]
[parameter()]
[string]$cmd,
[parameter(mandatory=$false, position=1, ValueFromRemainingArguments=$true)]$args
)
)
$config = @{
ping = { ping },
'write' = { param($text, $fg, $bg) write-host $text -fore $fg -back $bg }
}
& $config[$cmd] $args
}
$params = @{
Param1 = "value1"
}
if ($something) { $params.Param2 = "*value" }
MyFunction @params
Function Get-OtherCompProcess {
Param(
#здесь все обычные параметры для get-process кроме computername
)}
$PSBoundParameters.computername='othercomp'
Get-Process @PSBoundParameters
$sourcedata = get-content in_file
$jobs =
foreach ($data in $sourcedata) {
Start-Job { somescript $using:data }
}
$result = $jobs | wait-Job | receive-Job
$jobs | remove-Job
$result | set-content out_file
send-mailmessage ...
[parameter(ValueFromPipeline = $true)]
PROCESS{}
- блок будет в цикле получать всё что попадает на вход из пайпа. блоки BEGIN{}
и END{}
задействуются, соответственно в самом начале и конце, там значение $cmds доступно не будетfunction MyFunc() {
param (
[Alias("InputObject")] [parameter(ValueFromPipeline = $true)] [array] $cmds,
[Alias("FilePath")] [string] $fileName
)
BEGIN {}
PROCESS {
$cmds | Out-File $fileName} -Append
}
END{}
}
g++ test.cpp -o test 2>&1 | Tee-Object -Filepath error.txt
invoke-command { 'txt1'; write-error 'err'; 'txt2' } -ErrorVariable e
#пример вызова внешней команды
invoke-command { python 1.py 2>&1 } -ErrorVariable e
$e | Out-File error.txt