First post, be gentle. I’m trying to create a script using PS ISE 3.0 which takes a list of reference numbers, currently in a text file, adds them as an array in a variable, and takes each reference number, adds it into a Invoke-Webrequest URI, and then with the resulting XML, takes each ‘result’ (the webrequest is to an enterprise search tool) and outputs it to a .csv, formatted in columns.
So far I’ve managed to get the script to output the full search results for each reference number in its own .xml file (named after the searched reference number), but I’m struggling to get it to output to a .csv file in a format I can pass to my user - they essentially want a report.
This is what I have so far, I’m just trying to work with the first 20 numbers at the moment until I get it working, and the out-file within the Invoke-WebRequest is only there during development, unless I really need to do this another way and then process each XML file. I’ve had to omit the URL and some of the XML node names before posting this, but it shouldn’t be an issue.
## Define Variables
$SearchTermNumber = get-content C:\temp\Phones_JustNumbersDirty.txt
## Take number, add to search string, submit to Search
$SearchResult = $SearchTermNumber[1..20] | ForEach-Object -Process {Invoke-WebRequest -outfile C:\temp\Phones_Results_$_.xml -PassThru -URI "[Search tool URI" }
## Output to CSV file
ForEach-Object -process {
$SearchResult.[Search result XML Node Structure].hit.reference,
$SearchResult.[Search result XML Node Structure].hit.database,
$SearchResult.[Search result XML Node Structure].hit.content.document.bridgelink
} | Out-File C:\temp\temp_results.csv
I’ve tried several different iterations of the last section, I’ve managed to get a CSV with column headers using something like this;
ForEach-Object -process {
$SearchResultCurrent = new-object PSObject
$SearchResultCurrent | Add-Member ReferenceNumber $SearchResult.[Search result XML Node Structure].hit.reference
$SearchResultCurrent | Add-Member Source $SearchResult.[Search result XML Node Structure].hit.database
$SearchResultCurrent | Add-Member BridgeURL $SearchResult.[Search result XML Node Structure].hit.content.document.bridgelink
$SearchResultCurrent | Export-Csv C:\temp\temp_results.csv -NoTypeInformation
}
…but I can’t get it to format nicely with data in there. Can someone point me in the right direction?