views:

107

answers:

2

I am trying to execute a powershell script from Ruby, I have entered the below command:

scriptPath =  system('powershell \"C:\\Scripts\\DB_Setup.ps1\"')

The ruby Script is handling exceptions when an error is raised to stop the script as below command:

rescue => ex
  message = "\nscript on server '#{`hostname`.strip()}' terminated unexpectedly:\n\nMessage: '#{ex.message}"
raise ex

Output Error: script on server 'TestDB1' terminated unexpectedly: Message: 'can't convert true into String'

Thanks

A: 

Impossible to tell without seeing your script. Looks like your .ps1 script echos out a Boolean object, and Ruby isn't liking it. Either silence it, or change it to a string:

PS C:\> (1 -eq 1).gettype().name
Boolean
PS C:\> (1 -eq 1).tostring().gettype().name
String
PS C:\>
Marco Shaw
A: 

Thanks for your quick reply, I know that it is pretty easy to run a powershell script from ruby as mentioned in this link http://www.hanselman.com/blog/DNRTVScreencastPowershellIsStillShiny.aspx , but for some reason I kept getting the same error message even though running a very basic powershell script such as below:

function Main()
{
  Write-Host "Servername"
}

. Main

Plus on the otherside I tried your above solution.

This is very strange issue, after spending so much time on it I did a workaround to run a bat file from ruby kicking off the powershell script, I know this is not a good practice...

Ozie Harb