SOLVED

Export Results To Individual Files

%3CLINGO-SUB%20id%3D%22lingo-sub-2375926%22%20slang%3D%22en-US%22%3EExport%20Results%20To%20Individual%20Files%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2375926%22%20slang%3D%22en-US%22%3E%3CP%3EHello%20I%20am%20trying%20to%20export%20.xml%20files%20from%20a%20list%20of%20servers%20so%20it%20creates%20a%20separate%20file%20for%20each%20server%20in%20a%20loop.%20But%20the%20problem%20is%20it%20looks%20like%20the%20loop%20would%20overwrite%20the%20results%20from%20each%20server%20in%20the%20loop%20to%20the%20same%20output%20file.%20I%20have%20been%20searching%20but%20cannot%20find%20a%20way%20to%20do%20what%20I%20want%20it%20to%20do.%20Here%20is%20what%20I%20have%20for%20the%20scrip.%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-applescript%22%3E%3CCODE%3E%24computers%20%3D%20get-content%20.%5Cservers.txt%0Aforeach%20(%24computer%20in%20%24computers)%7B%0AExport-DhcpServer%20-ComputerName%20%24computer%20-File%20%22C%3A%5CDHCP_EXPORT%5CDHCP-Config.xml%22%20-Force%0A%7D%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3ECan%20someone%20tell%20me%20how%20I%20can%20get%20this%20to%20create%20a%20separate%20.xml%20file%20for%20each%20server%20in%20the%20servers.txt%20file%3F%20Its%20hard%20to%20search%20for%20a%20solution%20for%20this%20in%20Google%20so%20I%20though%20I%20could%20get%20some%20help%20here.%3CBR%20%2F%3EThanks%20in%20advance!!%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-2375926%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EPoweShell%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EWindows%20PowerShell%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2378275%22%20slang%3D%22en-US%22%3ERe%3A%20Export%20Results%20To%20Individual%20Files%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2378275%22%20slang%3D%22en-US%22%3E%3CP%3EHello%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F531473%22%20target%3D%22_blank%22%3E%40charlie4872%3C%2FA%3E%2C%3C%2FP%3E%3CP%3EOne%20of%20the%20options%20would%20be%20to%20add%20%3CSTRONG%3E%24computer%3C%2FSTRONG%3E%26nbsp%3Bto%20resulting%20filename%3A%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3E%24computers%20%3D%20get-content%20.%5Cservers.txt%0Aforeach%20(%24computer%20in%20%24computers)%7B%0AExport-DhcpServer%20-ComputerName%20%24computer%20-File%20%22C%3A%5CDHCP_EXPORT%5C%24computer-DHCP-Config.xml%22%20-Force%0A%7D%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3EThis%20will%20create%20a%20separate%20file%20for%20every%20computer%20in%20the%20input%20file%20in%20your%20%3CSTRONG%3EDHCP_EXPORT%3C%2FSTRONG%3E%20folder.%3C%2FP%3E%3CP%3EHope%20that%20helps%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E
Contributor

Hello I am trying to export .xml files from a list of servers so it creates a separate file for each server in a loop. But the problem is it looks like the loop would overwrite the results from each server in the loop to the same output file. I have been searching but cannot find a way to do what I want it to do. Here is what I have for the scrip.

$computers = get-content .\servers.txt
foreach ($computer in $computers){
Export-DhcpServer -ComputerName $computer -File "C:\DHCP_EXPORT\DHCP-Config.xml" -Force
}

Can someone tell me how I can get this to create a separate .xml file for each server in the servers.txt file? Its hard to search for a solution for this in Google so I though I could get some help here.
Thanks in advance!! 

2 Replies
best response confirmed by charlie4872 (Contributor)
Solution

Hello @charlie4872,

One of the options would be to add $computer to resulting filename:

$computers = get-content .\servers.txt
foreach ($computer in $computers){
Export-DhcpServer -ComputerName $computer -File "C:\DHCP_EXPORT\$computer-DHCP-Config.xml" -Force
}

This will create a separate file for every computer in the input file in your DHCP_EXPORT folder.

Hope that helps

 

Thanks AndySvints this does exactly what I was looking for!