hack のためのネタ帳, etc,,,

経緯

メモし忘れてたので、何処で見かけたのか忘れてしまったのだが、
ちょっと前に、PowerShell で grep 相当の処理する方法について調べてた時に、
ls | sls something
すると Object に対して Select-String するので、
ls | Out-String -Stream | sls somethins
とすると、パイプしないときに表示されるテキストに対して Select-String 出来るという話を見かけた。

それは頭の隅っこに残ってたんだが、今日それを使おうとしたら Out-String ってコマンド名は失念してしまっていて、幸い「Out-なんてら」ってところまではうろ覚えしてたので、Out-[Tab] でどうにかできたもののオプションに -Stream も付けるところまで思い出せなくて、結局ググる羽目に。

そんなわけで、「PowerShell grep selec-tstring out-string」でググったところ以下のページを見つけた。 そこにこんなツイートが


どうも、oss で Out-String -Stream 相当の処理をしてくれるらしい。

こんな感じ。
> ls


    ディレクトリ: C:\Users\kou\Desktop\tmp\svg


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----       2019/03/15     16:59           1716 a.svg

> ls | sls svg

a.svg:4:<svg
a.svg:8:   xmlns:svg="http://www.w3.org/2000/svg"
a.svg:9:   xmlns="http://www.w3.org/2000/svg"
a.svg:16:   id="svg8">
a.svg:41:        <dc:format>image/svg+xml</dc:format>
a.svg:58:</svg>

> ls | oss | sls svg

    ディレクトリ: C:\Users\kou\Desktop\tmp\svg
-a----       2019/03/15     16:59           1716 a.svg

> ls | Out-String -Stream | sls svg

    ディレクトリ: C:\Users\kou\Desktop\tmp\svg
-a----       2019/03/15     16:59           1716 a.svg



で、alias なら
alias |? {$_.Name -eq"ls"}
みたいにすれば引けるわけだけど、関数って話なので、これはどうやったら確認出来んだよって話。

そこで「PowerShell function 一覧」でググって以下のページを見つけた。 曰く、以下のようにすれば OK との事。
Get-ChildItem -path function:

覗いてみたところ、こんな感じだった。
> ls -pa function:

CommandType     Name                                               Version    Source
-----------     ----                                               -------    ------
Function        A:
Function        B:
Function        C:
Function        cd..
Function        cd\
Function        Clear-Host
Function        ConvertFrom-SddlString                             3.1.0.0    Microsoft.PowerShell.Utility
Function        D:
Function        E:
Function        F:
Function        Format-Hex                                         3.1.0.0    Microsoft.PowerShell.Utility
Function        G:
Function        Get-FileHash                                       3.1.0.0    Microsoft.PowerShell.Utility
Function        Get-Verb
Function        H:
Function        help
Function        I:
Function        Import-PowerShellDataFile                          3.1.0.0    Microsoft.PowerShell.Utility
Function        ImportSystemModules
Function        J:
Function        K:
Function        L:
Function        M:
Function        mkdir
Function        more
Function        N:
Function        New-Guid                                           3.1.0.0    Microsoft.PowerShell.Utility
Function        New-TemporaryFile                                  3.1.0.0    Microsoft.PowerShell.Utility
Function        O:
Function        oss
Function        P:
Function        Pause
Function        prompt
Function        PSConsoleHostReadLine                              2.0.0      PSReadline
Function        Q:
Function        R:
Function        S:
Function        T:
Function        TabExpansion2
Function        U:
Function        V:
Function        W:
Function        X:
Function        Y:
Function        Z:
> ls -pa function: |? {$_.Name -eq "oss"}

CommandType     Name                                               Version    Source
-----------     ----                                               -------    ------
Function        oss
> ls -pa function: |? {$_.Name -eq "oss"} | member


   TypeName: System.Management.Automation.FunctionInfo

Name                MemberType     Definition
----                ----------     ----------
Equals              Method         bool Equals(System.Object obj)
GetHashCode         Method         int GetHashCode()
GetType             Method         type GetType()
ResolveParameter    Method         System.Management.Automation.ParameterMetadata ResolveParameter(string name)
ToString            Method         string ToString()
PSDrive             NoteProperty   PSDriveInfo PSDrive=Function
PSIsContainer       NoteProperty   bool PSIsContainer=False
PSPath              NoteProperty   string PSPath=Microsoft.PowerShell.Core\Function::oss
PSProvider          NoteProperty   ProviderInfo PSProvider=Microsoft.PowerShell.Core\Function
CmdletBinding       Property       bool CmdletBinding {get;}
CommandType         Property       System.Management.Automation.CommandTypes CommandType {get;}
DefaultParameterSet Property       string DefaultParameterSet {get;}
Definition          Property       string Definition {get;}
Description         Property       string Description {get;set;}
HelpFile            Property       string HelpFile {get;}
Module              Property       psmoduleinfo Module {get;}
ModuleName          Property       string ModuleName {get;}
Name                Property       string Name {get;}
Noun                Property       string Noun {get;}
Options             Property       System.Management.Automation.ScopedItemOptions Options {get;set;}
OutputType          Property       System.Collections.ObjectModel.ReadOnlyCollection[System.Management.Automation.PS...
Parameters          Property       System.Collections.Generic.Dictionary[string,System.Management.Automation.Paramet...
ParameterSets       Property       System.Collections.ObjectModel.ReadOnlyCollection[System.Management.Automation.Co...
RemotingCapability  Property       System.Management.Automation.RemotingCapability RemotingCapability {get;}
ScriptBlock         Property       scriptblock ScriptBlock {get;}
Source              Property       string Source {get;}
Verb                Property       string Verb {get;}
Version             Property       version Version {get;}
Visibility          Property       System.Management.Automation.SessionStateEntryVisibility Visibility {get;set;}
HelpUri             ScriptProperty System.Object HelpUri {get=$oldProgressPreference = $ProgressPreference...
> ls -pa function: |? {$_.Name -eq "oss"} |% {$_.ScriptBlock}

[CmdletBinding()]
param(
    [ValidateRange(2, 2147483647)]
    [int]
    ${Width},

    [Parameter(ValueFromPipeline=$true)]
    [psobject]
    ${InputObject})

begin
{
    try {
        $PSBoundParameters['Stream'] = $true
        $wrappedCmd = $ExecutionContext.InvokeCommand.GetCommand('Out-String',[System.Management.Automation.CommandType
s]::Cmdlet)
        $scriptCmd = {& $wrappedCmd @PSBoundParameters }
        $steppablePipeline = $scriptCmd.GetSteppablePipeline($myInvocation.CommandOrigin)
        $steppablePipeline.Begin($PSCmdlet)
    } catch {
        throw
    }
}

process
{
    try {
        $steppablePipeline.Process($_)
    } catch {
        throw
    }
}

end
{
    try {
        $steppablePipeline.End()
    } catch {
        throw
    }
}
<#
.ForwardHelpTargetName Out-String
.ForwardHelpCategory Cmdlet
#>
単純な wrapper なのかと思いきや、割と面倒な手順踏んでてびっくり。

えっと、それよりもびっくりだったのが、コマンドプロンプト互換の C: とかでドライブ移動するやつが関数だったって事。
> ls -pa function: |? {$_.Name -eq "C:"} |% {$_.ScriptBlock}
Set-Location $MyInvocation.MyCommand.Name
ほぉ、そう来たか。

関連

コメントをかく


「http://」を含む投稿は禁止されています。

利用規約をご確認のうえご記入下さい

Wiki内検索

フリーエリア

管理人/副管理人のみ編集できます