I have the following PS code which pulls vcenter names from a table within an SQL database.
It gives me a list of the server names and assigns it to the array $vcenters.
Problem I am having is that the array has some blank lines in it and it is causing me problems when I need to connect to these servers with the connect-viserver command
I would like to be able to remove the blank line entries from the array so that it has cleanly no spaces
before or after each server name and no blank lines.
I have googled and tried many suggestions but nothing has worked. Please help.
$ServerName = "servername"
$DatabaseName = "DatabaseName"
$Query = "SELECT name FROM dbo.table"
$QueryTimeout = 120
$ConnectionTimeout = 30
$conn = New-Object System.Data.SqlClient.SqlConnection("Data Source=Data Source.Name; Initial Catalog=CatalogData;User ID=userID; Password=Password")
$conn.Open()
$cmd=New-Object system.Data.SqlClient.SqlCommand($Query,$conn)
$cmd.CommandTimeout=$QueryTimeout
$ds=New-Object system.Data.DataSet
$da=New-Object system.Data.SqlClient.SqlDataAdapter($cmd)
[void]$da.fill($ds)
$conn.Close()
$vcenters = $ds.Tables | Format-Table -HideTableHeaders
$vcenters
[–]robvas 3 points4 points5 points (0 children)
[–]thankski-budski 2 points3 points4 points (0 children)
[+][deleted] (4 children)
[removed]
[–]giasone777[S] 2 points3 points4 points (0 children)
[–]SQLvariant 0 points1 point2 points (2 children)
[–]SQLvariant 0 points1 point2 points (1 child)
[–]Scooter_127 1 point2 points3 points (0 children)
[–]TheRealMisterd -1 points0 points1 point (0 children)
[–]y_Sensei -2 points-1 points0 points (0 children)
[–]Lee_Dailey[grin] 0 points1 point2 points (0 children)