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>}.

131 questions
55
votes
8 answers

Pass arguments to a scriptblock in powershell

I guess you can't just do this: $servicePath = $args[0] if(Test-Path -path $servicePath) <-- does not throw in here $block = { write-host $servicePath -foreground "magenta" if((Test-Path -path $servicePath)) { <-- throws…
dexter
  • 6,473
  • 9
  • 48
  • 71
19
votes
4 answers

Powershell - how to pre-evaluate variables in a scriptblock for Start-Job

I want to use background jobs in Powershell. How to make variables evaluated at the moment of ScriptBlock definition? $v1 = "123" $v2 = "asdf" $sb = { Write-Host "Values are: $v1, $v2" } $job = Start-Job -ScriptBlock $sb $job | Wait-Job |…
Ivan
  • 7,084
  • 4
  • 49
  • 65
8
votes
1 answer

PowerShell ScriptBlock variable scope

I'm having trouble understanding scopes within ScriptBlocks. I was counting on some kind of closure-like system, but I can't seem to get it working. I have a ScriptBlock that takes a param and returns another ScriptBlock: $sb1 = { …
wensveen
  • 636
  • 8
  • 16
6
votes
2 answers

Supporting lexical-scope ScriptBlock parameters (e.g. Where-Object)

Consider the following arbitrary function and test cases: Function Foo-MyBar { Param( [Parameter(Mandatory=$false)] [ScriptBlock] $Filter ) if (!$Filter) { $Filter = { $true } } #$Filter =…
jimbobmcgee
  • 1,341
  • 8
  • 28
6
votes
1 answer

PowerShell Executing a function within a Script Block using Start-Process does weird things with double quotes

I have a PowerShell script that edits the registry, so it needs to run as admin. To do this, I start up a new PowerShell process from my running PowerShell script, and pass in part of the registry key path using a Script Block with a function in it.…
deadlydog
  • 19,009
  • 14
  • 91
  • 101
5
votes
2 answers

Why doesn't $PSItem behave as expected when using a bracket-based -Filter argument?

I was assisting a user with this question, linked to my answer here: Powershell script to add users to A/D group from .csv using email address only? Initially I wrote the script as follows, using a bracket-based filter for Get-AdUser like…
Bender the Greatest
  • 12,311
  • 17
  • 65
  • 124
5
votes
2 answers

Powershell expand variable in scriptblock

I am trying to follow this article to expand a variable in a scriptblock My code tries this: $exe = "setup.exe" invoke-command -ComputerName $j -Credential $credentials -ScriptBlock {cmd /c 'C:\share\[scriptblock]::Create($exe)'} How to fix the…
Glowie
  • 2,093
  • 18
  • 51
  • 94
5
votes
5 answers

Powershell Start-Process to start Powershell session and pass local variables

Is there a way to use the Powershell Start-Process cmdlet to start a new Powershell session and pass a scriptblock with local variables (once of which will be an array)? Example: $Array = @(1,2,3,4) $String = "This is string number" $Scriptblock =…
atownson
  • 338
  • 2
  • 9
  • 20
4
votes
2 answers

Powershell function with multiple scriptblock parameters

I have been unable to create a Powershell function that accepts more than one scriptblock parameter. Here's the simplified test script. What is the issue with multiple scriptblocks? function Task1 { param([scriptblock]$f={}) …
BC.
  • 22,131
  • 12
  • 44
  • 62
4
votes
0 answers

PowerShell implicit remoting - scriptblock

I am attempting to use implicit remoting. We have a jump server in a different data center. I want to run Invoke-Command implicitly on that jump server to be able to run commands on other servers it can access. I am able to export Invoke-Command,…
tony
  • 51
  • 8
4
votes
3 answers

Powershell: Use a variable to reference a property of $_ in a script block

$var =@( @{id="1"; name="abc"; age="1"; }, @{id="2"; name="def"; age="2"; } ); $properties = @("ID","Name","Age") ; $format = @(); foreach ($p in $properties) { $format += @{label=$p ; Expression = {$_.$p}} #$_.$p is not…
derek
  • 6,738
  • 10
  • 39
  • 69
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
  • 395
  • 2
  • 3
  • 14
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,329
  • 1
  • 17
  • 37
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,359
  • 16
  • 27
4
votes
4 answers

How to pass a named function as a parameter (scriptblock)

Let's take the classic first-order functions example: function Get-MyName { "George" } function Say-Hi([scriptblock]$to) { Write-Host ("Hi "+(& $to)) } This works just fine: Say-Hi { "Fred Flintstone" } this does not: Say-Hi Get-MyName because…
George Mauer
  • 103,465
  • 117
  • 349
  • 581
1
2 3
8 9