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

Need help to pass and execute Function with arguments into of a Scriptblock to execute it remotely

I am writing an application that uses a function to handle data manupilation. At this point I need to execute a scriptblock on the remote computer that uses this function and receves it as a parameter. The code I am testing accomplishes this, but it…
1
vote
1 answer

Powershell How to Modify Script to Hardcode " | export-csv c:\temp\filename.csv -notypeinformation"

I have this awesome script I use to generate a list of folders with their assigned security groups and each user in each group. When I run it, I type .\getfolderacls.ps1 -verbose | export-csv c:\temp\filename.csv -notypeinformation. That works…
1
vote
1 answer

Running multiple different ScriptBlocks with RSJobs

I have multiple different scriptblocks that I want to run with Start-RSJob from the PoshRSJob module. I have the following code: $SB1 = {Write-Output "Hello from 1"} $SB2 = {Write-Output "Hello from 2"} $SB3 = {Write-Output "Hello from 3"} $SB4 =…
sheldonzy
  • 5,505
  • 9
  • 48
  • 86
1
vote
1 answer

How to use powershell invoke-command within C#?

If I run the below command inside of Powershell, it works as expected invoke-command -computername [name] -scriptblock { ipconfig.exe > c:\ipconfig.txt } But when I try to incorporate this into a c# function, I'm getting this error {"Cannot bind…
Bbb
  • 517
  • 6
  • 27
1
vote
1 answer

How to call outside defined function in runspace scriptblock

I have a complex PowerShell function which I would like to run another threads. However, If I'm right, the function cannot be accessed in the scriptblock. I want to avoid to copy every related function next to it. Is there any way or method to call…
tomas
  • 67
  • 1
  • 9
1
vote
2 answers

Adding element to array in powershell scriptblock converts array to string

I noticed odd behaviour using arrays in scriptblocks. The following code shows the problem: $array = @("x", "y") Write-Host "$($array.GetType().Name)" Write-Host "$($array.GetType().BaseType)" $bad = { $array += "z" Write-Host…
Tobias Wollgam
  • 761
  • 2
  • 8
  • 25
1
vote
1 answer

PowerShell "=" not a recognised cmdlet error

Further to the response given here: PowerShell Enter Session find path bug, I've hit another wall in my script that I can't work around. The below script returns the error: The term '=' is not recognized as the name of a cmdlet, function, script…
Karl
  • 67
  • 1
  • 7
1
vote
1 answer

How to pass results of Get-Childitem into a Scriptblock properly?

In this example, I'm trying to pass the results of Get-ChildItem into a scriptblock, with limited success and unexpected results. C:\temp contains 3 files: A.txt B.txt C.txt What I want is to pass $files into the scriptblock here, and maintain the…
David Wall
  • 42
  • 8
1
vote
2 answers

How to control the scope of a PowerShell scriptblock

I have a function written in PowerShell: function replace([string] $name,[scriptblock] $action) { Write-Host "Replacing $name" $_ = $name $action.Invoke() } and would be used as: $name = "tick" replace…
bradgonesurfing
  • 30,949
  • 17
  • 114
  • 217
1
vote
1 answer

Odd issue with scriptblock in Powershell ISE: pasting its string representation into Notepad reveals unexpected character

I am trying to create a dynamic scriptblock, so I can use variables in the scriptblock. This is my code: $Servers = "server1", "server2" $Command = "c:\plink -t user@" + $Servers[0] + " -pw 'password'" $Command = [Scriptblock]::Create($Command)…
Fredster
  • 97
  • 1
  • 3
  • 11
1
vote
2 answers

Pass parameter to powershell encoded command

I have a script which has quite a lot of lines. I can easily paste this script in a scriptblock parameter without having to edit it (e.g. put backslashes in front of quotes in the script). I can then encode the script so it can be passed to…
user3231622
  • 341
  • 3
  • 12
1
vote
1 answer

Set Paramerters in ScriptBlock when Executing Powershell Commands with C#

I am trying to executing the following powershell command in C# Invoke-Command -Session $session -ScriptBlock { Get-MailboxPermission -Identity ${identity} -User ${user} } I tried with following C# code but couldn't set the identity and user…
Yasitha
  • 2,233
  • 4
  • 24
  • 36
1
vote
2 answers

Using variables in scriptblock and here-string

I started a script with script-block: [scriptblock]$HKCURegistrySettings = { Set-RegistryKey -Key 'HKCU\Software\Microsoft\Office\14.0\Common' -Name 'qmenable' -Value 0 -Type DWord -SID $UserProfile.SID Set-RegistryKey -Key…
François
  • 11
  • 2
1
vote
3 answers

How to pass local variable to Invoke-Command's -ScriptBlock

I am trying to execute following PowerShell script from Server-2 against Server-1 (i.e. Remote server): $DBServer = 'Server1' Invoke-Command -ComputerName $DBServer -ScriptBlock { $status = Start-Process "C:\Program…
1
vote
3 answers

Referencing variable in scriptblock

I have the following code which is parsing an XML file in PowerShell, and then iterating through the entries in the config file (which are backup jobs) and performing backups (by calling functions). if…
Brad
  • 1,979
  • 7
  • 35
  • 47