I am very new to powershell and have this script that will ping every computer from a database server. I am using MySQL (yes I know most people use MSSQL with powershell but in my case I have to use MySQL).
The ping works perfectly fine, how can I retrieve USERNAME for every computer that pings succesfully?
Thanks in advance!
Below is my code:
#ping the computers from the database to verify if it is online/offline
#Create a command object
$command = New-Object MySql.Data.MySqlClient.MySqlCommand;
$command.Connection = $connection
# $command = $connection.CreateCommand() #ERROR
#call procedure to execute the command
$command.CommandText = "SELECT w.ws FROM
sandbox_maesc4.coordinates c
INNER JOIN
softphone_materials_updater.workstations w
ON c.station_number = w.pod";
#Execute the Command by reading each row
$reader = $command.ExecuteReader();
#Read values from database
$workstation_list = $( while ($reader.Read() ){
$reader.GetValue(0)
})
#close reader
$reader.Close()
#ping each computer if online, obtain username logged in only
foreach($pc_db in $workstation_list){
if(Test-Connection -ComputerName $pc_db -Count 1 -ErrorAction SilentlyContinue){
#HERE IS THE CODE, I JUST WANT TO RETRIEVE USERNAME THAT'S LOGGED ON!
Get-WMIObject -ComputerName $pc_db -Class Win32_ComputerSystem | Select-Object -ExpandProperty Username
}
else{
Write-Host "$pc_db is Offline" -ForegroundColor Red
}
}#end for each loop
$connection.Close()
}#end ping_test function
db_conn #execute function