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
2
votes
1 answer

Can I pipe into a switch statement stored in a variable?

I have a variable that stores a switch statement $com = ' switch ($_) { 1 {"It is one."} 2 {"It is two."} 3 {"It is three."} 4 {"It is four."} } ' I am trying to pipe in the number to run the switch statement something like: 1 |…
I am Jakoby
  • 577
  • 4
  • 19
2
votes
1 answer

Rename columns in Select-Object

I have an object with spaces in their properties names. I want to Select-Object @{n='NewName';e={$_.'Old Name'}} for every NoteProperty. Since there is a lot of them, I created this function. Running this code will return an array of hash tables but…
PollusB
  • 1,726
  • 2
  • 22
  • 31
2
votes
1 answer

Variable in ScriptBlock - Powershell

I have a GUI for my powershell script. If the folder "Temp" in C: does not exist - create folder and the logging entry "Path C:\Temp does not exist - create folder". This is an extract of my code: $infofolder=$global:dText.AppendText("Path C:\Temp…
user18209625
  • 139
  • 2
  • 15
2
votes
2 answers

Powershell implement Switch Statement

I would like to implement a -parallel Switch to one of my skripts Non Parallel Version: $tmpArray | ForEach-Object { #region ... local Variables $stepWidthLocal = $stepWidth <# my code #> Parallel Funktion: $tmpArray |…
2
votes
4 answers

Why does pwsh.exe in combination with Start-Process not accept a script block directly?

Why does Start-Process powershell.exe { Read-Host } work, but Start-Process pwsh.exe { Read-Host } does not? I'm aware of the -ArgumentList switch of Start-Process, but it makes things more complicated when it comes to escaping quotation…
2
votes
1 answer

powershell join scriptblocks

so I a have this huge set of scripts made of scriptblocks in files. The problem is that some functionality is repeated in different scriptblocks, so I am thinking that it would be more elegant if I could split the scriptblocks to even smaller parts…
McVitas
  • 272
  • 1
  • 17
2
votes
0 answers

how to concatenate invoke-expression and variables in a one-liner

I want to set up a scheduled task and need to ideally fit the following in one line. I have tried using a script block {} but does not seem to work. I want to try and put [String] $cmd = Get-Content \pathto\script.ps1 Invoke-Expression…
powerpig
  • 21
  • 1
2
votes
0 answers

Why does PowerShell's Start-Job sometimes finish without executing code?

I have been investigating an issue we see in some deployments at work. For background, we have multiple services deploying into Azure, and there is one main script which uses Start-Job to run concurrent sub-jobs. Most of the time this worked,…
2
votes
2 answers

How to de-duplicate code in script block?

New to Powershell. I am writing a script that watches files in a directory and reports the changes to the console. I am noticing quite a bit of code duplication in the script blocks I'm using for the FS "watchers". Here is a snippet of the script…
jmreicha
  • 3,155
  • 7
  • 32
  • 39
2
votes
4 answers

When does Powershell honour default values when using $null splat parameters?

Consider the following function: function f1{ param( $sb = {}, $s = '' ) if ($sb -isnot [scriptblock]) { 'scriptblock' } if ($s -isnot [string] ) { 'string' } } Now invoke it with a splat parameter: PS C:\>…
alx9r
  • 3,675
  • 4
  • 26
  • 55
2
votes
1 answer

Is there a way to write a Help text for run-time Modules or ScriptBlocks?

A short explanation of the scenario. I am making an object using the New-Module CmdLet. Something like this: $object = New-Module -Name 'Logger' { New-Variable -Name level -Value 200 -Option Constant Function log($msg) { Write-Host…
Tony L.
  • 7,988
  • 5
  • 24
  • 28
2
votes
2 answers

PowerShell scriptBlock wraps the script when trying to write scriptblock to file

I am trying to create a PowerShell script by another PowerShell script. I have something like: > $scriptBlock = { write-host "this is the body of the script that I want to add to another PS script" if($true){ write-host "so that I…
FrozenLand
  • 259
  • 1
  • 4
  • 14
2
votes
3 answers

Piping into a ScriptBlock

I'm trying to find some way of flexibly altering/substituting pipeline elements in PowerShell: Function Where-DirectlyReportsTo { Param ( [Parameter( ValueFromPipeline = $true, HelpMessage = "The ADUser object to…
amloessb
  • 63
  • 1
  • 9
1
vote
1 answer

Using variable in Select-Object in Powershell

I have a table which is created using Time as the Column names. However if the first row does not have data in certain Hours(column) then the column does not show the column for the rest of the table. Thus, I'm trying to create a dynamic select…
1
vote
2 answers

access parent scriptblock scope in powershell

As part of learning its basics i am implementing a ternary operator cmdlet in pws. I have it taking scriptblocks, to emulate the conditional evaluation ternary operators usually have. And in most instances it works fine. function valOf($var){ …
1 2
3
10 11