Error on running Pester test using foreach-object -parallel - powershell

I have this infrastructure pester test. Code for demonstration purpose:
Describe 'WEB-Tests' {
$servers = 'ServerA','ServerB'
$sessions = #()
foreach ($server in $servers) {
$sessions += New-PSSession -ComputerName $server
}
$sessions | foreach-object {
Context " Service is Running on $($_.ComputerName)" {
$service = invoke-command -session $_ -scriptblock { get-service 'some service' }
It "Service $($service.Name) should be Running" {
$service.Status | Should be "Running"
}
}
}
}
This works fine. If I replace the $sessions|foreach-object with $sessions|foreach-object -Parallel, i get this error -
New-PesterState: C:\Program Files\WindowsPowerShell\Modules\Pester\4.9.0\Functions\Context.ps1:77:128
Line |
77 | … '] .) -TestNameFilter $null -TagFilter #() -SessionState SessionState
| ~~~~~~~~~~~~
| Cannot process argument transformation on parameter 'SessionState'. Cannot convert the "SessionState" value of type "System.String" to type
| "System.Management.Automation.SessionState".
Exception: The Context command may only be used from a Pester test script.
Please suggest on how to achieve parallelism here as there are hundreds of servers.
RESOLVED-I figured out a workaround by using jobs and it serves the purpose.
Describe 'WEB-Tests' {
$servers = 'ServerA','ServerB'
$sessions = #()
foreach ($server in $servers) {
$sessions += New-PSSession -ComputerName $server
}
Get-Job | Remove-Job
invoke-command -session $sessions -scriptblock { get-service W3SVC,WAS } -AsJob
$j = Get-Job | wait-job
($results = $j | Receive-Job) | out-null
Context "IIS Services are Running" {
foreach ($result in $results) {
It "Service $($result.Name) should be Running on $($result.PSComputerName)" {
$result.Status | Should be "Running"
}
}
}
}

Related

Invoke-Command in Workflow sequence

Please help to get Invoke-Command working. It says -ScriptBlock parameter is null. It seems RegHomePage() function is not available in InlineScript{}.
function RegHomePage (){
get-item -path Registry::"HKEY_USERS\*\Software\Microsoft\Internet Explorer\Main" | `
Get-ItemProperty | Where-Object {$_."Start Page" -ne $null} | Set-ItemProperty -Name "Start Page" -Value "about:blank"
}
$creds = Get-Credential -Credential value\wadmin
workflow foreachtest
{
param([Parameter(mandatory=$true)][string[]]$computers)
foreach -parallel -throttlelimit 20 ($computer in $computers)
{
sequence
{
$isPing = Test-Connection -count 1 $computer -quiet
if($isPing){
$isWSMan = [bool](Test-WSMan $computer -ErrorAction SilentlyContinue)
}
if($isWSMan){
InlineScript{
Invoke-Command -ComputerName $USING:computer -ScriptBlock ${function:RegHomePage}
} -PSComputerName $computer
echo "$computer OK"
}
Else{
$Workflow:out += "$computer`r`n"
echo "$computer FAILED"
}
}
}
Out-File .\offline.txt -InputObject $out
}
foreachtest -computers (Get-Content .\comps.txt)
Seems to have a few issues with this inlineScript block.
Dont provide the PSComputerName parameter since you are already running a job on each computer. There is no need to reference other systems here.
I would suggest using Write-Output instead of echo (use powershell native commands)
Move the function within the inlinescript to bring it in scope of each iteration.
workflow testing {
foreach -parallel ($computer in $computers) {
sequence {
inlinescript {
function RegHomePage {
Get-Item -path Registry::"HKEY_USERS\*\Software\Microsoft\Internet Explorer\Main" | `
Get-ItemProperty | Where-Object {$_."Start Page" -ne $null} | Set-ItemProperty -Name "Start Page" -Value "about:blank"
}
Invoke-Command -ComputerName $using:computer -ScriptBlock ${Function:RegHomePage}
}
}
}
}
Following is what I tested with.
workflow testingWF {
Param ([string[]] $computers)
foreach -parallel ($computer in $computers) {
sequence {
InlineScript {
function testFunc {
Param($comp)
Write-Output "$($comp.split('.')[0]) == TestFunc"
}
Invoke-Command -ComputerName $Using:computer -ScriptBlock ${Function:testFunc} -ArgumentList $using:computer
}
}
}
}
testingWF serverFQDN1,serverFQDN2
#Prints
server1 == TestFunc
server2 == TestFunc
Suggestion on how to re-write the above code
Instead of using a workflow to run a parallel foreach loop, i would recommend replacing the functionality with -AsJob.
foreach($computer in $computers) {
Invoke-Command -ComputerName $computer -ScriptBlock ${Function:RegHomePage} -AsJob
}
# Remove Jobs when done
Get-Job | Wait-Job | Remove-Job
InlineScript dont support $using:function , try nested workflow nested work
You can move your function inside InlineScript block .
Are you sure that key -PSComputerName must have value $Computers instead $computer
Adding
Only one way to call function at inlinescriptblock, it's a put it inside. But may you can use nested workflow to call few times invoke comand. Example nested:
workflow Test-Workflow {
function mess{"get ready"}
workflow nest-test{
mess
}
nest-test
}
Test-Workflow
You can also read why you can't use import it to inline script in this tutorial:
tutorial

