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
Add a CSS Class to <%= F.Submit %>
Rails 3.1 - Changing Default Scaffold Views and Template
Best Way to Handle Dynamic CSS in a Rails App
Why Is This Not a Syntax Error
Difference Between Lambda and -> Operator in Ruby
Why Does a Rails App on Heroku Serve Assets via All.CSS and Locally via Individual Files
Rails Shows "Warning: Can't Verify Csrf Token Authenticity" from a Restkit Post
Undefined Local Variable Based on Syntax in Ruby
Ruby-Rails Serve Ftp File Direct to Client
Removing or Overriding an Activerecord Validation Added by a Superclass or Mixin
Rails Fastercsv "Unquoted Fields Do Not Allow \R or \N"
What Is the Standalone Splat Operator (*) Used for in Ruby
Ruby Pipes: How to Tie the Output of Two Subprocesses Together
How to Merge Consecutive Repeating Elements in an Array
Using Bsearch to Find Index for Inserting New Element into Sorted Array