Array output to string sorting or filtering

Welcome Forums General PowerShell Q&A Array output to string sorting or filtering

This topic contains 3 replies, has 3 voices, and was last updated by

 
Participant
8 months, 1 week ago.

  • Author
    Posts
  • #95418

    Participant
    Points: 1
    Rank: Member

    Hi,

    I've started writing a script to migrate DHCP scopes from one server to another. I've like the scopes to be automatically taken from the source server, so I don't have to manually check and list them.

    If I use something like the below command to get the scopes which it gives as an array (also listed below)

    Is it possible to sort or filter the name(ScopeId) column to give me a result like

    10.111.1.64,10.111.1.128,10.111.1.192

    that I can use as a variable?

    I've tried a few options but I've had no luck.

    Get-DhcpServerv4Scope -ComputerName DhcpServerName | Group-Object ScopeId

    Count Name Group
    —– —- —–
    1 10.111.1.64 {DhcpServerv4Scope}
    1 10.111.1.128 {DhcpServerv4Scope}
    1 10.111.1.192 {DhcpServerv4Scope}
    1 10.111.2.0 {DhcpServerv4Scope}
    1 10.111.4.0 {DhcpServerv4Scope}
    1 10.111.5.0 {DhcpServerv4Scope}
    1 10.111.6.0 {DhcpServerv4Scope}

  • #95420

    Participant
    Points: 141
    Helping Hand
    Rank: Participant

    Have you tried piping it to sort?

    E.g.

    Get-DhcpServerv4Scope -ComputerName DhcpServerName | Group-Object ScopeId | Sort Name
    
  • #95426
    js

    Participant
    Points: 207
    Helping Hand
    Rank: Participant
    $a = (Get-DhcpServerv4Scope -ComputerName DhcpServerName | Group-Object ScopeId | 
      select -expand name) -join ','
    
    • #95427

      Participant
      Points: 1
      Rank: Member

      Thanks js just what I was looking for

      $ScopeIds = (Get-DhcpServerv4Scope -ComputerName DHCPServerName).ScopeId -join ","

The topic ‘Array output to string sorting or filtering’ is closed to new replies.