powershell -parallel not working

I am writing a PowerShell script to display the status of free space and CPU, below is snippet:
function Get_FreeSpace ($authType, $comp) {
$freespace = Invoke-Command -ScriptBlock {
Get-WmiObject Win32_LogicalDisk -Filter "DeviceID='E:'" |
Select-Object Size,Freespace
} -ComputerName $comp -Credential $authType
$freesize = [String]::Format("{0:P2}" -f ($freespace.Freespace / $freespace.Size))
return $freesize
}
function Get_CPUAverage ($authType, $comp) {
$Cpu_Average = Invoke-Command -ScriptBlock {
Get-WmiObject Win32_Processor |
Measure-Object -Property LoadPercentage -Average |
Select-Object Average
} -ComputerName $comp -Credential $authType
return $Cpu_Average.Average
}
workflow Test-Workflow {
foreach -parallel ($serv1 in $Servers) {
$Size = Get_FreeSpace -authType $cred -comp $serv1
$Cpu_Avg = Get_CPUAverage -authType $cred -comp $serv1
Write-Host $Size
}
}
Test-Workflow
Write-Host $Size
$Size is not returning anything if I use -parallel loop.
should pass $Servers as param as shown below:
workflow Test-Workflow {
param($Servers)
foreach -parallel ($serv1 in $Servers) {
$Size = Get_FreeSpace -authType $cred -comp $serv1
$Cpu_Avg = Get_CPUAverage -authType $cred -comp $serv1
Write-Host $Size
}
}
Test-Workflow $Servers
Alternatively, invoke-command runs in parallel with multiple computers:
invoke-command comp1,comp2,comp3 { sleep 5; "done $env:computername" }

Multi-threading with PowerShell

I have done lots of reading about multi-threading in PowwerShell with Get-Job and Wait-Job but still cant seem to work it out.
Eventually, I will have this as a GUI based script to run and don't want my GUI to freeze up while its doing its task.
The script is looking for Event Logs of my Domain Controllers and then getting the details I want, then outputting them, it works like I need it to.
I can start a job using Invoke-Command {#script goes here} -ComputerName ($_) -AsJob -JobName $_ and the jobs run.
Script below:
Clear-Host
Get-Job | Remove-Job
(Get-ADDomainController -Filter *).Name | ForEach-Object {
Invoke-Command -ScriptBlock {
$StartTime = (Get-Date).AddDays(-4)
Try{
Get-WinEvent -FilterHashtable #{logname='Security'; id=4740;StartTime=$StartTime} -ErrorAction Stop `
| Select-Object * | ForEach-Object {
$Username = $_.Properties[0].Value
$lockedFrom = $_.Properties[1].Value
$DC = $_.Properties[4].Value
$Time = $_.TimeCreated
Write-Host "---------------------------------------------"
Write-Host $Username
Write-Host $lockedFrom
Write-Host $DC
Write-Host $Time
Write-Host "---------------------------------------------"
}#ForEach-Object
}catch [Exception] {
If ($_.Exception -match "No events were found that match the specified selection criteria") {
Write-Host "No events for locked out accounts." -BackgroundColor Red
}#If
}#Try Catch
} -ComputerName ($_) -AsJob -JobName $_ | Out-Null # Invoke-Command
}#ForEach-Object
Currently I have a While loop to tell me its waiting then to show me the result:
(Get-ADDomainController -Filter *).Name | ForEach-Object {
Write-Host "Waiting for: $_."
While ($(Get-Job -Name $_).State -ne 'Completed') {
#no doing anything here
}#While
Receive-Job -Name $_ -Keep
}#ForEach-Object
#clean up the jobs
Get-Job | Remove-Job
Thinking of my GUI (to be created), I will have a column for each Domain Controller and showing results under each heading, how do make it not freeze my GUI and show the results when they arrive?
I know its been asked a few times, but the examples I cant work out.
I would avoid Start-Job for threading - for efficiency try a runspace factory.
This is a basic setup which could be useful (I also have PS 4.0), and open to suggestions/improvements.
$MaxThreads = 2
$ScriptBlock = {
Param ($ComputerName)
Write-Output $ComputerName
#your processing here...
}
$runspacePool = [RunspaceFactory]::CreateRunspacePool(1, $MaxThreads)
$runspacePool.Open()
$jobs = #()
#queue up jobs:
$computers = (Get-ADDomainController -Filter *).Name
$computers | % {
$job = [Powershell]::Create().AddScript($ScriptBlock).AddParameter("ComputerName",$_)
$job.RunspacePool = $runspacePool
$jobs += New-Object PSObject -Property #{
Computer = $_
Pipe = $job
Result = $job.BeginInvoke()
}
}
# wait for jobs to finish:
While ((Get-Job -State Running).Count -gt 0) {
Get-Job | Wait-Job -Any | Out-Null
}
# get output of jobs
$jobs | % {
$_.Pipe.EndInvoke($_.Result)
}

