Battle Faction Puzzle 4

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

  • Author
  • #92928

    Axel Bøg Andersen

    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


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

  • #92955

    Richard Siddaway

    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


    Works on 5.1

    function Get-NetSt {
    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


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

  • #93210


    I would say this:
    1. Those blogs are also good, or, possibly, better imho:
    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


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

    $template = @'
    $text = @'
       IPv4 Address. . . . . . . . . . . :
       Subnet Mask . . . . . . . . . . . :
       Default Gateway . . . . . . . . . :
    $text | ConvertFrom-String -TemplateContent $template
  • #93240


    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


    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


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

  • #93388

    $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.