scriptblock

How to pass a named function as a parameter (scriptblock)

℡╲_俬逩灬. 提交于 2019-12-06 02:00:29
Let's take the classic first-order functions example: function Get-MyName { "George" } function Say-Hi([scriptblock]$to) { Write-Host ("Hi "+(& $to)) } This works just fine: Say-Hi { "Fred Flintstone" } this does not: Say-Hi Get-MyName because Get-MyName is evaluated, not passed as a value itself. How do I pass Get-MyName as a value? You have to pass Get-Myname as a scriptblock, because that's how you've defined the variable type. Say-Hi ${function:Get-MyName} If you are ready to sacrifice the [scriptblock] parameter type declaration then there is one more way, arguably the simplest to use and

Undocumented changes to Powershell Scope handling v2/v3?

拈花ヽ惹草 提交于 2019-12-05 22:53: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 # store parameters in remote session # need to run with elevated privileges to access sharepoint farm #

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

纵饮孤独 提交于 2019-12-04 11:49:20
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? The reason you cannot just use $count++ in your script block in order to increment the sequence number directly is: Delay-bind script blocks - such as the one you passed to Rename-Item -NewName - and script blocks in

PowerShell Executing a function within a Script Block using Start-Process does weird things with double quotes

时间秒杀一切 提交于 2019-12-04 06:29:35
I have a PowerShell script that edits the registry, so it needs to run as admin. To do this, I start up a new PowerShell process from my running PowerShell script, and pass in part of the registry key path using a Script Block with a function in it. When I use double quotes within that function, PowerShell tries to interpret them as commands, rather than a string. If I use single quotes though, then everything works fine. I've created a little stripped down sample powershell script that reproduces the problem. Here's the snippet: $ScriptBlock = { function Test { $status = "This is a string"

Why doesn't $PSItem behave as expected when using a bracket-based -Filter argument?

扶醉桌前 提交于 2019-12-03 15:57:03
I was assisting a user with this question, linked to my answer here: Powershell script to add users to A/D group from .csv using email address only? Initially I wrote the script as follows, using a bracket-based filter for Get-AdUser like follows: Import-CSV "C:\users\Balbahagw\desktop\test1.csv" | Foreach-Object { # Here, $_.EmailAddress refused to resolve $aduser = Get-ADUser -Filter { EmailAddress -eq $_.EmailAddress } if( $aduser ) { Write-Output "Adding user $($aduser.SamAccountName) to groupname" Add-ADGroupMember -Identity groupname -Members $aduser } else { Write-Warning "Could not

Powershell expand variable in scriptblock

只愿长相守 提交于 2019-12-01 11:31:06
I am trying to follow this article to expand a variable in a scriptblock My code tries this: $exe = "setup.exe" invoke-command -ComputerName $j -Credential $credentials -ScriptBlock {cmd /c 'C:\share\[scriptblock]::Create($exe)'} How to fix the error: The filename, directory name, or volume label syntax is incorrect. + CategoryInfo : NotSpecified: (The filename, d...x is incorrect.:String) [], RemoteException + FullyQualifiedErrorId : NativeCommandError + PSComputerName : remote_computer To follow the article, you want to make sure to leverage PowerShell's ability to expand variables in a

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

喜欢而已 提交于 2019-11-30 17:24:12
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? One way is to use the [scriptblock]::create method to create the script block from an expanadable string using local variables: $v1 = "123" $v2 = "asdf" $sb = [scriptblock]::Create("Write-Host

Why doesn't $PSItem behave as expected when using a bracket-based -Filter argument?

妖精的绣舞 提交于 2019-11-30 07:39:11
问题 I was assisting a user with this question, linked to my answer here: Powershell script to add users to A/D group from .csv using email address only? Initially I wrote the script as follows, using a bracket-based filter for Get-AdUser like follows: Import-CSV "C:\users\Balbahagw\desktop\test1.csv" | Foreach-Object { # Here, $_.EmailAddress refused to resolve $aduser = Get-ADUser -Filter { EmailAddress -eq $_.EmailAddress } if( $aduser ) { Write-Output "Adding user $($aduser.SamAccountName) to

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

孤街浪徒 提交于 2019-11-29 12:34:50
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 Files\iQ4bis\HaloSource\HaloSource.Run.exe" '"D:\Test\UsageTracking.iqp" /wf "Default Workflow" /e "Dev" ' -Wait -PassThru $test2 = $status.ExitCode if ($test2 -ne 0) { Throw "The command exited with error code: $test2" } else { Write-host "Workflow executed successfully." } } Question: Code part "C:\Program Files\iQ4bis\HaloSource\HaloSource.Run.exe" '"D:\Test\UsageTracking.iqp"

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

断了今生、忘了曾经 提交于 2019-11-28 06:06:25
问题 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 Files\iQ4bis\HaloSource\HaloSource.Run.exe" '"D:\Test\UsageTracking.iqp" /wf "Default Workflow" /e "Dev" ' -Wait -PassThru $test2 = $status.ExitCode if ($test2 -ne 0) { Throw "The command exited with error code: $test2" } else { Write-host "Workflow executed successfully." } }