netsh result to powershell object

后端 未结 3 835
长发绾君心
长发绾君心 2021-01-26 19:46

I am trying to work with netsh from powershell. I want see result from this command such as object, but netsh return string:

ne         


        
3条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-26 20:09

    I recently wrote a cmdlet to parse arbitrary, multi-line text using regular expressions, called ConvertFrom-Text. (Not a great name, if you ask me, but it conforms to the PowerShell naming rules; suggestions are welcome!) So assuming you have that cmdlet, here is one possible solution to your question. (Caveat emptor! The regular expression given was derived from a very small sample of netsh output, so may need some tuning.)

    $regex = [regex] '(?ms)(?:^\s*$\s*)?^(?
    .*?)\s*-+\s*(?.*?)\s*^\s*$' $result = netsh wlan show hostednetwork | Out-String | ConvertFrom-Text -pattern $regex -multiline $result | % { $dataObj = [PsCustomObject]@{} $_.Data -split "`r`n" | % { $element = $_ -split '\s*:\s*' Add-Member -InputObject $dataObj -MemberType NoteProperty -Name $element[0].Trim() -Value $element[1].Trim() } $_.Data = $dataObj # Replace data text with data object } $result

    On my test system, netsh wlan show hostednetwork returns this:

    Hosted network settings
    -----------------------
        Mode                   : Allowed
        Settings               : 
    
    Hosted network status
    ---------------------
        Status                 : Not available
    

    And the output of the $result variable in the code above yields this:

    section                   data
    -------                   ----
    Hosted network settings   @{Mode=Allowed; Settings=}
    Hosted network status     @{Status=Not available}
    

    So $result is an array of objects with section and data properties, and the latter is an object with properties defined by the output of the netsh command.


    Of course, the above does not get you very far without the ConvertFrom-Text cmdlet. So here is the implementation. (I have copious documentation and examples for it, which will be publicly available once I eventually add it to my open-source PowerShell library.)

    filter ConvertFrom-Text
    {
        [CmdletBinding()]
        Param (
        [Parameter(Mandatory=$true,Position=0, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)]
        [string[]]$InputObject,
    
        [Parameter(Mandatory=$true,Position=1)]
        [regex]$Pattern,
    
        [switch]$RequireAll,
        [switch]$Multiline
        )
    
        if ($Multiline) {
            $dataString = $InputObject -join "`n"
            IterateByMatch $dataString $Pattern
        }
        else {
            IterateByLine $InputObject $Pattern
        }
    }
    
    function IterateByLine([string[]]$data, [regex]$regex)
    {
        $data | ForEach-Object {
            if ($PSItem -match $regex)
            {
                New-Object PSObject -Property (GetRegexNamedGroups $matches)
            }    
            elseif ($RequireAll) {
                throw "invalid line: $_"
            }
        }
    }  
    
    function IterateByMatch([string[]]$data, [regex]$regex)
    {
      $regex.matches($data) | Foreach-Object {
        $match = $_
        $obj = new-object object
        $regex.GetGroupNames() |
        Where-Object {$_ -notmatch '^\d+$'} |
        Foreach-Object {
          Add-Member -InputObject $obj NoteProperty `
              $_ $match.groups[$regex.GroupNumberFromName($_)].value
        }
        $obj
      }
    }
    
    function Get-RegexNamedGroups($hash)
    {
        $newHash = @{};
        $hash.keys | ? { $_ -notmatch '^\d+$' } | % { $newHash[$_] = $hash[$_] }
        $newHash 
    } 
    

提交回复
热议问题