【问题标题】:Wrapping powershell around ruby将 powershell 包裹在 ruby​​ 周围
【发布时间】:2017-07-24 13:11:15
【问题描述】:

我有一个 Linux 和 Windows 的混合环境,但是构建脚本是用 Ruby 创建的。我正在尝试在涉及 PSRemoting 的每个 Web 服务器上进行一些更改,但看起来我在将字符串转换为 powershell 命令时遇到了一些问题:

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

这个产品是:

%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' 不是内部或外部命令、可运行程序或批处理文件。

如果我只用where 替换where-object,那么错误会有所不同:

语句块或类型定义中缺少结束“}”。 + CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException + FullyQualifiedErrorId : MissingEndCurlyBrace

信息:找不到给定模式的文件

我认为管道 (|) 可能会让 ruby​​ 解释器感到困惑,但不知道如何避免?

【问题讨论】:

    标签: ruby powershell devops


    【解决方案1】:

    它可能只是 Anwser 的一部分,但对于 CMD 来说,| 的转义字符是 ^,如果使用 ^|然后输出不同但还不正确(我确实对代码进行了一些更改以使其工作,因此您可能想要复制和调整我的),Ruby 可能使用相同或不同的东西我不确定但我相信你是对的跟踪。

    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('\','\\'))}}}
    

    CMD 结果在

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

    但它在 powershell 中给出了:

        Directory: C:\Users\...\corehr
    
    
    Mode                LastWriteTime     Length Name                                                        
    ----                -------------     ------ ----                                                        
    d----         24-7-2017     15:41            test1                                                       
    d----         24-7-2017     15:41            test2    
    

    更换 |与^|结果

    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('\','\\'))}}
    

    现在它认为 -command 输入是一个字符串?但如您所见,管道字符在此输出中正确显示。

    【讨论】:

    • 是的!谢谢,所以 ^ 是 | 的转义键... 令人沮丧的是,这些小事情占用了这么多时间 :(
    猜你喜欢
    • 2015-05-09
    • 1970-01-01
    • 2010-11-08
    • 2013-01-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多