如何使用Powershell脚本将本地变量写入远程服务器中的文件?

时间:2018-02-28 14:36:29

标签: powershell

在远程服务器上有一个.BAT文件,它使用.properties文件来运行。

我能够运行.BAT文件调用.properties文件,但在.properties文件的最后一行是:

exportQuery1=SELECT * FROM CI_INFOOBJECTS where SI_ID='123456'. 

我正在手动修改该行/ SI_ID值,这实际上增加了我的努力。

我尝试了一些选项,但无法从本地powershell命令行提供值/整行,该命令行将写入.properties文件。

所以我每次都要修改.ps1。我想将带有本地powershell命令的条目作为变量传递。

删除旧行:

Invoke-Command -computername $ServerName -Credential $Cred -ErrorAction stop -ScriptBlock {Set-Content -Path D:\Script\TestFile.txt -Value (get-content -Path D:\Script\TestFile.txt | Select-String -Pattern 'SI_ID' -NotMatch)}

在文件末尾创建新行:

Invoke-Command -computername $ServerName -Credential $Cred -ErrorAction stop -ScriptBlock {add-content D:\Script\TestFile.txt "exportQuery1=SELECT * FROM CI_INFOOBJECTS where SI_ID='abcdef'"}

执行脚本时,请帮助从命令传递SI_ID /整行。

1 个答案:

答案 0 :(得分:0)

为什么不在单个调用调用中使用简单参数和using语句?

param($SI_ID)

$SB = {
    Set-Content -Path D:\Script\TestFile.txt -Value (get-content -Path D:\Script\TestFile.txt | Select-String -Pattern 'SI_ID' -NotMatch)
    add-content D:\Script\TestFile.txt "exportQuery1=SELECT * FROM CI_INFOOBJECTS where SI_ID='$using:SI_ID'"
}

Invoke-Command -computername $ServerName -Credential $Cred -ErrorAction stop -ScriptBlock $SB

然后只是.\myscript -SI_ID "abcd"

相关问题