# How to script my output

**URL:** <https://forums.powershell.org/t/how-to-script-my-output/11779>\
**Category:** PowerShell Help\
**Created:** [December 11, 2018, 11:42am UTC](https://forums.powershell.org/t/how-to-script-my-output/11779 "2018-12-11T11:42:50Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![trgrassijr](https://avatars.discourse-cdn.com/v4/letter/t/fbc32d/32.png) [@trgrassijr](https://forums.powershell.org/u/trgrassijr)\
**Post date:** [December 11, 2018, 11:42am UTC](https://forums.powershell.org/t/how-to-script-my-output/11779/1 "2018-12-11T11:42:50Z")

</div>

I would like to just list the backup jobs that show warning or critical in the out put My Code

```
asnpVeeamPSSnapin$Job=Get-VBRJob-name"MIS-M$-08-64bit"$Session=$Job.FindLastSession()$Session|Get-VBRTaskSession|selectname,status
```

The current out put is this

Name Status

* * *

ReslifeDB Warning

Webui Warning

FAReport Success

FAReportsDB Warning

Remus Success

Reslife-readyfordecommis Success

webadvisorview Critical

webadvisorview-gw Warning

webadvisorview-dev Warning

I only want to display the jobs with warning or critical status.

Any ideas?

&nbsp;

thank you

&nbsp;

&nbsp;

&nbsp;

I

&nbsp;

---

<div class="post-metadata">

**Author:** ![kvprasoon](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/kvprasoon/32/4449_2.png) [@kvprasoon](https://forums.powershell.org/u/kvprasoon)\
**Post date:** [December 11, 2018, 11:56am UTC](https://forums.powershell.org/t/how-to-script-my-output/11779/2 "2018-12-11T11:56:25Z")

</div>

I will give you an example, you can implement this to your code.

```
Get-Process | Select-Object -ExpandProperty Name,Id
Get-Process | Where-Object -FilterScript {$_.Name -eq 'PowerShell'} | Select-Object -ExpandProperty Name,Id
```

---

<div class="post-metadata">

**Author:** ![trgrassijr](https://avatars.discourse-cdn.com/v4/letter/t/fbc32d/32.png) [@trgrassijr](https://forums.powershell.org/u/trgrassijr)\
**Post date:** [December 11, 2018, 2:21pm UTC](https://forums.powershell.org/t/how-to-script-my-output/11779/3 "2018-12-11T14:21:39Z")

</div>

Thanks on my way

$Session | Get-VBRTaskSession | select name, status | Where-Object -FilterScript {$\_.status -eq ‘Warning’}

This worked.

Name Status

* * *

ReslifeDB Warning

Webui Warning

FAReportsDB Warning

webadvisorview Warning

webadvisorview-gw Warning

webadvisorview-dev Warning

&nbsp;

&nbsp;

How can I add ‘critical’

$Session | Get-VBRTaskSession | select name, status | Where-Object -FilterScript {$\_.status -eq ‘Warning’ or ‘Critical’}

&nbsp;

&nbsp;

---

<div class="post-metadata">

**Author:** ![ta11ow](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/ta11ow/32/131_2.png) [@ta11ow](https://forums.powershell.org/u/ta11ow)\
**Post date:** [December 11, 2018, 3:02pm UTC](https://forums.powershell.org/t/how-to-script-my-output/11779/4 "2018-12-11T15:02:54Z")

</div>

Two ways:

`$.Status -eq ‘Warning’ -or $.Status -eq ‘Critical’`

`$_.Status -in ‘Warning’, ‘Critical’`

🙂

Check `get-help about_operators` for some in depth information.

---

<div class="post-metadata">

**Author:** ![trgrassijr](https://avatars.discourse-cdn.com/v4/letter/t/fbc32d/32.png) [@trgrassijr](https://forums.powershell.org/u/trgrassijr)\
**Post date:** [December 11, 2018, 4:25pm UTC](https://forums.powershell.org/t/how-to-script-my-output/11779/5 "2018-12-11T16:25:08Z")

</div>

```
$Session|Get-VBRTaskSession|selectname,status|Where-Object-FilterScript {$\_.status -eq'Failed'-or$\_.status -eq'Warning' }
```

&nbsp;

That worked

et-VBRTaskSession | select name, status | Where-Object -FilterScript {$_.status -eq ‘Failed’ -or $_.status -eq ‘Warning’ }

&nbsp;

Name Status

* * *

Rum Failed

Scotch Failed

Vodka Failed

Cognac Failed

Mezcal Warning

Now how can I just get a count of each type failed warning and success

This is for a Nagios check and we have jobs that have over 20 servers if they all fail or have warnings that would be way too much data to display back to Nagios.

So I would like the output to look similar to this.

Critical Job $name 5 Servers Successful 2 Servers Warnings 1 Servers Critical

Thanks

&nbsp;

---

<div class="post-metadata">

**Author:** ![alex-aymonier](https://avatars.discourse-cdn.com/v4/letter/a/ecd19e/32.png) [@alex-aymonier](https://forums.powershell.org/u/alex-aymonier)\
**Post date:** [December 11, 2018, 7:24pm UTC](https://forums.powershell.org/t/how-to-script-my-output/11779/6 "2018-12-11T19:24:45Z")

</div>

Remember to filter to the left as much as possible. Might not make a great deal of difference now but further down the line with bigger more complex scripts it will save you time.

&nbsp;

```
$Session | Get-VBRTaskSession | Where-Object -FilterScript {$_.status -eq 'Failed' -or $_.status -eq 'Warning' }| select name, status
```

---

<div class="post-metadata">

**Author:** ![trgrassijr](https://avatars.discourse-cdn.com/v4/letter/t/fbc32d/32.png) [@trgrassijr](https://forums.powershell.org/u/trgrassijr)\
**Post date:** [December 11, 2018, 7:39pm UTC](https://forums.powershell.org/t/how-to-script-my-output/11779/7 "2018-12-11T19:39:27Z")

</div>

Alex

Yes that way works also

But I need to get the count of warning servers critical servers and success servers

&nbsp;

the out put would look like this

$name is the Job Name

Critical Job $name 5 Servers Successful 2 Servers Warnings 1 Servers Critical

&nbsp;

Thoughts

&nbsp;

&nbsp;

---

<div class="post-metadata">

**Author:** ![alex-aymonier](https://avatars.discourse-cdn.com/v4/letter/a/ecd19e/32.png) [@alex-aymonier](https://forums.powershell.org/u/alex-aymonier)\
**Post date:** [December 11, 2018, 8:11pm UTC](https://forums.powershell.org/t/how-to-script-my-output/11779/8 "2018-12-11T20:11:25Z")

</div>

Look at group-object

---

<div class="post-metadata">

**Author:** ![trgrassijr](https://avatars.discourse-cdn.com/v4/letter/t/fbc32d/32.png) [@trgrassijr](https://forums.powershell.org/u/trgrassijr)\
**Post date:** [December 11, 2018, 8:26pm UTC](https://forums.powershell.org/t/how-to-script-my-output/11779/9 "2018-12-11T20:26:32Z")

</div>

Current code

$Session | Get-VBRTaskSession | Where-Object -FilterScript {$_.status -eq ‘Failed’ -or $_.status -eq ‘Warning’ }| select name, status

group-object

$Session | Get-VBRTaskSession | group-object -property status | Where-Object -FilterScript {$_.status -eq ‘Failed’ -or $_.status -eq ‘Warning’ }| select name, status

no output even if it did how would I format as mentioned before

&nbsp;

---

<div class="post-metadata">

**Author:** ![trgrassijr](https://avatars.discourse-cdn.com/v4/letter/t/fbc32d/32.png) [@trgrassijr](https://forums.powershell.org/u/trgrassijr)\
**Post date:** [December 12, 2018, 4:17pm UTC](https://forums.powershell.org/t/how-to-script-my-output/11779/10 "2018-12-12T16:17:05Z")

</div>

Still need help on this one.

---

<div class="post-metadata">

**Author:** ![palhan](https://avatars.discourse-cdn.com/v4/letter/p/96bed5/32.png) [@palhan](https://forums.powershell.org/u/palhan)\
**Post date:** [December 12, 2018, 5:14pm UTC](https://forums.powershell.org/t/how-to-script-my-output/11779/11 "2018-12-12T17:14:45Z")

</div>

Try this

```
# sort it, so we know the index
# 0=critical, 1=success, 2=warning
$stats = $Session | Get-VBRTaskSession | Group-Object status | Sort-Object Name

"Critical Job $name $($stats[1].Count) Servers Successful $($stats[2].Count) Servers Warnings $($stats[0].Count) Servers Critical"
```

---

<div class="post-metadata">

**Author:** ![trgrassijr](https://avatars.discourse-cdn.com/v4/letter/t/fbc32d/32.png) [@trgrassijr](https://forums.powershell.org/u/trgrassijr)\
**Post date:** [December 12, 2018, 5:29pm UTC](https://forums.powershell.org/t/how-to-script-my-output/11779/12 "2018-12-12T17:29:19Z")

</div>

Thanks that worked with only one minor thing

&nbsp;

&nbsp;

```
asnpVeeamPSSnapin$Job=Get-VBRJob-name"USAS-SpiritsMadison\_StoreOnce"$Session=$Job.FindLastSession()# sort it, so we know the index

# 0=critical, 1=success, 2=warning$stats=$Session|Get-VBRTaskSession|Group-Objectstatus|Sort-ObjectName"Critical Job $name$($stats[1].Count) Servers Successful $($stats[2].Count) Servers Warnings $($stats[0].Count) Servers Critical"
```

Critical Job 2 Servers Successful 1Critical Servers Warnings 4 Servers

It is missing the Job name

&nbsp;

Critical

---

<div class="post-metadata">

**Author:** ![trgrassijr](https://avatars.discourse-cdn.com/v4/letter/t/fbc32d/32.png) [@trgrassijr](https://forums.powershell.org/u/trgrassijr)\
**Post date:** [December 12, 2018, 5:41pm UTC](https://forums.powershell.org/t/how-to-script-my-output/11779/13 "2018-12-12T17:41:13Z")

</div>

I got it now thanks

---

<div class="post-metadata">

**Author:** ![palhan](https://avatars.discourse-cdn.com/v4/letter/p/96bed5/32.png) [@palhan](https://forums.powershell.org/u/palhan)\
**Post date:** [December 12, 2018, 7:01pm UTC](https://forums.powershell.org/t/how-to-script-my-output/11779/14 "2018-12-12T19:01:18Z")

</div>

Wait, there is a bug. I was focused on making it simple, so I forgot to think it through. It only works, if all three states are always present. This version is a bit longer, but should work in all cases:

```
$stats = $Session | Get-VBRTaskSession | Group-Object status | Sort-Object Name

$success = 0
$warning = 0
$critical = 0

foreach($stat in $stats) {
    switch($stat.Name) {
        'Success' { $success = $stat.Count; break }
        'Warning' { $warning = $stat.Count; break }
        'Critical' { $critical = $stat.Count; break }
    }
}
"Critical Job $name $success Servers Successful $warning Servers Warnings $critical Servers Critical"
```

---

<div class="post-metadata">

**Author:** ![trgrassijr](https://avatars.discourse-cdn.com/v4/letter/t/fbc32d/32.png) [@trgrassijr](https://forums.powershell.org/u/trgrassijr)\
**Post date:** [December 13, 2018, 1:33pm UTC](https://forums.powershell.org/t/how-to-script-my-output/11779/15 "2018-12-13T13:33:03Z")

</div>

Phansen

&nbsp;

That worked great

&nbsp;

&nbsp;

&nbsp;

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:34pm UTC](https://forums.powershell.org/t/how-to-script-my-output/11779/16 "2024-05-16T20:34:07Z")

</div>

Closed on May 16, 2024, 8:34pm UTC


