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,240 points
1,942 views

1 answer to this question.

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
$newpath.replace('\','\\'))}}

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
• 7,340 points

Related Questions In DevOps Tools

+15 votes
2 answers

Git management technique when there are multiple customers and need multiple customization?

Consider this - In 'extended' Git-Flow, (Git-Multi-Flow, ...READ MORE

answered Mar 27, 2018 in DevOps & Agile by DragonLord999
• 8,450 points
3,507 views
+2 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,300 points
1,963 views
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,910 points
1,683 views
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,910 points
1,866 views
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
• 58,090 points
7,008 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP