Giving UrLs as value to input parameters in a powershell script

Hi, this is my following code with two input parameters,

param(
    [parameter(Mandatory=$true)]
    [ValidateNotNullOrEmpty()]
    [string]
    $textFilePath,
    [parameter(Mandatory=$true)]
    [ValidateNotNullOrEmpty()]
    [string]
    $searchDirectory
)

foreach($FileName in [System.IO.File]::ReadLines($textFilePath)) {
    # if the file could be found
    if(Get-ChildItem -Path $searchDirectory -Filter $Filename -Recurse) {
        return 1 # exit early from the script here
    }
}
return 0 # return is not needed here

i want to input url for the second input parameter, but error occurs as below. Please let me know, what is missing in my code. I have following files listed in the LibraryChecker.SharedEnvironmentLog.txt (specified in the URL)

Middleware.lo.dll
temp.txt

Thanks.

textFilePath: https://github.com/MW/tfsbuildrelease/blob/master/LibraryChecker.SharedEnvironmentLog.txt
searchDirectory: D:\

Exception calling "ReadLines" with "1" argument(s): "The given path's format is not supported."
At \Desktop\test.ps1:23 char:22
+ foreach($FileName in [System.IO.File]::ReadLines($textFilePath)) {
+                      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : NotSupportedException

Before we proceed … When you post code, sample data, console output or error messages please format it as code using the preformatted text button ( </> ). Simply place your cursor on an empty line, click the button and paste your code.

Thanks in advance

How to format code in PowerShell.org <---- Click :point_up_2:t4: :wink:

Please go back, edit your question and fix your formatting.

I have formatted. Thanks

You cannot read a file directly from the internet like this:

[System.IO.File]::ReadLines($textFilePath)

You have to download the file first.

Can you please help me with code to download the file and then read from it?

Thanks

https://www.google.com/search?q=powershell+download+file&oq=powershell+download+file&aqs=chrome..69i57.8707j0j1&sourceid=chrome&ie=UTF-8

1 Like

Hi, I modified code like this, But its throwing error .

param(
    [parameter(Mandatory=$true)]
    [ValidateNotNullOrEmpty()]
    [string]
    $SourceFilePath,
    [string]
    $DestFilePath,
    [parameter(Mandatory=$true)]
    [ValidateNotNullOrEmpty()]
    [string]
    $searchDirectory
)


#if(-not (Test-Path -Path $searchDirectory)){
    #Write-Host "The source folder specified is not valid"
#return

#$SourceFilePath = https://github.bnsf.com/raw/MW/tfsbuildrelease/master/LibraryChecker.SharedEnvironmentLog.txt
#$DestFilePath = c:\LibraryChecker.SharedEnvironmentLog.txt

#Download the file
Invoke-WebRequest -Uri $SourceFilePath -OutFile $DestFilePath

# Get-Content -Path $textFilePath

foreach($FileName in [System.IO.File]::ReadLines($DestFilePath)) {
    # if the file could be found
    if(Get-ChildItem -Path $searchDirectory -Filter $Filename -Recurse) {
        return 1 # exit early from the script here
    }
}
return 0 # return is not needed here
Supply values for the following parameters:
SourceFilePath: https://github.bnsf.com/raw/MW/tfsbuildrelease/master/LibraryChecker.SharedEnvironmentLog.txt
searchDirectory: D:\


StatusCode        : 200
StatusDescription : OK
Content           : Bnsf.Middleware.Log.Elk.Core.dll
                    a.txt

RawContent        : HTTP/1.1 200 OK
                    Content-Security-Policy: default-src 'none'; style-src 'unsafe-inline'; sandbox
                    Strict-Transport-Security: max-age=31536000
                    Vary: Authorization,Accept-Encoding,Origin
                    X-Content-Typ...
Forms             : {}
Headers           : {[Content-Security-Policy, default-src 'none'; style-src 'unsafe-inline'; sandbox], [Strict-Transport-Security, max-age=31536000], [Vary,     
                    Authorization,Accept-Encoding,Origin], [X-Content-Type-Options, nosniff]...}
Images            : {}
InputFields       : {}
Links             : {}
ParsedHtml        : mshtml.HTMLDocumentClass
RawContentLength  : 41

Exception calling "ReadLines" with "1" argument(s): "Empty path name is not legal.
Parameter name: path"
At C:\Users\C990345\Desktop\test.ps1:29 char:22
+ foreach($FileName in [System.IO.File]::ReadLines($DestFilePath)) {
+                      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : ArgumentException
 
0

You should learn how to debug your own code. Please keep in mind we cannot see your screen and we cannot read your mind.

Exception calling “ReadLines” with “1” argument(s): “Empty path name is not legal.
Parameter name: path”

It looks like you did not provide a value for the parameter -DestFilePath

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.