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
1
vote
2 answers

Remote variable

I am having an issue with passing variables to a remote powershell session and I've searched everywhere on the internet I can think of so I'm hoping someone out there can help. I have a function calling a job which is telling it to start a…
Todd
  • 25
  • 4
1
vote
0 answers

Is (,$arrr) the best way to pass a single array as an argument list to a powershell invoke-command cmdlet?

I want to pass an array to a PowerShell script block. This is the accepted method: $arr = 1..4; invoke-command -ScriptBlock { Param($li); $li | %{$_}} -ArgumentList (,$arr) From my investigations, these alternatives work too: invoke-command…
Nigel Davies
  • 1,640
  • 1
  • 13
  • 26
1
vote
0 answers

How to execute the output of a command inside a scriptblock tag?

THIS IS JUST A PROOF OF CONCEPT I have the following code I wrote that will store the description of a module in a variable and then execute it: $p = Find-Module tpl | select -ExpandProperty 'Description';& ([scriptblock]::Create($p)) the…
I am Jakoby
  • 577
  • 4
  • 19
1
vote
1 answer

Powershell startjob import-module call function with arguments

So I am trying to start-job from a module I wrote. Copy-Modules.psm1 function startcopy([string] $ShowToCopy) { if (-not($ShowToCopy)) { return "No name provided. Doing nothing." } } else { return "Name Provided $ShowToCopy" } } in the main…
1
vote
2 answers

Powershell - How to pass variable into scriptblock

I'm trying to understand and figure out how I can pass a variable into a scriptblock. In my below example script, when a new file is dropped into the monitored folder it executes the $action script block. But the $test1 variable just shows up blank.…
xeric080
  • 99
  • 8
1
vote
1 answer

Powershell start-job scope

I have a long script. i have a function for logging: function Log ([string]$Content){ $Date = Get-Date Add-Content -Path $LogPath -Value ("$Date : $Content") } In some point at the script i have the need to run jobs in parallel. I have a…
Shahar
  • 461
  • 5
  • 16
1
vote
1 answer

Why does circumventing execution-policy break my script?

I have a script which copies an ISO to C:\temp then mounts the ISO and stores the drive letter in a variable to be used to execute a file on the mounted drive. I do it like this: $mountinfo = Mount-DiskImage -ImagePath $ctempiso $driveletter =…
1
vote
1 answer

External JS file function cannot see page function

I have an existing legacy aspx page loading an external JS file. I am adding functionality and have added an async function in a script block on the page. The external JS file has been modified to call the async function. No matter where on the page…
McTech
  • 13
  • 3
1
vote
1 answer

The state of the current PowerShell instance is not valid for this operation in C#

I am having below method which is being called for different PS scripts and I would like the PowerShell object to be created only once and for that I made the Powershell object as static(see below code). but then it gives me error The state of the…
ZZZSharePoint
  • 1,163
  • 1
  • 19
  • 54
1
vote
4 answers

Using Invoke-Command with Variables in ScriptBlock

New to PowerShell and learning through writing random scripts using the help info. I've tried the following 3 ways to properly get variables into the ScriptBlock(along with way too many small variations to list) with listed error message wrapped in…
1
vote
1 answer

Using PowerShell to verify that a specific service exists from a .txt list of server names

I am trying to verify that a specific service exists from a .txt list of server names and then output to a file. In this case I need to also add credentials so I need to use the Invoke-Command. What am I doing wrong here? clear start-transcript…
1
vote
2 answers

Invoking Restmethod URI in Powershell Scriptblock not working

There seems to be an issue with the Scriptblock which is causing it to not process and I think it maybe a simple syntax issue: function start-wait4 { $scroll = "/-\|/-\|" $idx = 0 $job = Invoke-Command -ComputerName…
1
vote
1 answer

Why do I get different output in Powershell from start-job vs just running the code?

The script runs correctly when outside of Start-Job but when in a scriptblock I get incorrect results. Where am I going wrong? I need the Start-Job functionality since I have servers where the remote commands will hang (separate problem - WMI is…
Luke Fowler
  • 83
  • 2
  • 8
1
vote
1 answer

Variable = Executed script block with try/catch

I am trying to look for the presence of a specific registry property and if present set a variable to the value. I will be doing an If/Then/ElseIf sequence, so I don't want to wrap the hole thing in a Try/Catch to address the fact that in PS5.1…
Gordon
  • 6,257
  • 6
  • 36
  • 89
1
vote
0 answers

Why can't I use a script block with set-content's value parameter? (delayed binding)

Hmm, why doesn't this work? Usually if a parameter takes pipeline input, I can use a script block with it. PS C:\users\js> (Get-Content -Path file) | Set-Content -Path file -Value { $_ -Replace 1,2 } Set-Content : The input object cannot be bound…
js2010
  • 23,033
  • 6
  • 64
  • 66