scriptblock Questions
2
Solved
I would like to implement a
-parallel
Switch to one of my skripts
Non Parallel Version:
$tmpArray | ForEach-Object {
#region ... local Variables
$stepWidthLocal = $stepWidth
<#
my code
#>
...
Parotic asked 30/8, 2021 at 13:9
1
Solved
I have a long script. i have a function for logging:
function Log ([string]$Content){
$Date = Get-Date
Add-Content -Path $LogPath -Value ("$Date : $Content")
}
In some point at the scr...
Billiton asked 22/8, 2021 at 12:15
8
Solved
I guess you can't just do this:
$servicePath = $args[0]
if(Test-Path -path $servicePath) <-- does not throw in here
$block = {
write-host $servicePath -foreground "magenta"
if((Test-P...
Shopper asked 2/5, 2013 at 20:33
2
Solved
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"...
Stillwell asked 28/10, 2013 at 7:16
3
Solved
I'm looking at the documentation of PowerShell's Rename-Item cmdlet and there is an example like this.
Get-ChildItem *.txt | Rename-Item -NewName { $_.name -Replace '\.txt','.log' }
This example ...
Cluck asked 14/10, 2018 at 18:28
2
Solved
Consider the following arbitrary function and test cases:
Function Foo-MyBar {
Param(
[Parameter(Mandatory=$false)]
[ScriptBlock] $Filter
)
if (!$Filter) {
$Filter = { $true }
}
#$Filt...
Afforest asked 16/8, 2018 at 19:49
2
Solved
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 ...
Wallachia asked 2/7, 2018 at 13:34
3
Solved
$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...
Coyotillo asked 12/2, 2017 at 3:28
1
Solved
I'm having trouble understanding scopes within ScriptBlocks. I was counting on some kind of closure-like system, but I can't seem to get it working.
I have a ScriptBlock that takes a param and ret...
Turbid asked 3/2, 2017 at 10:34
4
Solved
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 { "Fre...
Correspondence asked 12/4, 2013 at 17:12
1
Solved
I have a class I'm attempting to use from Powershell. This class has a method, called Execute(), with two overloads: one which takes a Func<T> and one which takes an Action<T>. I can ca...
Hypopituitarism asked 10/5, 2016 at 18:49
5
Solved
Is there a way to use the Powershell Start-Process cmdlet to start a new Powershell session and pass a scriptblock with local variables (once of which will be an array)?
Example:
$Array = @(1,2,3...
Fonzie asked 21/7, 2013 at 20:9
2
Solved
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:\...
Indite asked 28/8, 2014 at 14:53
1
Solved
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 ...
Hardecanute asked 20/3, 2014 at 20:57
4
Solved
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 ...
Bakery asked 7/11, 2013 at 11:22
1
© 2022 - 2024 — McMap. All rights reserved.