# Help with if statement

**URL:** <https://forums.powershell.org/t/help-with-if-statement/6116>\
**Category:** PowerShell Help\
**Created:** [April 3, 2016, 8:18pm UTC](https://forums.powershell.org/t/help-with-if-statement/6116 "2016-04-03T20:18:58Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![simon-taylor](https://avatars.discourse-cdn.com/v4/letter/s/85e7bf/32.png) [@simon-taylor](https://forums.powershell.org/u/simon-taylor)\
**Post date:** [April 3, 2016, 8:18pm UTC](https://forums.powershell.org/t/help-with-if-statement/6116/1 "2016-04-03T20:18:58Z")

</div>

Hi all,

Wonder if anyone more learned than myself with Powershell can help me out. As I say I’m a beginner so please bear with me if my request seems a bit simple. I am attempting to write a script that grabs the contents of a particular column in a csv file and then runs a command against it, in this instance ‘Get-SCOMMonitor’. Once it has run this it should then output it to a csv file.

Now, this actually works but here is the annoying part. As the script works its way through the spreadsheet, if it comes across a line that does not generate a valid result then it just skips it. What I want it to do on my outputted spread sheet is have some kind of message on the said line such as ‘Monitor not valid’ or something like that.

My original input csv file could be something like 600 lines long but after running my script it could say reduce to 200 lines and then I wouldn’t be able to match up the results with my original spreadsheet. I need the number of lines to match basically. My original script without any if statements look like: -

$csv = Import-Csv D:\Scripts\temp\monitors.csv  
$output = foreach ($line in $csv) {  
Get-SCOMMonitor -Name $line.MonitorName  
}

$output | Export-csv -Path D:\Scripts\temp\output.csv -NoTypeInformation

I was hoping to use something like: if ($output -eq $null) { Write-Host “Monitor could not be determined” }

I couldn’t get it to work properly so hopefully someone will look at this and easily spot my problem.

Thanks,

Simon

---

<div class="post-metadata">

**Author:** ![david-schmidtberger](https://avatars.discourse-cdn.com/v4/letter/d/ba9def/32.png) [@david-schmidtberger](https://forums.powershell.org/u/david-schmidtberger)\
**Post date:** [April 3, 2016, 10:51pm UTC](https://forums.powershell.org/t/help-with-if-statement/6116/2 "2016-04-03T22:51:55Z")

</div>

i don’t use SCOM so not sure exactly what is returned from the cmdlet, however, you can’t just “write” to the csv file the way you are trying, my suggestion is to have 2 output files

```
$erroractionpreference = "stop"
$csv = Import-Csv D:\Scripts\temp\monitors.csv
$output = "D:\Scripts\temp\output.csv"
$erroroutput = "D:\Scripts\temp\errors.txt"
foreach ($line in $csv)
{
	try
	{
	Get-SCOMMonitor -Name $line.MonitorName|Export-Csv $output -NoTypeInformation -append
	}
	catch
	{
	"Monitor could not be determined for $($line.monitor)"|Out-File $erroroutput -append
	}
}
```

---

<div class="post-metadata">

**Author:** ![paal-braathen](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/paal-braathen/32/523_2.png) [@paal-braathen](https://forums.powershell.org/u/paal-braathen)\
**Post date:** [April 4, 2016, 5:23pm UTC](https://forums.powershell.org/t/help-with-if-statement/6116/3 "2016-04-04T17:23:31Z")

</div>

I’m not familiar with the Get-SCOMMonitor either.

@david-schmidtberger  
I believe you’re overwriting the files in the foreach. You should add the append parameter.

---

<div class="post-metadata">

**Author:** ![david-schmidtberger](https://avatars.discourse-cdn.com/v4/letter/d/ba9def/32.png) [@david-schmidtberger](https://forums.powershell.org/u/david-schmidtberger)\
**Post date:** [April 4, 2016, 10:26pm UTC](https://forums.powershell.org/t/help-with-if-statement/6116/4 "2016-04-04T22:26:36Z")

</div>

good catch Paal  
i updated the code above to include appends

---

<div class="post-metadata">

**Author:** ![simon-taylor](https://avatars.discourse-cdn.com/v4/letter/s/85e7bf/32.png) [@simon-taylor](https://forums.powershell.org/u/simon-taylor)\
**Post date:** [April 12, 2016, 8:21pm UTC](https://forums.powershell.org/t/help-with-if-statement/6116/5 "2016-04-12T20:21:43Z")

</div>

Thanks for the responses guys this helped! 🙂

---

<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:41pm UTC](https://forums.powershell.org/t/help-with-if-statement/6116/6 "2024-05-16T20:41:34Z")

</div>


