$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
$LastExitCode
Contains the exit code of the last Windows-based program that was run.
try {
Copy-Item -ErorAction Stop .......
}
catch {
exit 1
}
MyUtils/
MyUtils.psd1
MyUtils.psm1
functions/
Get-Something.ps1
Get-SomethingOther.ps1
@{
ModuleVersion = '1.0'
GUID = '72d739dd-bddf-4d7c-a358-1a40e2ff961d'
Description = 'MyUtils module'
NestedModules = @('MyUtils.psm1')
}
Try {
Get-ChildItem "$PSScriptRoot\functions\*.ps1" -Exclude *.tests.ps1, *profile.ps1 |
ForEach-Object {
$Function = $_.Name
. $_.FullName
}
} Catch {
Write-Warning ("{0}: {1}" -f $Function,$_.Exception.Message)
Continue
}
function Get-Something {
param(
$parameter
)
Write-Host "I'm Get-Something with $parameter"
}
... | Sort-Object -Property TimeCreated
$date = [datetime]'2020-04-13';
... |
Where-Object { $_.TimeCreated.Date -eq $date } |
Measure-Object -Sum -Property SessionDuration
[Console]::CursorVisible = $false
работает, но до первого нажатия. Потому что его модуль PSReadline по новой включает.if ((TestPath $pathToProjectFile) -eq $false) {
function TestPath($Path) {
Write-Host -fore Green "Args: $args"
return (Test-Path -Path $Path -PathType leaf)
}
Args: -eq False
$x1 = [xml]$xml1
$x2 = [xml]$xml2
$x2.FORM.Group.InnerXml = $x1.FORM.Group.InnerXml
?