none
Powershell DHCPserverv4lease output to csv file RRS feed

  • Question

  • I am new to powershell scripting and need a help to resolve a problem

    I have retrieved information from DHCPserver for particular IP but I want to use the command in script to traverse all IPs in file and save each DHCPSERVERV4LEASE output to CSV on c drive

     IPs are recorded in file named IPs.txt in c drive and using this command get-dhcpserverv4lease -computername dhcp.xyz.com -ipaddress 192.168.1.1 (want to read all IPs in text file from c drive"

    now want to store output in csv file for each IP read from IP.CSV

    need little urgent support

    thanks in advance

    Monday, August 24, 2015 10:29 AM

Answers

  • You're welcome.

    Here's what I'd try (I don't have these cmdlets available, so I can't do much more with this). I've added some comments as well:

    # Read input file and process each line
    Get-Content .\ipList.txt | ForEach-Object {
    
        # Set the $ip variable to the current item in the loop
        # This isn't really necessary, but I do this for clarity's sake
        $ip = $_
    
        # Run the cmdlet using the current item in the loop
        Get-DhcpServerv4Lease -ComputerName serverName -IPAddress $ip
    
    } | Export-Csv .\outputFile.csv -NoTypeInformation
    # The line above takes all output from the loop and exports it to a CSV file


    Monday, August 24, 2015 1:31 PM

All replies

  • Use Get-Content to read your file, ForEach-Object to loop, and then Export-Csv outside of the loop to create your output file.

    Links to syntax/examples:

    http://ss64.com/ps/get-content.html

    http://ss64.com/ps/foreach-object.html

    http://ss64.com/ps/export-csv.html

    Basic skeleton:

    Get-Content .\ipList.txt | ForEach-Object {
    
        $ip = $_
    
        ....
    
    } | Export-Csv .\outputFile.csv -NoTypeInformation


    Monday, August 24, 2015 12:14 PM
  • Thanks Mike for your time,

    Import-Moduledhcpserver


    Get-Content

    C:\IPList.Txt


    ForEach-object

    {


    $IP

    =Get-DhcpServerv4Lease-computernamedhcp-vertex.vertex.com.pk-IPAddress$IP


    }

    |export-csvc:\test123.csv-NoTypeInformation

    this is how I am trying to do this but since I am very new to powershell scripting therefore receiving errors :( can you please help to sort it out....the text file from which IPs are being read only have IP addresses 

    Monday, August 24, 2015 12:43 PM
  • You're welcome.

    Here's what I'd try (I don't have these cmdlets available, so I can't do much more with this). I've added some comments as well:

    # Read input file and process each line
    Get-Content .\ipList.txt | ForEach-Object {
    
        # Set the $ip variable to the current item in the loop
        # This isn't really necessary, but I do this for clarity's sake
        $ip = $_
    
        # Run the cmdlet using the current item in the loop
        Get-DhcpServerv4Lease -ComputerName serverName -IPAddress $ip
    
    } | Export-Csv .\outputFile.csv -NoTypeInformation
    # The line above takes all output from the loop and exports it to a CSV file


    Monday, August 24, 2015 1:31 PM
  • Oooooo Great...Thanks Mike for your kind support it has resolved the issue...thanks again
    Tuesday, August 25, 2015 4:21 AM
  • Cheers, you're very welcome. Glad it worked out.

    Tuesday, August 25, 2015 4:45 AM