function turtlesong {
param(
[string]$time,
[string]$link,
[string]$number
)
# create exception for when variables are not passed
if ($time -eq $null -or $link -eq $null -or $number -eq $null) {
return "variables that need to be passed: -time, -link, -number"
}
if (Test-Path "C:\Users\aster\turtlesongs\song.$number.mp4" -eq $false) {
C:\Users\###\.stacher/youtube-dl --embed-metadata --no-check-certificate --ffmpeg-location C:\Windows\ --download-sections "*$time" -o "C:\Users\###\turtlesongs\song.$number.mp4" $link
$choice = Read-Host "Use ffmpeg and convert to audio? (y/n)"
if ($choice -eq "y") {
ffmpeg -i "C:\Users\###\turtlesongs\song.$number.mp4" -vn -c:a copy "C:\Users\###\turtlesongs\audio\song.$number.m4a"
} else {
write-host "Not converting to audio."
}
} else {
write-host "Song already downloaded, incrementing number by 1."
$number = $number + 1
turtlesong -time $time -link $link -number $number
}
}
At first I had it so the null was checked after the three or’s, copilot said to add a check for each, but neither works, and it continues right past.
>Solution :
Your comparison fails to end early your function because your parameters are typed as string, when no arguments are passed to your function, then these parameters become an empty string and an empty string is not null:
'' -eq $null -or '' -eq $null -or '' -eq $null # is false
However, you could use -not here, negating an empty string becomes true in PowerShell so, this condition would work:
-not '' -or -not '' -or -not '' # is true
But, taking a step back, there is a much better approach to avoid all this trouble, make your parameters Mandatory:
function turtlesong {
param(
[Parameter(Mandatory)]
[string] $time,
[Parameter(Mandatory)]
[string] $link,
[Parameter(Mandatory)]
[string] $number
)
if (-not (Test-Path "C:\Users\aster\turtlesongs\song.$number.mp4")) {
$choice = Read-Host 'Use ffmpeg and convert to audio? (y/n)'
if ($choice -eq 'y') {
ffmpeg -i "C:\Users\###\turtlesongs\song.$number.mp4" -vn -c:a copy "C:\Users\###\turtlesongs\audio\song.$number.m4a"
return
}
Write-Host 'Not converting to audio.'
return
}
Write-Host 'Song already downloaded, incrementing number by 1.'
$number = $number + 1
turtlesong -time $time -link $link -number $number
}
There is also an extra issue here:
if (Test-Path "C:\Users\aster\turtlesongs\song.$number.mp4" -eq $false) {
You must use (...) to wrap the Test-Path expression, otherwise PowerShell thinks that -eq $false are actually arguments to Test-Path (you can also use -not here instead of -eq $false):
if (-not (Test-Path "C:\Users\aster\turtlesongs\song.$number.mp4")) {