1

I have a Powershell script that is called from the command line.

script.ps1 "\\testfolder" "testinput" "xml" "xml2html.xsl" "testfile" "css"

The script uses these command line arguments:

param([string]$publish_folder, [string]$input_filename, [string]$input_ext, [string]$transformation_filename, [string]$output_filename, [string]$output_ext)

$input_filename and $output_filename may be a full path+filename, the filename only or the filename without extension.

$inputFileNameOnly = [System.IO.Path]::GetFileNameWithoutExtension($input_filename)
$inputPath=$publish_folder+"\"+$inputFileNameOnly+"."+$input_ext 
$outputFileNameOnly = [System.IO.Path]::GetFileNameWithoutExtension($output_filename) 
$outputPath=$publish_folder+"\"+$outputFileNameOnly+"."+$output_ext

When I run this locally, it works. Output path:

\\testfolder\testfile.css

When I run the same script in an AWS instance, it fails. $inputpath is calculated correctly, but Output path becomes:

\\testfolder\.

so both $output_filename and $output_ext are empty.

The paths are longer than \\testfolder\, but not long enough to cause trouble (about 150 characters). Total length of the arguments doesn't seem to be a problem either.

What could be causing this problem?

Hobbes
  • 1,964
  • 3
  • 18
  • 35
  • 1
    Different powershell versions? – Paolo Dec 23 '21 at 13:39
  • 1
    `$outputFileNameOnly` and `$output_ext` are empty, you need to track back their origin. – Vasil Nikolov Dec 23 '21 at 14:00
  • @VasilNikolov I've confirmed $outputFileNameOnly and $output_ext are present in the command line arguments. I've updated the script to log the PS version and report back all of the arguments. Now I have to wait for a colleague to run the script in the AWS instance... – Hobbes Dec 23 '21 at 14:05

3 Answers3

0
$outputPath="$publish_folder+"\"+

Looks like you broke your concatenation here. Double-quote before $publish_folder.

Edit: Can also be written like this if you don't want to concatenate (+)

$outputPath="$publish_folder\$outputFileNameOnly.$output_ext"

Anything in double-quotes should be expanded correctly.

tanstaafl
  • 190
  • 5
  • sorry, that was a typo I added while editing the question, the " before $publish_folder shouldn't be there. Modified the question. – Hobbes Dec 23 '21 at 14:25
0

I set up your script as a function and passed those parameters.

function rename {
    param(
        [string]$publish_folder, 
        [string]$input_filename,
        [string]$input_ext, 
        [string]$transformation_filename,
        [string]$output_filename, 
        [string]$output_ext
    )

    $inputFileNameOnly = [System.IO.Path]::GetFileNameWithoutExtension($input_filename)
    $inputPath = $publish_folder+"\"+$inputFileNameOnly+"."+$input_ext 
    $outputFileNameOnly = [System.IO.Path]::GetFileNameWithoutExtension($output_filename) 
    $outputPath = $publish_folder+"\"+$outputFileNameOnly+"."+$output_ext
    return $outputPath
}

Output is this...
\\\\testfolder\\\\testfile.css

Try passing $publish_folder without ending backslash

Edit: Sorry about formatting. Need some forum practice. =)

Santiago Squarzon
  • 41,465
  • 5
  • 14
  • 37
tanstaafl
  • 190
  • 5
  • Checked the code, the path is being passed without a backslash. If `$publish_folder` was causing the problem, I'd also have wrong values in `$inputFileNameOnly` – Hobbes Dec 23 '21 at 14:45
  • AWS could be handling [System.IO.Path] differently. Could try a Split-Path/Replace and see if you get different results. $inputFileNameOnly = (Split-Path $input_filename -leaf) -replace "\.$input_ext$" – tanstaafl Dec 23 '21 at 15:00
  • 1
    This [Q&A](https://stackoverflow.com/questions/70254096/system-io-fileinfo-and-relative-paths) can help you deal with `UNC` and `IO.FileInfo` as input parameters :) – Santiago Squarzon Dec 23 '21 at 15:10
0

With some more testing, we found the cause of the problem.

$outputFileNameOnly = [System.IO.Path]::GetFileNameWithoutExtension($output_filename)

GetFileNameWithoutExtension fails (in the AWS instance) when $output_filename does not contain an extension. On my local machine this worked correctly.

So we added an extension to the command line argument and the script works correctly.

Hobbes
  • 1,964
  • 3
  • 18
  • 35