将编码内容写入.ps1文件

时间:2017-05-09 16:54:59

标签: ruby powershell

我正在尝试创建一个文件.ps1并使用ruby编写内容。但是我收到错误。下面是代码

output = File.open( "test.ps1", "w" )
  output << "$a = type c://edmundsapi-final ;(Get-Content C:\inetpub\sites\tsd_atlasapi\AppSettings.config) -replace '(?<=<add key="Edmunds" value=")[^"]*', $a | Set-Content C:\inetpub\sites\tsd_atlasapi\AppSettings.config "
 output.close

1 个答案:

答案 0 :(得分:0)

语法是:

File.open("test.ps1", w) {|f| f.write("$a = type c://edmundsapi-final ;(Get-Content C:\inetpub\sites\tsd_atlasapi\AppSettings.config) -replace '(?<=<add key="Edmunds" value=")[^"]*', $a | Set-Content C:\inetpub\sites\tsd_atlasapi\AppSettings.config ") } 

out_file = File.new("test.ps1", "w") 
out_file.puts("$a = type c://edmundsapi-final ;(Get-Content C:\inetpub\sites\tsd_atlasapi\AppSettings.config) -replace '(?<=<add key="Edmunds" value=")[^"]*', $a | Set-Content C:\inetpub\sites\tsd_atlasapi\AppSettings.config ")

out_file.close

参见&#34; How to create a file in Ruby&#34;可能重复。