Wrapping powershell around ruby

0 votes

I have a mixed environment of Linux and Windows, but build scripts are created in Ruby. I am trying to do some changes on each web server that involves PSRemoting but looks like I have some issue converting string to powershell command:

puts "%x{ powershell.exe invoke-command  -computername #{server} -scriptblock { $oldpath = #{current_iis_site_folder}; $newpath = #{new_site_folder}; Get-ChildItem -Path $oldpath -Recurse | where {($_.FullName -notmatch $oldpath.replace('\\','\\\\')) -or ($_.FullName -notmatch $newpath.replace('\\','\\\\'))}} }.strip.downcase"

%x{ powershell.exe invoke-command  -computername #{server} -scriptblock { $oldpath = #{current_iis_site_folder}; $newpath = #{new_site_folder}; Get-ChildItem -Path $oldpath -Recurse | where {$_.FullName -notmatch "corehr"}} }.strip.downcase

The product of this is:

%x{ powershell.exe invoke-command  -computername iistest.neogov.net -scriptblock { $oldpath = c:\webdata\corehr; $newpath = c:\webdata\corehr\07241439; Get-ChildItem -Path $oldpath -Recurse | where {($_.FullName -notmatch $oldpath.replace('\','\\')) -or ($_.FullName -notmatch $newpath.replace('\','\\'))}} }.strip.downcase

'where-object' is not recognized as an internal or external command,operable program or batch file.

If I replace where-object with just where then error is different:

Missing closing '}' in statement block or type definition. + CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException + FullyQualifiedErrorId : MissingEndCurlyBrace

INFO: Could not find files for the given pattern(s)

I think pipe (|) might be confusing ruby interpreter but not sure how to avoid it?

Jun 17, 2018 in DevOps Tools by Atul
• 10,200 points

1 answer to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes

It might be only part of the Anwser, but for CMD the escape character for | is ^, if use ^| then the output is different but not correct yet (i did change the code somewhat to make it work so you might want to copy and adjust mine), Ruby might use the same or something different i am not sure but i believe youre on the right track.

powershell.exe -command {invoke-command  -scriptblock {$oldpath = "C:\Users\...\corehr"; $newpath = "C:\Users\...\corehr\07241439"; Get-ChildItem -Path $oldpath -Recurse | where {($_.FullName -notmatch $oldpath.replace('\','\\')) -or ($_.FullName -notmatch $newpath.replace('\','\\'))}}}

With CMD Results in

INFO: Could not find files for the given pattern(s).

But it in powershell it gave:

    Directory: C:\Users\...\corehr

Mode                LastWriteTime     Length Name                                                        
----                -------------     ------ ----                                                        
d----         24-7-2017     15:41            test1                                                       
d----         24-7-2017     15:41            test2    

Replacing | with ^| results in

invoke-command -scriptblock {$oldpath = C:\Users\...\corehr; $newpath = C:
\Users\...\corehr\07241439; Get-ChildItem -Path $oldpath -Recurse | where
{($_.FullName -notmatch $oldpath.replace('\','\\')) -or ($_.FullName -notmatch

Now it thinks the -command input is a string? but as you can see, the pipe character is correctly displayed in this output.

I hope that the above information will be helpful for you. 

answered Jun 17, 2018 by shubham
• 6,500 points

Related Questions In DevOps Tools

+13 votes
2 answers
0 votes
1 answer
+6 votes
5 answers

can't remove SSIS variables using remove method on PowerShell

you're missing the additional $environment.Alter() after your ...READ MORE

answered Mar 27, 2018 in DevOps & Agile by ajs3033
• 7,040 points
0 votes
1 answer
0 votes
1 answer

Untrusted Certificates Force Ruby Selenium

Check this out capabilities = Selenium::WebDriver::Remote::W3C::Capabilities.firefox(accept_insecure_certs: true) driver = ...READ MORE

answered May 9, 2018 in Selenium by Samarpit
• 5,130 points
0 votes
1 answer

Opening Tabs using a Watir in Ruby

Selenium doesn't explicitly support opening of tabs, ...READ MORE

answered May 10, 2018 in Selenium by Samarpit
• 5,130 points
0 votes
2 answers

In Azure powershell Select-AzureSubscription command fetching error: the subscription name doesn't exist

The problem you're having is that the ...READ MORE

answered Aug 16, 2018 in Azure by Priyaj
• 56,120 points

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.