Battle Faction Puzzle 4

This topic contains 10 replies, has 4 voices, and was last updated by  Yubu 1 week, 4 days ago.

  • Author
    Posts
  • #92928

    Axel Bøg Andersen
    Participant

    Import the module in the gist and run commands like this:
    Get-LegacyCommandObject "arp -a"
    Get-LegacyCommandObject "netstat -an"

    Works on 5.1 and 6.0

  • #92947

    js
    Participant

    Well, there is Get-NetTCPConnection (netstat) and Get-NetNeighbor (arp) from the NetTCPIP module in 5.1 but not 6.0...

  • #92955

    Richard Siddaway
    Moderator

    I'd missed get-netNeighbor so good call on that. Its output and get-nettcpconnection are sufficiently different from the legacy commands that the exercise is worth doing. Also, it brings out the approach to dealing with legacy command line tools which was the object of the exercise.

  • #92965

    Yubu
    Participant

    Works on 5.1

    function Get-NetSt {
    [CmdletBinding()]
    Param ([Parameter(Mandatory=$True,Position=0)][ValidateSet("netstat","arp")]$cmd )
    
    if ($cmd -match "netstat") {
    	$res=(netstat -ano).trim() | select -skip 4 
    	($res | sls tcp | ConvertFrom-String -PropertyNames Protocol,LocalAddress,RemoteAddress,State,Process) + ($res | sls udp | ConvertFrom-String -PropertyNames Protocol,LocalAddress,remoteAddress,Process) | select *,@{n='ProcessPath';e={(gps -pid $_.process).path}}
    } 
     
    if ($cmd -match "arp") {
    	$res=(arp -a).trim() | ?{$_ -NotMatch 'internet'} | %{if ($_ -match ':') {$int=(($_).replace(' ',''))} elseif (($_ -match '^$')){} else {("$int ")+$_}}
    	$res | ConvertFrom-String -PropertyNames Interface,InternetAddress,HardwareAddress,Type}
    }
    
  • #93030

    js
    Participant

    There's actually a nice blog post that talks about this too, and the examples are network-related.

    https://social.technet.microsoft.com/wiki/contents/articles/4244.how-to-convert-text-output-of-a-legacy-console-application-to-powershell-objects.aspx

  • #93210

    Yubu
    Participant

    LoL.
    I would say this:
    1. Those blogs are also good, or, possibly, better imho:

    http://codegists.com/code/powershell-netstat-wrapper/
    2. Lots of people recorded Beethoven's "Eroica". Does it mean, that nobody else should do this?
    3. I learned something new... From posts, from Axel's code, and from yours *Neighbor* stuff.
    4. All above == win/win == fun + knowledge.
    5. Jeffrey, Bruce and the team are Beethoven, and we're just performers... In our special way 😉

  • #93219

    js
    Participant

    When I try convertfrom-string, it doesn't work for me. It's not in Powershell 6 btw.

    $template = @'
    {ipv4address:198.6.1.3},{subnetmask:255.255.255.128},{defaultgateway:198.6.1.1}
    '@
    
    $text = @'
       IPv4 Address. . . . . . . . . . . : 198.6.1.3
       Subnet Mask . . . . . . . . . . . : 255.255.255.128
       Default Gateway . . . . . . . . . : 198.6.1.1
    '@
    
    $text | ConvertFrom-String -TemplateContent $template
    
    defaultgateway
    --------------
    198.6.1.1
    
  • #93240

    Yubu
    Participant

    Yes. It's not working in 6.0, because there is no ConvertFrom-String there. The first line of the post is "Works on 5.1"
    All cmdlets below are missing in 6.0.
    I will miss some, like Get-ClipBoard, for instance...

    Get-PowershellDefaultCMD | ? present -match false | ft -a | clip
    CommandType Name                          Definition                     Present ReadOnlyOption AllScopeOption
    ----------- ----                          ----------                     ------- -------------- --------------
    Alias       asnp                          Add-PSSnapIn                   False   ReadOnly                     
    Alias       CFS                           ConvertFrom-String             False   ReadOnly                     
    Alias       curl                          Invoke-WebRequest              False   ReadOnly                     
    Alias       epsn                          Export-PSSession               False                                
    Alias       gcb                           Get-Clipboard                  False   ReadOnly                     
    Alias       gsnp                          Get-PSSnapIn                   False   ReadOnly                     
    Alias       gwmi                          Get-WmiObject                  False   ReadOnly                     
    Alias       ipsn                          Import-PSSession               False                                
    Alias       ise                           powershell_ise.exe             False   ReadOnly                     
    Alias       iwmi                          Invoke-WMIMethod               False   ReadOnly                     
    Alias       lp                            Out-Printer                    False   ReadOnly                     
    Alias       npssc                         New-PSSessionConfigurationFile False   ReadOnly                     
    Alias       nwsn                          New-PSWorkflowSession          False   ReadOnly                     
    Alias       ogv                           Out-GridView                   False   ReadOnly                     
    Alias       rsnp                          Remove-PSSnapin                False                                
    Alias       rujb                          Resume-Job                     False                                
    Alias       rwmi                          Remove-WMIObject               False   ReadOnly                     
    Alias       sc                            Set-Content                    False   ReadOnly                     
    Alias       scb                           Set-Clipboard                  False   ReadOnly                     
    Alias       shcm                          Show-Command                   False   ReadOnly                     
    Alias       sujb                          Suspend-Job                    False                                
    Alias       swmi                          Set-WMIInstance                False   ReadOnly                     
    Alias       trcm                          Trace-Command                  False   ReadOnly                     
    Alias       wget                          Invoke-WebRequest              False   ReadOnly                     
    Cmdlet      Add-Computer                                                 False                                
    Cmdlet      Add-PSSnapin                                                 False                                
    Cmdlet      Checkpoint-Computer                                          False                                
    Cmdlet      Clear-EventLog                                               False                                
    Cmdlet      Clear-RecycleBin                                             False                                
    Cmdlet      Complete-Transaction                                         False                                
    Cmdlet      ConvertFrom-String                                           False                                
    Cmdlet      Convert-String                                               False                                
    Cmdlet      Disable-ComputerRestore                                      False                                
    Cmdlet      Enable-ComputerRestore                                       False                                
    Cmdlet      Export-BinaryMiLog                                           False                                
    Cmdlet      Export-Console                                               False                                
    Cmdlet      Export-Counter                                               False                                
    Cmdlet      Format-Default                                               False                                
    Cmdlet      Get-Clipboard                                                False                                
    Cmdlet      Get-ComputerRestorePoint                                     False                                
    Cmdlet      Get-ControlPanelItem                                         False                                
    Cmdlet      Get-Counter                                                  False                                
    Cmdlet      Get-EventLog                                                 False                                
    Cmdlet      Get-HotFix                                                   False                                
    Cmdlet      Get-PSSnapin                                                 False                                
    Cmdlet      Get-Transaction                                              False                                
    Cmdlet      Get-WmiObject                                                False                                
    Cmdlet      Import-BinaryMiLog                                           False                                
    Cmdlet      Import-Counter                                               False                                
    Cmdlet      Invoke-WmiMethod                                             False                                
    Cmdlet      Limit-EventLog                                               False                                
    Cmdlet      New-EventLog                                                 False                                
    Cmdlet      New-WebServiceProxy                                          False                                
    Cmdlet      Out-GridView                                                 False                                
    Cmdlet      Out-LineOutput                                               False                                
    Cmdlet      Out-Printer                                                  False                                
    Cmdlet      Register-WmiEvent                                            False                                
    Cmdlet      Remove-Computer                                              False                                
    Cmdlet      Remove-EventLog                                              False                                
    Cmdlet      Remove-PSSnapin                                              False                                
    Cmdlet      Remove-WmiObject                                             False                                
    Cmdlet      Reset-ComputerMachinePassword                                False                                
    Cmdlet      Restore-Computer                                             False                                
    Cmdlet      Resume-Job                                                   False                                
    Cmdlet      Set-Clipboard                                                False                                
    Cmdlet      Set-WmiInstance                                              False                                
    Cmdlet      Show-Command                                                 False                                
    Cmdlet      Show-ControlPanelItem                                        False                                
    Cmdlet      Show-EventLog                                                False                                
    Cmdlet      Start-Transaction                                            False                                
    Cmdlet      Suspend-Job                                                  False                                
    Cmdlet      Test-ComputerSecureChannel                                   False                                
    Cmdlet      Undo-Transaction                                             False                                
    Cmdlet      Update-List                                                  False                                
    Cmdlet      Use-Transaction                                              False                                
    Cmdlet      Write-EventLog                                               False                                
    
  • #93243

    Yubu
    Participant

    And Invoke-WebRequest, what will break a lot of my stuff. This won't work on 6.0 🙂 :
    Search-viaMichelin New-York
    —–> New-York
    544 restaurants
    Bib Gourmand (126)
    1 star (56)
    2 stars (11)
    3 stars (5)

    • #93264

      js
      Participant

      My example of convertfrom-string didn't work in 5.1 either.

  • #93388

    Yubu
    Participant
    $props=@{}; $text.split("`n`r") | %{(($_).split(" :") | sls "\w")[0,2] -join " " | ? {$_} | out-string | Remove-EmptyLines | ConvertFrom-String} | %{$props+=@{$_.p1=$_.p2}};  New-Object -TypeName PSObject -Property $Props
    

You must be logged in to reply to this topic.