I am trying to import a CSV which contains fictional places and hours each day of the week those fictional places are open.
The hours are from 5:00-4:00 format. Some have spaces. I created a function to remove the spaces. After that function is run, it appears PowerShell can't run any further operations on the returned string (i.e. -split).
The CSV:
Node,SAT,SUN,MON,TUE,WED,THU,FRI
PizzaPlace,9:00 – 4:30,0,8:00-3:30,7:00 – 10:00,10:00 – 4:00,10:00 – 4:00,10:00 – 4:00
BigPharma,0,5:00 – 4:00,7:00-6:00,7:00-6:00,0,0,7:00-6:00
GreenHouse,12:00-8:00,0,12:00-7:30,12:00-7:30,12:00-7:30,12:00-7:30,12:00-7:30
Portapoty,12:00-8:00,Closed,10:00-6:00,10:00-7:30,10:00-6:00,10:00-7:30,10:00-6:00
The PS1 script:
function Unk-AMPM ($openStr) {
$openStr -replace " ";
}
$csvInputs = Import-CSV SampHours.csv;
$srPRBN = "Our hours for";
$srPRAN = "are";
$dsWed = "Wednesday";
foreach ($csvLine in $csvInputs) {
$retailer = $csvLine.Node;
[string] $openWed = Unk-AMPM $csvLine.WED;
Write-Host "Value of openWed before split: "$openWed;
$openWedA = $openWed -split "-";
Write-Host "Value of openWedA[0]: "$openWedA[0];
Write-Host "Value of openWedA[1]: "$openWedA[1];
if ($openWedA[0] -eq 0 -or $openWedA[0] -eq 'Closed') {
$ohsWed = "closed";
} else { $ohsWed = $openWedA[0] + " to " + $openWedA[1]; }
Write-Host $srPRBN $retailer $srPRAN $ohsWed "on" $dsWed;
}
And the results:
Value of openWed before split: 10:00–4:00
Value of openWedA[0]: 10:00–4:00
Value of openWedA[1]:
Our hours for PizzaPlace are 10:00–4:00 to on Wednesday
Value of openWed before split: 0
Value of openWedA[0]: 0
Value of openWedA[1]:
Our hours for BigPharma are closed on Wednesday
Value of openWed before split: 12:00-7:30
Value of openWedA[0]: 12:00
Value of openWedA[1]: 7:30
Our hours for GreenHouse are 12:00 to 7:30 on Wednesday
Value of openWed before split: 10:00-6:00
Value of openWedA[0]: 10:00
Value of openWedA[1]: 6:00
Our hours for Portapoty are 10:00 to 6:00 on Wednesday
0? Thats because the second record has a0in the wednsday column...10:00-4:00. It should have been split so that$openWedAcontained two elements with each time, but it seems like it didn't work on that one whereas it did on the others.