0

It throws errors in PowerShell 2, can someone help me convert to PS2 please?

var args = WScript.Arguments
var FSO = new ActiveXObject("Scripting.FileSystemObject");
var src = FSO.OpenTextFile(args(0));
var dst = FSO.CreateTextFile(args(0) + ".tmp");
var tmpline;
var re = new RegExp("%" + args(1) + "%","ig");

while(!src.AtEndOfStream)
{
    tmpline = src.ReadLine();
    tmpline = tmpline.replace(re, args(2));
    dst.WriteLine(tmpline);
}

src.Close();
dst.Close();
FSO.DeleteFile(args(0));
FSO.MoveFile(args(0) + ".tmp", args(0));
1
  • What is your PowerShell code so far? Commented Nov 18, 2010 at 12:55

1 Answer 1

1

What about

Get-Content $args[0] `
  | ForEach-Object { $_ -replace "%$($args[1])%", $args[2] } `
  | Out-File ($args[0] + '.tmp')
Move-Item ($args[0] + '.tmp') $args[0] -Force

provided I understood your code correctly. You may need to add the -Encoding parameter with a suitable argument to Out-File, though.

Sign up to request clarification or add additional context in comments.

3 Comments

how is that ? I am very new to this.
I need to run Install-ClamAgent.ps1 , in the same directory is that JavaScript.js file but when run in PW V2.0 it throws a bunch of errors.
PowerShell tells you where to look it up, if you try executing a script. In any case: Set-ExecutionPolicy RemoteSigned would be a start.

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.