Chapter 8
Activities
- The easy way to do this is to create an
$Output
variable in theParam()
block, and pass that as a parameter to theOut-File
cmdlet in the script, like this:[CmdletBinding()] Param( $City = "London", $Output = "c:\temp\poshbook\ch8\WeatherData.html" ) $headers = @{"key" = "<Key>"} $uri = "https://api.weatherapi.com/v1/current.json?q=$($City)&aqi=no" $response = Invoke-RestMethod -Uri $uri -Method GET -Headers $headers $response | Convertto-Html | Out-File $Output
The trouble with that is if we want to change the filename, we need to type both the filename and the path each time. It’s likely that we will want to change the filename far more often than we want to change the path. Let’s separate out the path and the filename like this:
Figure A.8 – Separating the filename and file path
Now, we can just pass a different filename when we want to, and...