Author Posts

April 11, 2017 at 6:03 pm

So I'm trying to create a script to audit any number of servers at our different customer locations. All of the information I'm looking for can be extracted from the config and xml files for our program, and the files are saved to a standard location. I'm able to use

$data = Get-Content "C:\MyFolder\MyConfig.Config
$data[3]

To get the 3rd line of the text. But let's say the contents of that line are:

value="Data Source=HOSTNAME\INSTANCE;Initial Catalog=DATABASENAME;User Id=DATABASEUSER;Password=DATABASEPASSWORD

I'm trying to find a way to display different outputs with just "HOSTNAME\INSTANCE," "DATABASENAME," "DATABASEUSER," and "DATABASEPASSWORD."

Any thoughts?

April 11, 2017 at 7:04 pm

# For demo:
$Lines = 'value="Data Source=HOSTNAME\INSTANCE1;Initial Catalog=DATABASENAMEx;User Id=DATABASEUSER3;Password=DATABASEPASSWORD6',
         'value="Data Source=HOSTNAME\INSTANCE2;Initial Catalog=DATABASENAMEy;User Id=DATABASEUSER4;Password=DATABASEPASSWORD7',
         'value="Data Source=HOSTNAME\INSTANCE3;Initial Catalog=DATABASENAMEz;User Id=DATABASEUSER5;Password=DATABASEPASSWORD8'
# In your case you may use:
# $Lines = Get-Content 'C:\MyFolder\MyConfig.Config'

$MyOutput = $Lines | % {
    [PSCustomObject]@{
        DataSource     = $_.Split(';')[0].Split('=')[1]
        InitialCatalog = $_.Split(';')[1].Split('=')[1]
        UserID         = $_.Split(';')[2].Split('=')[1]
        Password       = $_.Split(';')[3].Split('=')[1]
    }
}


$MyOutput | FT -a # output to console
$MyOutput | out-gridview # in ISE

$MyOutput | Export-Csv .\myOutput.csv -not 

April 12, 2017 at 4:45 pm

Thank you! That helped a lot! Unfortunately I found that different versions of our program had slightly different config files so I had to add in a bunch of If statements, but I finally got it to display exactly as I wanted, and now that I better understand the split command, I feel like I'm on top of the world!