0

Am automating youtubeDl tasks with Powershell via some functions in profile.ps1 This function accepts different parameters - if some are set, i'll setup additional config to youtube-dl executable.

For instance, executing ytDl https://vimeo.com/1548390390 would result in executing .\youtube-dl.exe https://vimeo.com/1548390390. When executing ytDl https://vimeo.com/1548390390 3458F89 would lead to .\youtube-dl.exe https://vimeo.com/1548390390 --video-password 3458F89

Am able to do what i want working script is not "beautiful"/"clean". I pasted below the two versions.

Can anyone of you help me - explain me why it's not behaving as expected ? Thanks a lot !

function ytDl_Working_But_NotBeautiful {
    param (
        [Parameter(Mandatory = $true, Position = 0)][string]$url,
        [Parameter(Mandatory = $false, Position = 1)][string]$password,
        [Parameter(Mandatory = $false, Position = 2)][string]$youtubeDlPath = "C:\APPS\"
    )
        Write-Host "URL = $url || Password = $password"
        
        cd $youtubeDlPath
        
        if ($password.Length -gt 0) {
            Write-Host "Password set - adding video-password parameter"
            .\youtube-dl.exe $url --video-password $password
        }
        else{
            .\youtube-dl.exe $url
        }
}

function ytDl_NotWorking {
    param (
        [Parameter(Mandatory = $true, Position = 0)][string]$url,
        [Parameter(Mandatory = $false, Position = 1)][string]$password,
        [Parameter(Mandatory = $false, Position = 2)][string]$youtubeDlPath = "C:\APPS\"
    )
        Write-Host "URL = $url || Password = $password"
        
        $fullParams = $url
        if ($password.Length -gt 0) {
            Write-Host "Password set - adding video-password parameter"
            $fullParams = "$url --video-password $password"
        }
        Write-Host ".\youtube-dl.exe $fullParams"
        
        .\youtube-dl.exe $fullParams
}
4
  • 1
    You forgot to tell us what exactly you are expecting and how these functions defy your expectations. Commented Nov 5, 2020 at 20:14
  • Updated description, thanks for highlighting it ! Commented Nov 5, 2020 at 20:32
  • And what is it that's "not working"? Commented Nov 5, 2020 at 20:35
  • When i launch ytDl with some parameters, it works (sets properly the extra parameter) ytDl https://vimeo.com/473689210 2345no [vimeo] 473689210: Downloading webpage [vimeo] 473689210: Extracting information [vimeo] 473689210: Verifying the password When launching ytDl2 with same param; it fails ytDl2 https://vimeo.com/473689210 2345no Password set - adding video-password parameter [vimeo:user] 473689210 --video-password 2345no: Downloading page 1 Which means parameters are not properly set. Thus this question :) Commented Nov 5, 2020 at 20:44

1 Answer 1

2

If you want to pass a variable number of arguments to an executable without having to write many nested if/else statements, take advantage of splatting!

function Save-OnlineVideo
{
    param (
        [Parameter(Mandatory = $true, Position = 0)][string]$url,
        [Parameter(Mandatory = $false, Position = 1)][string]$password,
        [Parameter(Mandatory = $false, Position = 2)][string]$youtubeDlPath = "C:\APPS\"
    )

    # Create array to hold the arguments
    $fullParams = @($url)

    if ($password.Length -gt 0) {
        # Add any additional arguments to the array
        $fullParams += "--video-password","$password"
    }

    # prepend variable name with @ instead of $ to make powershell splat them
    .\youtube-dl.exe @fullParams
}
Sign up to request clarification or add additional context in comments.

3 Comments

Unfortunately, it is not working. Usage: youtube-dl.exe [OPTIONS] URL [URL...] youtube-dl.exe: error: no such option: --video-password PASSWORD My understanding is that we are setting one parameter ( --video-password PASSWORD) whilst we should be setting a -- --video-password parameter which value is PASSWORD.
@FranckDeny Updated the answer
BTW - thanks to your splatting explanation, i now understand many scripts i've seen before :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.