none
lose spaces in powershell output RRS feed

  • Question

  • I have the following script:

    Clear-Host
     # get free IP adress
     $strFileName= "C:\temp\UsedIP.txt"
    If (Test-Path $strFileName){
    Remove-Item $strFileName
    }
    $Used_IP = Get-DhcpServerv4Reservation -ScopeId x.x.20.0 | select ipaddress | Out-File C:\temp\UsedIP.txt
    $All_IP = Get-Content "C:\Scripts\IP_reservation\IPrange_20.txt"
    $reservations = Get-Content "C:\temp\UsedIP.txt"
    Compare-Object $All_IP $reservations
     # input variabels
    $name = Read-Host "enter te name of the device you want to make a reservation for"
    $ip = Read-Host "enter the last digets of the reservation needed"
    $mac = Read-Host "enter the mac-adress for the reservation"
     # DC-04
    Add-DhcpServerv4Reservation -ScopeId x.x.20.0 -IPAddress x.x..20.$ip -ClientId $mac -Name $name
     # DC-02
    Add-DhcpServerv4Reservation -ComputerName dc-02.x.local -ScopeId  x.x.20.0 -IPAddress x.x.20.$ip -ClientId $mac -Name $name
    Add-DhcpServerv4Reservation -ComputerName dc-02.x.local -ScopeId  x.x.20.0 -IPAddress x.x.20.$ip -ClientId $mac -Name $name
     # DC-03
    Add-DhcpServerv4Reservation -ComputerName dc-03.x.local -ScopeId  x.x.20.0 -IPAddress x.x.20.$ip -ClientId $mac -Name $name

    what i want with the first sector is to view a list of free ip addresses to choose from. The problem is that the output of the $Used_IP isn't consistend. It come's whit a lot of spaces after the IP-addresses. Because of that the output on the screen consists the used ip-addresses as wel as the list i've made to compare them with.

    Can anyone tell me how to lose all those spaces?

    • Edited by Wim Duizer Monday, March 6, 2017 1:56 PM domainname removal
    Friday, March 18, 2016 4:17 PM

Answers

  • $Used_IP = Get-DhcpServerv4Reservation -ScopeId 192.168.20.0 | select -expand ipaddress


    \_(ツ)_/

    • Marked as answer by Wim Duizer Monday, March 21, 2016 8:23 AM
    Friday, March 18, 2016 4:31 PM

All replies

  • $Used_IP = Get-DhcpServerv4Reservation -ScopeId 192.168.20.0 | select -expand ipaddress


    \_(ツ)_/

    • Marked as answer by Wim Duizer Monday, March 21, 2016 8:23 AM
    Friday, March 18, 2016 4:31 PM
  • Without having tried anything myself, I suspect it's because the IP address ending up in C:\temp\UsedIP.txt indicates the property name, has a horizontal separator, and includes your value. Try one of the two options below:

    Get-DhcpServerv4Reservation -ScopeId 192.168.20.0 | Select-Object -ExpandProperty ipaddress | Out-File C:\temp\UsedIP.txt
    
    (Get-DhcpServerv4Reservation -ScopeId 192.168.20.0).ipaddress | Out-File C:\temp\UsedIP.txt

    Notice that I've removed the $Used_IP variable assignment. That's not necessary and honestly, it's not going to set your variable, although, it will create the file. Here's a little proof:

    PS> Get-Service -Name svsvc
    
    Status   Name               DisplayName
    ------   ----               -----------
    Stopped  svsvc              Spot Verifier
    
    PS> Get-Service -Name svsvc | Select-Object DisplayName
    
    DisplayName
    -----------
    Spot Verifier
    
    PS> Get-Service -Name svsvc | Select-Object -ExpandProperty DisplayName
    Spot Verifier
    PS> $x = Get-Service -Name svsvc | Select-Object -ExpandProperty DisplayName | Out-File -FilePath C:\service.txt
    PS> $x
    PS> # See, nothing in $x
    PS> Get-Content -Path C:\service.txt
    Spot Verifier


    Friday, March 18, 2016 4:41 PM
  • That worked great, thanks. At first i added a out file to your line but came with a ******** of extra info so wasn't useable.

    Directly it works like a charm


    Monday, March 21, 2016 8:30 AM