169 questions
2
votes
1
answer
92
views
powershell -Action Scriptblock doesn't care about closure
My problem is fairly simple:
I'm trying to execute a file on a timer elapsed event.
I use a closure to preserve the value of my variable.
But I think I struggle to understand what is the lifetime of ...
0
votes
0
answers
88
views
How can I create a PowerShell function to wrap multiple nested script blocks?
I'm working on a simple function to indent my log files.
The premise is extremely basic:
Set-LogIndent will add an indent to all following log commands
Set-LogOutdent will remove an indent to all ...
2
votes
0
answers
971
views
Understanding Scriptblock in PowerShell and Event ID 4104
I am running a PowerShell script with around 10,000 lines of code, and I noticed that Event ID 4104 is being generated during the execution. In each Event ID 4104 entry, I see the following message:
&...
2
votes
1
answer
85
views
Predicate script blocks and collections?
Function Invoke-Keep {
<#
.SYNOPSIS
Implement the keep operation on collections, including nested arrays.
.DESCRIPTION
Given a collection (which may contain nested arrays), ...
1
vote
1
answer
63
views
To what session state does the script block in [scriptblock]::Create().GetSteppablePipeline() get bound?
Jason Shirk wrote
A script block is bound to a SessionState immediately if you use the { ... } syntax, or upon the first invocation if the script block was created some other way, e.g. [ScriptBlock]::...
1
vote
3
answers
120
views
For a compiled Cmdlet, how can I output each object immediately from an invoked scriptblock?
Consider the following
Invoke-Command {
'a'; Write-Verbose 'a' -Verbose
'b'; Write-Verbose 'b' -Verbose
} |
. { process { Write-Verbose "downstream_$_" -Verbose } }
which outputs
...
1
vote
2
answers
142
views
How can I detect that a scriptblock is bound to the SessionState of another Runspace?
Consider the following code
foreach ($i in 1..100) {
$sb = {'bound_to_parent_runspace'}
. $sb | Out-Null
1..2 |
ForEach-Object {
Start-ThreadJob `
-...
3
votes
2
answers
136
views
Script block in replace string using powershell
I am searching for following regex pattern in a file
"[a-zA-Z0-9]+[^"]+[a-zA-Z0-9]+"
This pattern contains all text including spaces. I am trying to Replace spaces(\s) to underscore(_) ...
1
vote
2
answers
77
views
How to return a parameterized code block in PowerShell
Can a PowerShell function return a parameterized function as one would in JavaScript (see below)?
I would like to make a reusable code block which renames files in a case-sensitive way. This should ...
2
votes
2
answers
183
views
Invoke on a simple scriptblock with different behavior with PowershellDataFile / without
I am attempting to invoke a scriptblock in Powershell from a value within a PSD1 imported through Import-PowerShellDataFile.
Let's take the simplest expression (no data file) of executing a ...
2
votes
2
answers
249
views
How to pass an Object in a ScriptBlock instead of a single variable?
I am trying to get a Powershell script working where one of my ScriptBlock commands requires an Object as a parameter, rather than a single variable. Example:
Invoke-Command -Computername "...
2
votes
1
answer
327
views
Piping to Where-Object and Foreach.Object not working in module delayed loaded in $Profile
Preamble: this is not about "fixing the code", as I already fixed it.
This is about "understanding what went wrong, so to avoid similar mistakes in future"
Situation:
Powershell 7....
1
vote
1
answer
69
views
PowerShell unrolling arrays differently in function vs. script block
I have an array of hashmaps as follows, and I would like to compute the sum of the x property for each item in the list:
$Items = @(
@{x = 110; y = 108 },
@{x = 102; y = 100 },
@{x = 116; ...
0
votes
0
answers
165
views
Wait for function to exit before continuing foreach loop in PowerShell
I have a list of functions that get called in loop $listoffunctions.
Some are run in RunspacePool (multithread) while others run in the main thread. The ones that run in main thread are forms with ...
1
vote
2
answers
477
views
How can I print script block content in PowerShell
Hi guys, I want to create a function that will be checking execution of selected commands and in case of catched failure retry 3 times and display the command name with the retry message. I placed a ...
2
votes
4
answers
3k
views
PowerShell Make ForEach Loop Parallel
This is working code:
$ids = 1..9
$status = [PSCustomObject[]]::new(10)
foreach ($id in $ids)
{
$uriStr = "http://192.168." + [String]$id + ".51/status"
$uri =...
0
votes
1
answer
288
views
Can't get my PowerShell Function to export to a CSV with value in the file
I'm a noob at Powershell, and I'm working on an assignment I already tried to do as much research on my own, but I'm stomped and not getting anywhere.
So, what I'm trying to do is Create a function ...
1
vote
1
answer
522
views
Using variable in Select-Object in Powershell
I have a table which is created using Time as the Column names. However if the first row does not have data in certain Hours(column) then the column does not show the column for the rest of the table....
1
vote
2
answers
245
views
access parent scriptblock scope in powershell
As part of learning its basics i am implementing a ternary operator cmdlet in pws.
I have it taking scriptblocks, to emulate the conditional evaluation ternary operators usually have.
And in most ...
1
vote
2
answers
89
views
Remote variable
I am having an issue with passing variables to a remote powershell session and I've searched everywhere on the internet I can think of so I'm hoping someone out there can help.
I have a function ...
1
vote
0
answers
171
views
Is (,$arrr) the best way to pass a single array as an argument list to a powershell invoke-command cmdlet? [duplicate]
I want to pass an array to a PowerShell script block. This is the accepted method:
$arr = 1..4;
invoke-command -ScriptBlock { Param($li); $li | %{$_}} -ArgumentList (,$arr)
From my investigations, ...
2
votes
1
answer
165
views
Can I pipe into a switch statement stored in a variable?
I have a variable that stores a switch statement
$com = '
switch ($_)
{
1 {"It is one."}
2 {"It is two."}
3 {"It is three."}
4 {"It is four."}
}
...
1
vote
0
answers
25
views
How to execute the output of a command inside a scriptblock tag? [duplicate]
THIS IS JUST A PROOF OF CONCEPT
I have the following code I wrote that will store the description of a module in a variable and then execute it:
$p = Find-Module tpl | select -ExpandProperty '...
0
votes
1
answer
235
views
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 ...
0
votes
1
answer
401
views
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 ...
0
votes
0
answers
241
views
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 ...
0
votes
2
answers
80
views
powershell - how can I consume an error in remote script block so calling scope doesnt see it
I have a ps script on a server, it goes round calling various servers/clients on its domain using invoke-command and passing over a script to remotely run some jobs via a COM object it uses on the ...
-1
votes
1
answer
98
views
Need help gui background-job double-hop issue and script freezing
I'm having an issue with double hopping scriptblock for background job that either does nothing or freezes the script. Explenation below.
The idea for the script is to get a list of files (very large ...
3
votes
2
answers
9k
views
Rename columns in Select-Object
I have an object with spaces in their properties names. I want to Select-Object @{n='NewName';e={$_.'Old Name'}} for every NoteProperty. Since there is a lot of them, I created this function. Running ...
2
votes
1
answer
2k
views
Variable in ScriptBlock - Powershell
I have a GUI for my powershell script.
If the folder "Temp" in C: does not exist - create folder and the logging entry "Path C:\Temp does not exist - create folder".
This is an ...
0
votes
1
answer
94
views
Input gets changed inside a scriptblock
I have this script with a scriptblock:
$RoboArgs = @{
Source = '01'
Target = '02'
ExtraArgs = '/e', '/purge'
}
Write-Host @RoboArgs
Start-ThreadJob -InputObject $RoboArgs -ScriptBlock {
. ...
0
votes
1
answer
2k
views
How to pass MFA enabled Azure account credentials into PowerShell ScriptBlock?
connect-azuread | out-null
$global:token = [Microsoft.Open.Azure.AD.CommonLibrary.AzureSession]::AccessTokens
$ceo = "<ceo_username>"
Import-Csv "C:\Users\abhilash\Music\users....
1
vote
1
answer
546
views
Powershell startjob import-module call function with arguments
So I am trying to start-job from a module I wrote.
Copy-Modules.psm1
function startcopy([string] $ShowToCopy) {
if (-not($ShowToCopy)) { return "No name provided. Doing nothing." }
} ...
2
votes
2
answers
3k
views
Powershell - How to pass variable into scriptblock
I'm trying to understand and figure out how I can pass a variable into a scriptblock. In my below example script, when a new file is dropped into the monitored folder it executes the $action script ...
0
votes
1
answer
771
views
How to pass ArrayList to Invoke-Command which runs on remote machine
I had a piece of code which deletes Google Chrome cache for all user profiles on remote machine.
To achieve this I had function GetMachineUserProfiles which returns ArrayList of all user profiles on ...
2
votes
2
answers
879
views
Powershell implement Switch Statement
I would like to implement a
-parallel
Switch to one of my skripts
Non Parallel Version:
$tmpArray | ForEach-Object {
#region ... local Variables
$stepWidthLocal = $stepWidth
<#...
2
votes
1
answer
4k
views
Powershell start-job scope
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 ...
1
vote
1
answer
68
views
Why does circumventing execution-policy break my script?
I have a script which copies an ISO to C:\temp then mounts the ISO and stores the drive letter in a variable to be used to execute a file on the mounted drive. I do it like this:
$mountinfo = Mount-...
0
votes
1
answer
277
views
PowerShell - ScriptBlock to iterable type
We're playing with Inovke-RestMethod response. As a part of response we have an array:
$array = @(
{
id = "9916"
title = "title9916"
}
{
id = &...
0
votes
1
answer
390
views
Powershell ScriptBlock closure: am I missing something?
I have been struggling with this for several hours now and after reading many threads about ScriptBlocks, Closures, scopes etc I still don't see what's wrong in my code.
Let me explain: I have a main ...
3
votes
0
answers
506
views
Add-Member to add a custom method to a PowerShell object
I want to add a custom method to an existing object. My problem is I may not find out how to make it accept parameters.
In this greatly simplified example I want to add a script block to a System.IO....
0
votes
1
answer
202
views
Pipe a Windows Batch Scriptblock with existence test of variable to another command [duplicate]
I'm trying to get the following code to work that tests for existence of a variable to a pipe, which is a simplification of the real code I want to execute that contains other code inside the script ...
3
votes
4
answers
4k
views
Why does pwsh.exe in combination with Start-Process not accept a script block directly?
Why does Start-Process powershell.exe { Read-Host } work, but Start-Process pwsh.exe { Read-Host } does not?
I'm aware of the -ArgumentList switch of Start-Process, but it makes things more ...
1
vote
1
answer
96
views
External JS file function cannot see page function
I have an existing legacy aspx page loading an external JS file. I am adding functionality and have added an async function in a script block on the page. The external JS file has been modified to ...
1
vote
1
answer
1k
views
The state of the current PowerShell instance is not valid for this operation in C#
I am having below method which is being called for different PS scripts and I would like the PowerShell object to be created only once and for that I made the Powershell object as static(see below ...
1
vote
4
answers
7k
views
Using Invoke-Command with Variables in ScriptBlock
New to PowerShell and learning through writing random scripts using the help info. I've tried the following 3 ways to properly get variables into the ScriptBlock(along with way too many small ...
0
votes
0
answers
164
views
Scriptblocking A Perfectly good Powershell Script within xp_CMDShell
I have a script that works perfectly in powershell. You give it a video file and it tells you all the extended attributes.
I'm trying to execute this from SQL's XPCmdShell by surrounding it with a ...
1
vote
1
answer
289
views
Using PowerShell to verify that a specific service exists from a .txt list of server names
I am trying to verify that a specific service exists from a .txt list of server names and then output to a file. In this case I need to also add credentials so I need to use the Invoke-Command. What ...
0
votes
1
answer
226
views
Recreate PowerShell Script Block Using Scope
I have the following cmdlet to invoke an arbitrary scriptblock (that usually calls an exe) and handles the return code. The goal here is to print a command line, run it, and then throw an error if it ...
0
votes
1
answer
480
views
Incrementing inside foreach and scriptblock in Powershell
I hope someone can spot the error:
This is a small function inside a bigger script.
I want it to increment $i every time it iterate the foreach, but I can't get it to work. If I run only the line ...