Skip to main content
Filter by
Sorted by
Tagged with
4 votes
2 answers
187 views

Consider this example, a script named test.ps1 function MyTestFunction { [CmdletBinding()] param () try { [bool]$functionBool= $null } catch {throw $_} Get-...
Paul π's user avatar
  • 585
2 votes
1 answer
100 views

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 ...
Rouge a's user avatar
  • 25
0 votes
0 answers
95 views

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 ...
Martin Hatch's user avatar
2 votes
0 answers
1k views

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: &...
Madathi Mayil's user avatar
2 votes
1 answer
99 views

Function Invoke-Keep { <# .SYNOPSIS Implement the keep operation on collections, including nested arrays. .DESCRIPTION Given a collection (which may contain nested arrays), ...
Kangles's user avatar
  • 51
1 vote
1 answer
70 views

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]::...
alx9r's user avatar
  • 4,355
2 votes
3 answers
143 views

Consider the following Invoke-Command { 'a'; Write-Verbose 'a' -Verbose 'b'; Write-Verbose 'b' -Verbose } | . { process { Write-Verbose "downstream_$_" -Verbose } } which outputs ...
alx9r's user avatar
  • 4,355
1 vote
2 answers
151 views

Consider the following code foreach ($i in 1..100) { $sb = {'bound_to_parent_runspace'} . $sb | Out-Null 1..2 | ForEach-Object { Start-ThreadJob ` -...
alx9r's user avatar
  • 4,355
3 votes
2 answers
147 views

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(_) ...
MMA's user avatar
  • 458
1 vote
2 answers
78 views

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 ...
Marc's user avatar
  • 15k
2 votes
2 answers
193 views

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 ...
Sage Pourpre's user avatar
  • 10.4k
2 votes
2 answers
262 views

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 "...
Dan Murray's user avatar
2 votes
1 answer
341 views

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....
sirtao's user avatar
  • 3,031
1 vote
1 answer
73 views

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; ...
Max's user avatar
  • 887
0 votes
0 answers
168 views

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 ...
davidprogrammercdn's user avatar
1 vote
2 answers
492 views

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 ...
Maximus Gebo's user avatar
2 votes
4 answers
3k views

This is working code: $ids = 1..9 $status = [PSCustomObject[]]::new(10) foreach ($id in $ids) { $uriStr = "http://192.168." + [String]$id + ".51/status" $uri =...
Net Dawg's user avatar
  • 611
0 votes
1 answer
296 views

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 ...
Jeremy Frank's user avatar
1 vote
1 answer
534 views

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....
Greg's user avatar
  • 13
1 vote
2 answers
252 views

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 ...
Andrea Bardelli's user avatar
1 vote
2 answers
89 views

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 ...
Todd's user avatar
  • 25
1 vote
0 answers
176 views

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, ...
Nigel Davies's user avatar
  • 1,710
2 votes
1 answer
170 views

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."} } ...
I am Jakoby's user avatar
1 vote
0 answers
25 views

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 '...
I am Jakoby's user avatar
0 votes
1 answer
237 views

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 ...
Nawad-sama's user avatar
0 votes
1 answer
401 views

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 ...
Anirban Banerjee's user avatar
0 votes
0 answers
248 views

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 ...
cosmo_'s user avatar
  • 11
0 votes
2 answers
88 views

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 ...
Keith's user avatar
  • 281
-1 votes
1 answer
99 views

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 ...
Nawad-sama's user avatar
3 votes
2 answers
9k views

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 ...
PollusB's user avatar
  • 1,876
2 votes
1 answer
2k views

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 ...
user18209625's user avatar
0 votes
1 answer
98 views

I have this script with a scriptblock: $RoboArgs = @{ Source = '01' Target = '02' ExtraArgs = '/e', '/purge' } Write-Host @RoboArgs Start-ThreadJob -InputObject $RoboArgs -ScriptBlock { . ...
gargoylebident's user avatar
0 votes
1 answer
2k views

connect-azuread | out-null $global:token = [Microsoft.Open.Azure.AD.CommonLibrary.AzureSession]::AccessTokens $ceo = "<ceo_username>" Import-Csv "C:\Users\abhilash\Music\users....
abhilash m's user avatar
1 vote
1 answer
552 views

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." } } ...
Lin sain's user avatar
2 votes
2 answers
3k views

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 ...
xeric080's user avatar
  • 146
0 votes
1 answer
772 views

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 ...
punky's user avatar
  • 163
2 votes
2 answers
886 views

I would like to implement a -parallel Switch to one of my skripts Non Parallel Version: $tmpArray | ForEach-Object { #region ... local Variables $stepWidthLocal = $stepWidth <#...
Farbkreis's user avatar
  • 644
2 votes
1 answer
4k views

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 ...
Shahar's user avatar
  • 511
1 vote
1 answer
69 views

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-...
displayed_name's user avatar
0 votes
1 answer
283 views

We're playing with Inovke-RestMethod response. As a part of response we have an array: $array = @( { id = "9916" title = "title9916" } { id = &...
user3719188's user avatar
0 votes
1 answer
403 views

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 ...
Julien LAFFITTE's user avatar
3 votes
0 answers
506 views

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....
GuidoT's user avatar
  • 322
0 votes
1 answer
207 views

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 ...
Russ Thils's user avatar
3 votes
4 answers
4k views

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 ...
FatalBulletHit's user avatar
1 vote
1 answer
96 views

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 ...
McTech's user avatar
  • 13
1 vote
1 answer
1k views

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 ...
ZZZSharePoint's user avatar
1 vote
4 answers
7k views

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 ...
tigger's user avatar
  • 13
0 votes
0 answers
170 views

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 ...
jimerb's user avatar
  • 77
1 vote
1 answer
293 views

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 ...
Brian Snyder's user avatar
0 votes
1 answer
230 views

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 ...
Jeff's user avatar
  • 36.7k