Strange character error when running native cmdline app

Howdy,

I’ve been trying to produce just a simple script (pasted at bottom) which will run a native cmdline application (psftp) and which that native app will run using its own command script - a script also created by the powershell script. However I’m having a lot of difficulty with this and I don’t understand why. I even put the native app and args in its own batch script (rsynctemp.bat) and had Powershell run that instead, to no avail. Every time I run the PS script it errors with the following:

PS C:\Scripts> Download-Log Test

C:\Scripts>þp
C:\Users\ThisUser\rsynctemp.bat : 'þp' is not recognized as an internal or external command,
At line:1 char:1
+ C:\Users\ThisUser\rsynctemp.bat
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: ('þp' is not rec...ternal command,:String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError
 
operable program or batch file.

It looks like when it is trying to execute the batch script (or the command by itself) Powershell is feeding it erroneous characters. Unless I missing some here-string gotchas that I’m not aware of, I don’t really see anything particularly wrong with them.

What’s particularly interesting is that I can pop open the Powershell shell and test out the native app by running the rsynctemp.bat straight from the prompt and it’ll work great. It’s only when trying to do it from this script does it bug out.

Script:

function Download-Log
{
    [CmdletBinding()]
    [OutputType([int])]
    Param
    (
        # Param1 help description
        [Parameter(Mandatory=$true,
                   ValueFromPipelineByPropertyName=$true,
                   Position=0)]
        [string[]]$Device,

        [Parameter(Mandatory=$false,
                    ValueFromPipelineByPropertyName=$true)]
        [string]$Directory="$ENV:USERPROFILE\My Documents\Logs\"
        
    )

    Begin
    {
        
        #Filenames for the scripts
        $TempCmdFile = "$ENV:USERPROFILE\rsync.tmp"
        $TempBatFile = "$ENV:USERPROFILE\rsynctemp.bat"

        #DO NOT INDENT HERE-STRING CONTENTS/TERMINATOR
        #Create command script for psftp
        $TempCmd = @"
lcd "$Directory"
mget -r $($Device.ToUpper())
"@
        
        #Create batch script to run psftp command
        $TempBat = @"
psftp -pw APassword AServer -batch -bc -b $TempCmdFile
"@
        
        #Slap the scripts into temporary files
        Out-File -FilePath $TempCmdFile -Force -InputObject $($TempCmd -join ' ') #replace newlines w/ spaces
        Out-File -FilePath $TempBatFile -Force -InputObject $TempBat

    }
    Process
    {
        #$ErrorActionPreference = "SilentlyContinue"
        Invoke-Expression -Command $TempBatFile
        #$ErrorActionPreference = "Stop"
    }
    End
    {
        #Open window to logs directory
        explorer $Directory

        #Delete temp files
        Remove-Item $TempCmdFile
        Remove-Item $TempBatFile
    }
}

Most command-line utilities don’t handle Unicode, and that’s the default encoding used by Out-File. Try adding -Encoding Ascii to all of those calls to Out-File.

Wonderful. I totally did not recognize the Unicode output. Thank you!