Home > OS >  Malformed URL powershell spaces in filename
Malformed URL powershell spaces in filename

Time:05-04

I am using the following script to create URLs.

$files1 =Get-ChildItem $ENV:WORKSPACE/Pro_result
$prefix="https://test-jenkins-a-master-black.aws.jhgf.com/job/qa/job/TestDemo_email_PDF/ws/Pro_result/"
foreach ($file in $files1) {
  write-Output ($prefix $file.Name)
 }

However, I am getting a url like this and I have no control over the filename https://test-jenkins-a-master-black.aws.jhgf.com/job/qa/job/TestDemo_email_PDF/ws/Pro_result/W0A-209-193-210_Dealer portal production smoke test_20220427-120342.pdf

It breaks after portal. How do I fix this?

CodePudding user response:

Use the [uri]::EscapeUriString method to escape spaces and similar characters in URI paths:

foreach ($file in $files1) {
  [uri]::EscapeUriString($prefix $file.Name)
}

This will correctly re-encode the spaces as , resulting in a valid URL with no whitespace a la:

https://test-jenkins-a-master-black.aws.jhgf.com/job/qa/job/TestDemo_email_PDF/ws/Pro_result/W0A-209-193-210_Dealer portal production smoke test_20220427-120342.pdf
  • Related