Use PowerShell to run virus scan on multiple servers

I'm trying to run a virus scan on a list of servers in our environment. There are hundreds of machines, so we'd like to run the scan (using a command line prompt that we already have) around 10 at a time. We're totally new to PowerShell so any help would be really appreciated. We have a general idea of what commands we need to use -- here's how we think it might work for now:
$server = Get-Content "serverlist.txt"
$server | % {
$VirusScan = { Scan32.exe }
Invoke-Command -ScriptBlock { $VirusScan } -computerName $server -ThrottleLimit 10 -Authentication domain/admin
}
Does anyone have any suggestions on how we might orchestrate this?
I'm using something like this for running tasks in parallel on remote hosts:
$maxSlots = 10
$hosts = "foo", "bar", "baz", ...
$job = {
Invoke-Command -ScriptBlock { Scan32.exe } -Computer $ARGV[0] -ThrottleLimit 10 -Authentication domain/admin
}
$queue = [System.Collections.Queue]::Synchronized((New-Object System.Collections.Queue))
$hosts | ForEach-Object { $queue.Enqueue($_) }
while ( $queue.Count -gt 0 -or #(Get-Job -State Running).Count -gt 0 ) {
$freeSlots = $maxSlots - #(Get-Job -State Running).Count
for ( $i = $freeSlots; $i -gt 0 -and $queue.Count -gt 0; $i-- ) {
Start-Job -ScriptBlock $job -ArgumentList $queue.Dequeue() | Out-Null
}
Get-Job -State Completed | ForEach-Object {
Receive-Job -Id $_.Id
Remove-Job -Id $_.Id
}
Sleep -Milliseconds 100
}
# Remove all remaining jobs.
Get-Job | ForEach-Object {
Receive-Job -Id $_.Id
Remove-Job -Id $_.Id
}

How do I add multi-threading?

Is there a way of getting the below to run in parallel (multi-threading)? I have about 200 servers that need to run and was wondering if there is a way of checking say 10 servers at once rather then one at a time...WMI is very slow in checking this one at a time.
clear
Write-Host "Script to Check if Server is Alive and Simple WMI Check"
$servers = Get-Content -Path c:\Temp\Servers.txt
foreach($server in $servers)
{
if (Test-Connection -ComputerName $server -Quiet)
{
$wmi = (Get-WmiObject -Class Win32_ComputerSystem -ComputerName $server).Name
Write-Host "$server responds: WMI reports the name is: $wmi"
}
else
{
Write-Host "***$server ERROR - Not responding***"
}
}
Use powershell jobs:
$scriptblock = {
Param($server)
IF (Test-Connection $server -Quiet){
$wmi = (gwmi win32_computersystem -ComputerName $server).Name
Write-Host "***$server responds: WMI reports the name is: $wmi"
} ELSE { Write-Host "***$server ERROR -Not responding***" }
}
$servers | % {Start-Job -Scriptblock $scriptblock -ArgumentList $_ | Out-Null}
Get-Job | Wait-Job | Receive-Job