Menu
  • HOME
  • TAGS

Get Average CPU utilization for test execution in powershell

powershell,cpu-usage,powershell-v3.0,scriptblock

The underlying problem was that the test was running for anywhere between 2 and 60 seconds, and there where runs where the Get-WmiObject was taking longer than the test run, and thus the code for creating the file with the collected metrics wasn't executed for those runs. Adding a wait...

Executing multiple commands in a scriptblock and returning multiple responses

powershell,scriptblock

One option is to load your results into a hash table, the return that. $Scriptblock = { $response = @{} $response.dir = gc "C:\test.txt" $response.service = get-webservice | ? {$_.Name -eq "w32time"} $response } $result = &$Scriptblock This eliminates any ambiguity in the results of any of the commands returns...

How to invoke a powershell scriptblock with $_ automatic variable

powershell,scriptblock,automatic-variable

After fiddling around with varoius options, I ended up sloving the problem, in a sort of Hackish way.. But I find it to be the best solution because it's small, easy to read, maintain and understand. Even though we are talking about single object, use it in the pipeline (which...

Powershell passing arguments in ScriptBlock

powershell,invoke-command,scriptblock

Another way, if you are using PowerShell 3. You can do something like this: $lastWrite = Invoke-Command -Computername $server -ScriptBlock { Get-ChildItem "\\$using:server\hot.war" } | select -Property LastWriteTime ...

Powershell function creation with Set-Item and GetNewClosure

powershell,scriptblock

The clue is in the MSDN docs but it is subtle: Any local variables that are in the context of the caller are copied into the module. GetNewClosure() appears to capture "only" local variables i.e those from the caller's current scope. So try this: function Make-Function { $x = $global:x...

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

powershell,delegates,pipeline,scriptblock

That'll work. You've just got some syntax issues with your function definitions and how you're passing the parameters: Function Get-Square{ [CmdletBinding()] Param([Parameter(ValueFromPipeline=$true)]$x) $x*$x } Function Get-Cube{ [CmdletBinding()] Param([Parameter(ValueFromPipeline=$true)]$x) $x*$x*$x } Function Get-Result{ [CmdletBinding()] Param([Parameter(ValueFromPipeline=$true)]$x,$Cmdlet) $x | . $cmdlet } 10 | Get-Result -Cmdlet Get-Square 10 | Get-Result -Cmdlet Get-Cube 100...

How to refer to entire pipeline object without a script block

powershell,scriptblock

To reduce $stuff | Where { $_ -match "win" } you can always do it like this (works for all powershell version): $stuff = "a","b","win", "winona", "d", "windows" $stuff -match "win" win winona windows ...