简体   繁体   中英

Use variable for URL in powershell BITS transfer

I have the following in a powershell script:

$src_url = "http://my_server/my_file.zip"
if (!(Test-Path $src_zip))
{
    "Downloading $src_url"
    Start-BitsTransfer -Source "$src_url" -Destination .\$src_zip
}

The output of which is

Downloading http://my_server/my_file.zip
Start-BitsTransfer : The server name or address could not be resolved
At C:\foo.ps1:18 char:5
+     Start-BitsTransfer -Source "$src_url" -Destination .\$src_zip
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [Start-BitsTransfer], Exception
    + FullyQualifiedErrorId : StartBitsTransferCOMException,Microsoft.BackgroundIntelligentTransfer.Management.NewBitsTransferComma
   nd

I have tried with, without double and single quotes around the URL. However if I type the URL out directly the name is resolved correctly. What should I do to expand $src_url ?

Considering it works with hard-coding URL, the source variable should expand correctly in your syntax.

And from error you pasted, it may due to destination path resolution. Try to use full path in it. Example,

$src_zip = "C:\temp\abc.zip"

Please see if the destination file needs to be removed or you can use force parameter.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM