Call Ruby Script from Powershell

Call ruby script from powershell

First, $args is an automatic variable managed by PowerShell, so I would avoid trying to declare your own variable with the same name.

To invoke a command stored in a variable from PowerShell, you can use the call operator &:

& $ruby 'script.rb'

If you need to build up the list of arguments, I would recommend creating an array, instead of mashing them all together into a single string:

$rubyArgs = @('C:\files\script.rb', $Arg1, $Arg2, $Arg3)
& $ruby $rubyArgs

If you need to pass more complicated arguments, you may find this answer useful.

execute powershell commands from ruby

I have tested this now:

require 'base64'
cmd = %{Get-WmiObject -Class Win32_Product -ComputerName . -Filter "Name='Qlik Sense Client'"|Select-Object -Property version}
encoded_cmd = Base64.strict_encode64(cmd.encode('utf-16le'))
find = `powershell.exe -encodedCommand #{encoded_cmd}`

Powershell expects UTF-16LE-encoded strings, so you have to convert from Ruby's encoding (UTF-8) before the base64 conversion.


Alternatively you could try using shellescape from shellwords to escape the command so the shell interprets it as a single string.

Another alternative is to use powershell.exe -Command - with popen3. That will let you write your commands and read their results using a file stream.

How can I execute Ruby scripts and irb from Windows7 Powershell?

Is the directory that Windows Ruby Installer installed to (like C:\Ruby192\bin) on your path? I can't recall if it should add that automatically on install but I've not had any issues running irb or scripts from powershell. You can check if it's on your path by running the following from a powershell session:

$env:path

Use Ruby variable when calling exec powershell.exe in script

In order to interpolate a value in a string, you need to use double quote.

exec "Powershell.exe write-host #{mydata}"

instead of

exec 'Powershell.exe write-host #{mydata}'


Related Topics



Leave a reply



Submit