I'm a noob at Powershell, and I'm working on an assignment I already tried to do as much research on my own, but I'm stomped and not getting anywhere.
So, what I'm trying to do is Create a function with a required filename parameter.
This Function will need to do the following: Import the provided CSV file, Filter only to show UT state results, Show only the following properties: state, zip, utility_name, commercial_rate, industrial_rate, residential_rate, Sort the result by Zip code and finally Export the result into a new CSV file
Any help will be appreciated. If there is anything I said that's not clear. Let me know so I can clarify it.
Here's my current function:
function Get-SortCity {
>> param (
>> #This Parameter is mandatory
>> [Parameter(mandatory=$true)]
>> [string]$Filename
>>
>> )#below is my script to import the CSV file, select properties, and then export it to a CSV.
>> Import-Csv $Filename | Where-Object state -Match utah | Select-Object state, zip, utility_name, commercial_rate, industrial_rate, residential_rate| Sort-Object zip | Export-Csv -Path C:\Users\kyros\Downloads\Documents\utahrates.csv
>> }
When I run sections of my script manually, I get an error around "Import-Csv $Filename" It tells me Import-Csv: Cannot validate argument on parameter 'Path' The argument is null or empty. Provide an argument that is not null or empty, and then try the command again
What am I doing wrong that variable would be empty until I call the function and input a path.
The problem is most likely in the
Where-Object state -match utah
What is utah? Powershell has no idea what you are talking about. It doesn't know that utah is a state. It tries to resolve utah in the same way it resolved state. It looks for a property named utah in the input object. There is no such property. So the comparison is with $null, and this always fails.
Try this instead
Where-Object state -match 'UT'
Here 'UT' is a literal string, because of the quotes. 'UT' is a two character abbreviation for the state, and this may be what's in your input CSV file. Two letter state abbreviations are upper case, and so is 'UT'. If I remember right, -match is not case sensitive, but what the heck.