scriptblock

Powershell: Use a variable to reference a property of $_ in a script block

与世无争的帅哥 提交于 2019-12-21 02:46:18
问题 $var =@( @{id="1"; name="abc"; age="1"; }, @{id="2"; name="def"; age="2"; } ); $properties = @("ID","Name","Age") ; $format = @(); foreach ($p in $properties) { $format += @{label=$p ; Expression = {$_.$p}} #$_.$p is not working! } $var |% { [PSCustomObject]$_ } | ft $format In the above example, I want to access each object's property through a variable name. But it cannot work as expected. So in my case, how to make Expression = {$_.$p} working? 回答1: The OP's code and this answer use PSv3+

command execution ordering inside a PowerShell scriptblock

冷暖自知 提交于 2019-12-19 10:19:56
问题 I got excited with PowerShell ScriptBlock at first but I was confused recently with its executing ordering inside blocks. For example: $test_block = { write-host "show 1" ps write-host "show 2" Get-Date } The output by calling $test_block.Invoke(): show 1 show 2 <result of command 'ps'> <result of command 'get-date'> Do commands who output something run first? 回答1: This behaviour is because write-host doesn't put the output on the pipeline. The other commands are placed on the pipeline so are

Powershell - how to pre-evaluate variables in a scriptblock for Start-Job

萝らか妹 提交于 2019-12-18 14:14:39
问题 I want to use background jobs in Powershell. How to make variables evaluated at the moment of ScriptBlock definition? $v1 = "123" $v2 = "asdf" $sb = { Write-Host "Values are: $v1, $v2" } $job = Start-Job -ScriptBlock $sb $job | Wait-Job | Receive-Job $job | Remove-Job I get printed empty values of $v1 and $v2. How can I have them evaluated in (passed to) the scriptblock and so to the background job? 回答1: One way is to use the [scriptblock]::create method to create the script block from an

++ Operator on Variable Is Not Changing As Expected In ScriptBlock

99封情书 提交于 2019-12-12 08:58:13
问题 I am trying to rename files by putting a prefix based on an incrementing counter in the files such as: $directory = 'C:\Temp' [int] $count=71; gci $directory | sort -Property LastWriteTime | ` rename-item -newname {"{0}_{1}" -f $count++, $_.Name} -whatif Yet all the files processed are 71_ and $count in $count++ never increments and the filenames are prefixed the same? Why? 回答1: The reason you cannot just use $count++ in your script block in order to increment the sequence number directly is:

Set Paramerters in ScriptBlock when Executing Powershell Commands with C#

点点圈 提交于 2019-12-12 03:04:32
问题 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 parameters. var command = new PSCommand(); command.AddCommand("Invoke-Command"); command.AddParameter("ScriptBlock", ScriptBlock.Create("Get-MailboxPermission -Identity ${identity} -User ${user}")); command.AddParameter("identity", mailbox); command

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

浪子不回头ぞ 提交于 2019-12-11 08:29:17
问题 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 described as Powershell's equivalent of anonymous functions. Idiomatic powershell, however, makes use of powershell's pipeline parameter bindings. But neither delegates nor scriptblocks seem to make use of Powershell's pipeline parameter bindings. Is there a

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

廉价感情. 提交于 2019-12-11 06:59:11
问题 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 entire list of three files that Get-ChildItem has returned. In other words, I would like for $files to return A.txt, B.txt, and C.txt, both inside and outside the scriptblock. I am able to do this, but I'm not able to do it reliably, and cannot

PowerShell: Start-Job -scriptblock Multi line scriptblocks?

不羁的心 提交于 2019-12-10 21:03:37
问题 Hoping someone can help me to figure out if this is possible. I have a 20 ish lines that scan a log file in a while loop. I need this to happen in parallel with the rest of the script. The log scanning loop is passing the entries into SQLite and other scripts need to act on this information - hence wanting to run them in parallel. If i use the Job-Start command then it seems the -SciptBlock function will only accept one piped line of commands. I have too many commands to want to pipe so i

Undocumented changes to Powershell Scope handling v2/v3?

▼魔方 西西 提交于 2019-12-07 12:44:32
问题 Background : I've been writing a powershell script to migrate files from a Sharpoint 2010 instance on Windows Server '08 (with Powershell 2.x) to a Sharepoint 2013 instance on Windows Server '12 (with Powershell 3.x). I have that working but I noticed a change in how scope is handled. Issue : I have the following code that gets run on both PSSessions ( $param is a hashtable of parameter values) Invoke-Command -session $Session -argumentlist $params -scriptblock ` { Param ($in) $params = $in #

PowerShell ScriptBlock and multiple functions

末鹿安然 提交于 2019-12-06 12:04:29
I have written the following code: cls function GetFoo() { function GetBar() { $bar = "bar" $bar } $foo = "foo" $bar = GetBar $foo $bar } $cred = Get-Credential "firmwide\srabhi_adm" $result = Invoke-Command -Credential $cred -ComputerName localhost -ScriptBlock ${function:GetFoo} Write-Host $result[0] Write-Host $result[1] It works but I don't want to define GetBar inside of GetFoo . Can I do something like this? cls function GetBar() { $bar = "bar" $bar } function GetFoo() { $foo = "foo" $bar = GetBar $foo $bar } $cred = Get-Credential "firmwide\srabhi_adm" $result = Invoke-Command