0

I have written a script to scan an excel spreadsheet and return a count of specific values and output that count.

The script works fine if there is more than 1 of the same value and returns the count. If the excel spreadsheet only contains 1 value I am looking for count returns as blank.

This is the script I am running

$Skipped = Import-excel -path 'C:\temp\IM April Test Migration Stuff\Sharegate Task Reports\im3.xlsx' | where 'Status' -eq 'Skipped' | select title, Type, status
$report = New-Object psobject
$report | Add-Member -MemberType NoteProperty -Name Skipped -Value $Skipped.count

write-host $report

Any ideas on how to workaround this issue?

  • TLDR: please try to build an [mcve]. Anyways, I guess you ran into a [classic PowerShell gotcha (`#4a`)](https://stackoverflow.com/a/69644807/1701026) – iRon Apr 13 '23 at 05:37
  • 1
    Thank you, I have amended my question to make it more minimal and easier to understand. Thanks for the heads up. – Dominic Gibson Apr 13 '23 at 09:05
  • It is indeed minimal now, but I can't reproduce anything with it... – iRon Apr 13 '23 at 09:16
  • 1
    @iRon I think I have sorted it using the #4a advice. I have just loaded the value into an array and counted the array, seems to be working fine now. I will post up the code I am using below. – Dominic Gibson Apr 13 '23 at 09:25

1 Answers1

0

I think I have sorted it using the #4a advice. I have just loaded the value into an array and counted the array, seems to be working fine now. This is he code I am now using

$Skipped = @(($Skipped = Import-excel -path 'C:\temp\IM April Test Migration Stuff\Sharegate Task Reports\im3.xlsx' | where 'Status' -eq 'Skipped' | select title, Type, status))

$report = New-Object psobject
$report | Add-Member -MemberType NoteProperty -Name Skipped -Value $Skipped.count

write-host $report

I have tested with a different range of values and picks them all up.