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

Executing multiple commands in a scriptblock and returning multiple responses

I'd like to connect to a remote host, run 2 commands and return the seperate responses. However I'd like to do this as part of one scriptblock. I've done this before with one command but no joy with two. For example having gc…
user1054637
  • 695
  • 11
  • 28
1
vote
3 answers

Powershell passing arguments in ScriptBlock

I'm trying to get the last write time on a file from a remote server. This doesn not work: $server = "MyServerName" $lastWrite = Invoke-Command -Computername $server -ScriptBlock {Get-ChildItem "\\$args[0]\hot.war" } -argumentlist $server | select…
Matt616
  • 33
  • 1
  • 5
1
vote
1 answer

Is there a way to selectively capture variables in a scriptblock's context inside modules?

Suppose you have defined two functions in a module (i.e. a .psm1 file): function f1{ param($x1) $a1 = 10 f2 $x1 } function f2{ param($x2) $a2 = 100 & $x2 } Now suppose you run the following: PS C:\> $a0 = 1 PS C:\> $x0 =…
alx9r
  • 3,675
  • 4
  • 26
  • 55
1
vote
1 answer

Is there a way to create a Cmdlet "delegate" that supports pipeline parameter binding?

In .NET if you have a subroutine whose implementation might change from one call to another, you can pass a delegate to the method that uses the subroutine. You can also do this in Powershell. You can also use scriptblocks which have been…
alx9r
  • 3,675
  • 4
  • 26
  • 55
1
vote
2 answers

powershell Loop on scriptblocks

I'm trying to export several information from a collection of objects into CSV files. For this purpose I always have the following pipe : $users | < my filters/grouping/selects and expands> | Export-CSV ... Instead of copy/paste these lines I…
Jeanb
  • 23
  • 6
1
vote
1 answer

Remote Microsoft SQL Server Database Restore with Powershell and Script Block

Long time lurker, first time poster. I'm hoping somebody here can give me a hand trying to figure out an issue that's got me stumped. Essentially I am trying to write a Powershell script that will restore a multi-part SQL Server database to a remote…
1
vote
1 answer

Sharing variables between jobs

I have a powershell script which is supposed to run a background job until the user decides it should end. The background job is this Script Block: $block = { param( [Parameter(Mandatory = $true, Position = 0,…
Orphid
  • 2,722
  • 2
  • 27
  • 41
1
vote
1 answer

How to incorporate ScriptBlocks into a hosted powershell environment

I've recently started to explore powershell scripting as well as powershell hosting. From a powershell command prompt the following line executes as I would expect. "fee" | &{ process { $_; $args } } "fi" "fo" "fum" This script outputs the "fee"…
smarcaurele
  • 425
  • 3
  • 9
1
vote
2 answers

How to refer to entire pipeline object without a script block

With the advent of PowerShell V3 instead of having to write: Get-Process | Where { $_.ProcessName -match "win" } ...one could now write the more terse: Get-Process | Where ProcessName -match "win" ... a clear win (ahem) for shell use. Now let's…
Michael Sorens
  • 35,361
  • 26
  • 116
  • 172
1
vote
1 answer

Powershell start-job scriptblock not executing

I have some pretty simply code which aims to convert each file in a directory into HTML. My problem is that although a job is successfully created for each file, the scriptblock does not run, ever. $convert = { Param( …
user3445141
  • 11
  • 1
  • 2
1
vote
1 answer

Powershell Start-Job not executing scriptblock

The title says it all i have a Start-Job with a script block that instead of executing the commands it output the info about the job. The order in which this gets executed is the following $location = $(Get-Location).toString() $oldModulePath =…
user1752736
  • 105
  • 1
  • 3
  • 10
0
votes
1 answer

Dynamically calculated value for scriptproperty

The script below is a part of a bigger project of converting msgs to pdfs. What I'm having problem with implementing is the attachments custom property. I'd like is for it to take custom value based on calculated value based on msg attachments. The…
0
votes
1 answer

Invoke-Command -ScriptBlock bringing variable with LIST

I am trying to execute a script remotely to get the list of service with exclusion of certain service currently in stopped state. I am not able to pass the exclusion variable as list. If i just use it locally it works but within remote invoke…
0
votes
0 answers

Using Select-Object from Scriptblock

Im quite new to PowerShell, I have 4 piped scripts, each with their own select statements, then i have a select-object after the scriptblock that selects all columns previously selected from the different statements in the scriptblock, I then use…
0
votes
0 answers

Problems with Invoke-Command -ScriptBlock in PowerShell

Hey Guys im new to StackOverflow and I'm here to ask a Question about my Powershell Script for the Automation of a Script on my mgmt Server to all XENAPP Servers im hosting. I want to execute with one click the Citrix Optimizer to every XENAPP. This…
cosmo_
  • 11
  • 5