Avatar of Ed Walsh
Ed Walsh
Flag for United States of America

asked on 

Turning Powershell output into Column CSV file

I have a powershell script to pull information from IIS, currently it just drops the output on the screen and for the time that was fine, but now we need to drop the output into columns in a csv file. This will then be imported into a Database to allow us to query for more information.

I am having issues with getting this into columns and into a CSV file output. Any help is appreciated.

Set-ExecutionPolicy RemoteSigned
Import-Module WebAdministration

Get-WebApplication | `
ForEach-Object {

Write-Host "Web Application: $($_.path)"
"Physical Path: $($_.PhysicalPath)"
"Application Pool: $($_.ApplicationPool)"

$test_webconfig_exists = test-path "$($_.PhysicalPath)\Web.config"

$webConfigFile = [xml](Get-Content "$($_.PhysicalPath)\Web.config")

foreach($connString in $webConfigFile.configuration.connectionStrings.add)
  Write-Host "-----------------"
  Write-Host "Connection String $($connString.name): $($connString.connectionString)"
  $dbRegex = "((Initial\sCatalog)|((Database)))\s*=(?<ic>[a-z\s0-9]+?);"
  $found = $connString.connectionString -match $dbRegex
  if ($found)
  Write-Host "~~~~~~~~~~~~~~~~~~~~"
   Write-Host "Database: $($Matches["ic"])"
  Write-Host "~~~~~~~~~~~~~~~~~~~~"
  #Write-Host "-----------------"

if($test_webconfig_exists -eq "")
Write-Host "Missing Web.Config File: Yes"
} Else
Write-Host "Missing Web.Config File: No"
Write-Host "**======================================================**"


Open in new window


Avatar of undefined
Last Comment
Ed Walsh

8/22/2022 - Mon