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
0
votes
2 answers

powershell - how can I consume an error in remote script block so calling scope doesnt see it

I have a ps script on a server, it goes round calling various servers/clients on its domain using invoke-command and passing over a script to remotely run some jobs via a COM object it uses on the called computer. On one of the servers, because the…
Keith
  • 251
  • 1
  • 4
  • 13
0
votes
1 answer

Input gets changed inside a scriptblock

I have this script with a scriptblock: $RoboArgs = @{ Source = '01' Target = '02' ExtraArgs = '/e', '/purge' } Write-Host @RoboArgs Start-ThreadJob -InputObject $RoboArgs -ScriptBlock { . robocopy_invoke.ps1 Write-Host @input } |…
gargoylebident
  • 373
  • 1
  • 2
  • 12
0
votes
1 answer

How to pass MFA enabled Azure account credentials into PowerShell ScriptBlock?

connect-azuread | out-null $global:token = [Microsoft.Open.Azure.AD.CommonLibrary.AzureSession]::AccessTokens $ceo = "" Import-Csv "C:\Users\abhilash\Music\users.txt" | ForEach-Object { $scriptblock =…
0
votes
1 answer

How to pass ArrayList to Invoke-Command which runs on remote machine

I had a piece of code which deletes Google Chrome cache for all user profiles on remote machine. To achieve this I had function GetMachineUserProfiles which returns ArrayList of all user profiles on remote machine. In other function I need to run…
punky
  • 125
  • 2
  • 12
0
votes
1 answer

PowerShell - ScriptBlock to iterable type

We're playing with Inovke-RestMethod response. As a part of response we have an array: $array = @( { id = "9916" title = "title9916" } { id = "9917" title = "title9917" } ) We've noticed that every…
user3719188
  • 590
  • 5
  • 7
0
votes
1 answer

Powershell ScriptBlock closure: am I missing something?

I have been struggling with this for several hours now and after reading many threads about ScriptBlocks, Closures, scopes etc I still don't see what's wrong in my code. Let me explain: I have a main script that dynamically generates an HTML page…
0
votes
1 answer

Pipe a Windows Batch Scriptblock with existence test of variable to another command

I'm trying to get the following code to work that tests for existence of a variable to a pipe, which is a simplification of the real code I want to execute that contains other code inside the script block, but this demonstrates the problem: ( if…
Russ Thils
  • 130
  • 1
  • 9
0
votes
0 answers

Scriptblocking A Perfectly good Powershell Script within xp_CMDShell

I have a script that works perfectly in powershell. You give it a video file and it tells you all the extended attributes. I'm trying to execute this from SQL's XPCmdShell by surrounding it with a script block like "&{my code goes here}" but that…
jimerb
  • 67
  • 9
0
votes
1 answer

Recreate PowerShell Script Block Using Scope

I have the following cmdlet to invoke an arbitrary scriptblock (that usually calls an exe) and handles the return code. The goal here is to print a command line, run it, and then throw an error if it fails.... function Invoke-ScriptBlock { …
Jeff
  • 35,755
  • 15
  • 108
  • 220
0
votes
1 answer

Incrementing inside foreach and scriptblock in Powershell

I hope someone can spot the error: This is a small function inside a bigger script. I want it to increment $i every time it iterate the foreach, but I can't get it to work. If I run only the line where $i++ is present, it will increment. The message…
0
votes
1 answer

Create scriptblocks with expanded variables

I know that this question has been asked before here and here. But for one reason or another it doesn't really work for us. Desired result 2 scriptblocks that are usable with the Pester parameter ParameterFilter whre only the variable $testTable is…
DarkLite1
  • 13,637
  • 40
  • 117
  • 214
0
votes
0 answers

How to write a powershell function with a scriptblock that allows automatic pipeline variable $_

I want to write a cmdlet that invokes a call back method. When invoking the callback method, I want to be able to use the automatic variable $_. This is so that I don't have to For example, let's say my function is an implementation similar to…
Nandun
  • 1,802
  • 2
  • 20
  • 35
0
votes
1 answer

How its possible, to run scriptblock with different credentials and runas?

My script should load credentials an run code with elevation. I try to include one scriptblock to another. And run Start-Process with different parameter sets. I have error "This command cannot be run due to the error: The stub received bad…
0
votes
1 answer

how to dealing with quotes in powershell scriptblock

this code works as expected: $var = "a123" $script = @' Add-Content C:\temp\test.txt -Value {{"{0} name1"}} Add-Content C:\temp\test.txt -Value {{"{0} name2"}} '@ -f $var $command = [scriptblock]::Create($script) Start-Process powershell.exe…
0
votes
1 answer

If statement within FileSystemWatcher Not working in Powershell

I am trying to implement a FileSystemWatcher that contains an if statement that evaluates and argument passed to it, but it doesn't seem to be evaluating. Am I making a mistake? Here is the code: Function Auto-Watcher { param ($folder, $filter,…
Andy
  • 207
  • 4
  • 18