The First step of the script pulls 3 fields from SQL table. The 2nd steps grabs the server name and loops through the servername to grab more information. My goal is to combine the other 2 columns from SQL table with the information gathers from the WMI in the output.
#SQL Variables
$SQLServer = 'remoteservername'
$Database = 'Dbname'
#Create a SQL Query function
Function SQL_Query ($Query) {
$SqlConnection = New-Object System.Data.SqlClient.SqlConnection
$SqlConnection.ConnectionString = "Server=$SQLServer;Database=$Database;Integrated Security=True"
$SqlCmd = New-Object System.Data.SqlClient.SqlCommand
$SqlCmd.Connection = $SqlConnection
$SqlCmd.CommandText = $Query
$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
$SqlAdapter.SelectCommand = $SqlCmd
$DataSet = New-Object System.Data.DataSet
$a = $SqlAdapter.Fill($DataSet)
$SqlConnection.Close()
$DataSet.Tables[0]
} #End SQL Query Function
$Servers = SQL_Query "SELECT
Servername, Status, Purpose
FROM tablename "
$Results = $Servers | Select-Object -ExpandProperty ServerName
#loop through servers
$Output = @()
$Output =Foreach ($Server in $Results)
{
$Servers # This variable stores Servername, Status, and Purpose information. I want the Status and Purpose fields to show up in the loop below. The idea is if the Servername from $servers matches $server,add the extra 2 fields from #Servers
$BootTime = GET-WmiObject win32_Operatingsystem -ComputerName $server -EA STOP | select @{n='ServerName';e={$_.csname}},@{n='LastBootUpTime';e={$_.ConverttoDateTime($_.lastbootuptime)}}`
,@{n='LocalTime';e={$_.ConverttoDateTime($_.LocalDateTime)}} ,@{n='UpTime';e={"Uptime:" + ((GET-DAte) - $_.ConverttoDateTime($_.lastbootuptime)).Days + " Days " +`
((GET-DAte) - $_.ConverttoDateTime($_.lastbootuptime)).Hours + " Hours " + ((GET-DAte) - $_.ConverttoDateTime($_.lastbootuptime)).Minutes + " Minutes "}},@{n='OS';e={$_.caption}}
}
#Display Out here
$Boottime | Select ServerName,LastBootUpTime,Uptime,Status,Purpose |Out-GridView