Powershell DHCPserverv4lease output to csv file

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

August 24th, 2015 6:33am

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

Free Windows Admin Tool Kit Click here and download it now
August 24th, 2015 8:18am

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 

August 24th, 2015 8:47am

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

Free Windows Admin Tool Kit Click here and download it now
August 24th, 2015 9:35am

Oooooo Great...Thanks Mike for your kind support it has resolved the issue...thanks again
August 25th, 2015 12:25am

Cheers, you're very welcome. Glad it worked out.
Free Windows Admin Tool Kit Click here and download it now
August 25th, 2015 12:49am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics