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

PowerShell using Hashtable and ScriptBlock as Parameters from C#

I'm trying to do some remote PowerShell commands to an Exchange-Server. Now I'm having problems executing ScriptBlocks in Parameters. My C# Code looks like: List parameters = new List() { …
0
votes
2 answers

How to pass a script block as parameter and execute multiple times?

I wish to pass "Write-Host mm" as script block to function "f", and I hope "f" will execute it 10 times, so I tried: function f([ScriptBlock]$s) { 1..10|$s } f(Write-Host mm) Unfortunately, powershell gives error: At…
vik santata
  • 2,989
  • 8
  • 30
  • 52
0
votes
0 answers

timeout and passing variables to argumentlist when starting job

I'm using the following script to try and ping a list of computers stored in a CSV. The desired outcome is that I'll be able to verify the timeout condition. $servers = ipcsv "\\testbox\c$\Tools\PAL Stuff\servers.csv" $timeoutSeconds = 3 $code = { …
0
votes
1 answer

Powershell call batch within scriptblock

I have the script below which is not working the way I want. Initially, I would like to pass install.cmd to function which will use the "Start-Job" in the background so that it doesn't freeze up the main Powershell window. But I can't get it to call…
Besiktas
  • 331
  • 1
  • 6
  • 23
0
votes
0 answers

Scriptblock with IF statement as parameter of function

I have this script file contains: function run_command { param ( [string] $scriptblock ) $sb = [Scriptblock]::Create($scriptblock) Start-Job -ScriptBlock $sb | Wait-Job | Receive-Job } $a = "aaa" $scriptblock =…
0
votes
1 answer

Get Average CPU utilization for test execution in powershell

Context: I want to run a test, and while it is running to gather CPU utilization. Basically, when I start the test I also want to start collecting data for CPU utilization, at the end of the test I also want to stop collecting data for CPU…
Mircea
  • 178
  • 1
  • 1
  • 6
0
votes
1 answer

Enter-PSSession with -Credential still prompts for credentials when inside a ScriptBlock

I have a script that automatically logs a user into a remote PowerShell session using Enter-PSSession. I create the -Credential with New-Object System.Management.Automation.PSCredential. This works great if it is not inside a ScriptBlock. When I put…
0
votes
1 answer

Start-Job - check $lastexicode of robocopy inside a Scriptblock

I'm trying to do something simple. I want to create a robocopy job that would launch the copy and check the $lastexitcode. $Source = "c:\SourceFolder" $Destination "\\someserver\Destinationfolder" $ScriptBlock = [scriptblock]::Create("robocopy…
xashcorex
  • 134
  • 1
  • 3
  • 11
0
votes
1 answer

PowerShell - Trying to use a variable in ScriptBlock with quoted string?

I have the following PowerShell script Job that uses a ScriptBlock. I am trying to use the variable $pw1 but it does not seem to be using it correctly. How can I use variables in a ScriptBlock or a string? Start-Job -ScriptBlock {Start-Process…
0
votes
1 answer

Advice about javascript configuration for objects/widgets and where to add this

I have a lot of javascript configuration that I need to output (configuration objects for my own needs, jquery dialog setup and so on). They are for specific fields in forms. I was using 1 script block for this that is always added, but now have…
RISC OS
  • 149
  • 11
0
votes
1 answer

What is the value of a powershell scriptblock?

I am trying to run a piece of remote code using powershell remoting and getting some strange behavior which I am unable to explain. This is the sequence of commands I run. $sb1 = {$r1 = 1; $r2 = 2; $r3 = Get-Culture; return $r3} $sb2 = {1; 2; $r3 =…
spervez
  • 33
  • 4
0
votes
1 answer

Include a PowerShell file inside a ScriptBlock

I have written the following code $sb = { . .\Myfunctions.ps1 $x = MyFunction1 $y = MyFunction2 $x + $y } $cred = Get-Credential "domain\user" Invoke-Command -Computer localhost -Credentials $cred -ScriptBlock $sb This does not…
Knows Not Much
  • 30,395
  • 60
  • 197
  • 373
-1
votes
1 answer

Need help gui background-job double-hop issue and script freezing

I'm having an issue with double hopping scriptblock for background job that either does nothing or freezes the script. Explenation below. The idea for the script is to get a list of files (very large count, ~200k) and then process them in three…
Nawad-sama
  • 151
  • 2
  • 12
-1
votes
1 answer

Powershell remoting Windows -> Linux : submit a parametrized script

I guess I have it 90% correct, but a small part seems to be missing... I have no problem opening a remote session from Windows -> Linux (host) No problem passing a command either. This works fine: $Session = New-PSSession -SSHTransport -HostName…
Philippe
  • 103
  • 1
  • 1
  • 8
-1
votes
1 answer

powershell scriptblock passing arguments (looked into "duplicate" solutions - no answer)

This should be a quite obvious question... and I have looked into the proposed "duplicate" answers... No way... Only my "hardcoded solution works :-( I just would like to have one parameter (the path) in a Start-Job - ScriptBlock {} command. Here is…
Philippe
  • 103
  • 1
  • 1
  • 8
1 2 3
10
11