Questions tagged [scriptblock]

Single unit of operation of expression and statements.

Multiple expressions or statements which make up a single unit of operations. The block can accept return values and arguments. Usually found in a statement list in braces such as {<list of statements>}.

154 questions
4
votes
1 answer

PowerShell: Start-Job -scriptblock Multi line scriptblocks?

Hoping someone can help me to figure out if this is possible. I have a 20 ish lines that scan a log file in a while loop. I need this to happen in parallel with the rest of the script. The log scanning loop is passing the entries into SQLite and…
SynxUK
  • 83
  • 1
  • 10
4
votes
1 answer

Powershell create scriptblock for Func instead of Action

I have a class I'm attempting to use from Powershell. This class has a method, called Execute(), with two overloads: one which takes a Func and one which takes an Action. I can call the overload that takes an Action using a scriptblock as…
Mark
  • 11,257
  • 11
  • 61
  • 97
4
votes
1 answer

Powershell function creation with Set-Item and GetNewClosure

I am attempting to build a function that can itself create functions through the Set-Item command where I pass a scriptblock for the new function to the -Value parameter of Set-Item. I'm running into an issue where using GetNewClosure on the…
LoganTheSnowEater
  • 555
  • 2
  • 4
  • 17
4
votes
2 answers

How to invoke a powershell scriptblock with $_ automatic variable

What works - Lets say I have a scriptblock which I use with Select-Object cmdlet. $jobTypeSelector = ` { if ($_.Type -eq "Foo") { "Bar" } elseif ($_.Data -match "-Action ([a-zA-Z]+)") { …
Vikas Gupta
  • 4,455
  • 1
  • 20
  • 40
4
votes
4 answers

How to write a PowerShell advanced function that can work with both piped in objects and objects get from parameter value?

I'm writing a function Chunk-Object that can chunk an array of objects into sub arrays. For example, if I pass it an array @(1, 2, 3, 4, 5) and specify 2 elements per chunk, then it will return 3 arrays @(1, 2), @(3, 4) and @(5). Also the user can…
user133580
  • 1,479
  • 1
  • 19
  • 28
4
votes
2 answers

command execution ordering inside a PowerShell scriptblock

I got excited with PowerShell ScriptBlock at first but I was confused recently with its executing ordering inside blocks. For example: $test_block = { write-host "show 1" ps write-host "show 2" Get-Date } The output by calling…
dave
  • 43
  • 6
3
votes
0 answers

Add-Member to add a custom method to a PowerShell object

I want to add a custom method to an existing object. My problem is I may not find out how to make it accept parameters. In this greatly simplified example I want to add a script block to a System.IO.FileInfo-Object to output a specific parameter to…
GuidoT
  • 280
  • 1
  • 12
3
votes
3 answers

++ Operator on Variable Is Not Changing As Expected In ScriptBlock

I am trying to rename files by putting a prefix based on an incrementing counter in the files such as: $directory = 'C:\Temp' [int] $count=71; gci $directory | sort -Property LastWriteTime | ` rename-item -newname {"{0}_{1}" -f $count++, $_.Name}…
ΩmegaMan
  • 29,542
  • 12
  • 100
  • 122
3
votes
1 answer

How can I invoke a scriptblock in the caller's context?

Consider the following call site: $modifiedLocal = 'original local value' 'input object' | SomeScriptblockInvoker { $modifiedLocal = 'modified local value' [pscustomobject] @{ Local = $local DollarBar = $_ …
alx9r
  • 3,675
  • 4
  • 26
  • 55
3
votes
2 answers

How to call function outside a scriptblock

I have a function that I should be able to call from any place in my powershell script. The problem is that it doesn't identify the function in a script block. In the following example I have the function getNumberFive which should return the…
E235
  • 11,560
  • 24
  • 91
  • 141
3
votes
1 answer

Explain scoping for functions called from PowerShell closures

The following PowerShell code displays unexpected scoping behavior for functions called from closures. Can you explain whether this is "by design", or is a defect? function runblock($block) { $x = 4 & $block } function printx() { " in…
John Rees
  • 1,553
  • 17
  • 24
3
votes
3 answers

I cannot use begin/process/end block in a ForEach-Object?

I've got the following code: function f() { begin{$count=0} process{$count+=10} end{$count} } 1..10|f # OK 1..10|%{ begin{$count=0} process{$count+=10} end{$count} } # Error The first "f" call succeeds, while the %{} block…
vik santata
  • 2,989
  • 8
  • 30
  • 52
3
votes
4 answers

When is a ScriptBlock not a ScriptBlock?

I do not mean to sound too cute with the question, but that really is the question at hand. Consider the following two functions defined in a PowerShell module Test.psm1 installed under $env:PSModulePath: function Start-TestAsync { …
Michael Sorens
  • 35,361
  • 26
  • 116
  • 172
3
votes
1 answer

Undocumented changes to Powershell Scope handling v2/v3?

Background: I've been writing a powershell script to migrate files from a Sharpoint 2010 instance on Windows Server '08 (with Powershell 2.x) to a Sharepoint 2013 instance on Windows Server '12 (with Powershell 3.x). I have that working but I…
CodePartizan
  • 318
  • 3
  • 7
3
votes
5 answers

PowerShell ScriptBlock and multiple functions

I have written the following code: cls function GetFoo() { function GetBar() { $bar = "bar" $bar } $foo = "foo" $bar = GetBar $foo $bar } $cred = Get-Credential "firmwide\srabhi_adm" $result =…
Knows Not Much
  • 30,395
  • 60
  • 197
  • 373
1
2
3
10 11