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
63
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
  • 7,063
  • 9
  • 54
  • 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
  • 9,089
  • 4
  • 61
  • 74
10
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
  • 783
  • 10
  • 20
7
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
  • 22,611
  • 14
  • 112
  • 118
7
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
  • 368
  • 2
  • 10
  • 22
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,561
  • 11
  • 34
6
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,271
  • 21
  • 60
  • 104
5
votes
3 answers

Call a function inside a Script Block powershell

I have code to register a Tentacle in Octopus and I want to call a Function called RunCommand inside the Scriptblock. It keeps failing when I try to call it inside the Scriptblock. I am reading my data from a csv file but just cant figure out how to…
hankey39
  • 349
  • 2
  • 5
  • 19
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…
codewario
  • 19,553
  • 20
  • 90
  • 159
5
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
  • 9,358
  • 11
  • 53
  • 94
5
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
  • 117,483
  • 131
  • 382
  • 612
4
votes
2 answers

Why does ScriptBlock convert Hashtable to Collection and how to avoid it?

Consider this PowerShell script: [Hashtable] $t = @{} function foo($x) { $x } $t2 = foo $t $t3 = {param([Hashtable] $x) [Hashtable]$x}.Invoke($t) $t4 = $function:foo.Invoke($t) Write-Host "argument type " $t.GetType() Write-Host…
Konrad Jamrozik
  • 3,254
  • 5
  • 29
  • 59
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.
  • 24,298
  • 12
  • 47
  • 62
4
votes
3 answers

For PowerShell cmdlets, can I always pass a script block to a string parameter?

I'm looking at the documentation of PowerShell's Rename-Item cmdlet and there is an example like this. Get-ChildItem *.txt | Rename-Item -NewName { $_.name -Replace '\.txt','.log' } This example shows how to use the Replace operator to rename…
Just a learner
  • 26,690
  • 50
  • 155
  • 234
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
1
2 3
10 11