Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to tell if a variable was passed in Poweshell

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 :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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")) {
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading