Skip to content

Powershell Query

One part cool sql query to get a section out of a connection string…other part how you loop over query results in powershell

$use = "use That_Database; "
$query = $use + "select distinct(SUBSTRING(super_long_string,CHARINDEX('Database=', super_long_string)+9,CHARINDEX(';', SUBSTRING(super_long_string,CHARINDEX('Database=', super_long_string)+10,len(super_long_string)))))as FoundString from DB_Table where super_long_string!= ''"
$results = Invoke-Sqlcmd -Query $query -ServerInstance $server -Username $user -Password $password -Verbose -querytimeout 65535 -AbortOnError -ErrorAction SilentlyContinue
$wh_info = @();

if($results -ne $null)
  foreach($row in $results)
    $wh_info += $row[0]

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *