3

Fairly new to scripting. I have a very basic script which works fine within the ISE but when I run it within a file it doesn't. Script:

#
# WPM Convert to Ascii.ps1
# Process to remove accented characters from a text file as they cause issues when importing to U4BW via GL07
# SP Jan 2019
#
# Parameters
#
$usefile    =$dir+"\"+'SPTEMP.txt'
$outfile    =$dir+"\"+'SPOUT.txt'

#
# Convert characters
#
Get-Content $usefile -replace 'a', 'A' |Set-Content $outfile 

Simply converting characters in 1 file outputting to another. Called from U4BW(Agresso) command being:-

powershell.exe -ExecutionPolicy Unrestricted -File "c:\scripts\WPM Convert to Ascii.ps1" -infile "[File name]" -dir "[Directory]"

I have debugged all the parameters sent (infile and dir) and they are fine. Tried closing the file (outfile) beforehand.

I know this is probably a basic issue but I just can't see it. Any help gratefully received! Steve

3
  • How does it fail? Can you provide sample data and show what you expect to happen vs. what actually does happen? Commented Jan 22, 2019 at 13:00
  • What is the -infile parameter for? It is never used. Commented Jan 22, 2019 at 13:06
  • Thanks folks. infile is used elsewhere - I took out bits of the script that would just confuse plus the param line is actually there in my script. Commented Jan 22, 2019 at 14:15

2 Answers 2

1

You need to make the following changes to your script.

Declare this at the top of your script so the -dir parameter in your call is actually recognized in the script:

param($dir)

Also, your replace command looks wrong, -Replace is not a valid parameter of Get-Content. You probably meant this?

(Get-Content $usefile) -replace 'a', 'A' | Set-Content $outfile

Final script (with some other minor improvements):

# WPM Convert to Ascii.ps1
# Process to remove accented characters from a text file as they cause issues when importing to U4BW via GL07
# SP Jan 2019

# Passed parameters
param (
    # The base directory path
    $dir
)

# Derived parameters
$usefile = Join-Path $dir "SPTEMP.txt"
$outfile = Join-Path $dir "SPOUT.txt"

# Replace characters
(Get-Content $usefile) -replace 'a', 'A' | Set-Content $outfile 

Then call it like this:

powershell.exe -ExecutionPolicy Unrestricted -File "c:\scripts\WPM Convert to Ascii.ps1" -dir "[Directory]"
Sign up to request clarification or add additional context in comments.

Comments

0

I agree with what was said above, "replace" is not a parameter for get-content.

# WPM Convert to Ascii.ps1
# Process to remove accented characters from a text file as they cause issues when importing to U4BW via GL07
# SP Jan 2019
#

Param
(
    [Parameter(Mandatory=$true)]
    [string]$usefile,

    [Parameter(Mandatory=$true)]
    [string]$outfile,

    [Parameter(Mandatory=$false)]
    $dir = $(Get-Location)
)


#
# Convert characters
#
(Get-Content $usefile) -replace 'a', 'A' |Set-Content $dir/$outfile 

1 Comment

Thanks marsze/ConorT. Will try that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.