开发者

Convert this JavaScript to PowerShell 2

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();
    tmp开发者_运维技巧line = tmpline.replace(re, args(2));
    dst.WriteLine(tmpline);
}

src.Close();
dst.Close();
FSO.DeleteFile(args(0));
FSO.MoveFile(args(0) + ".tmp", args(0));


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