I have this insert query that's not inserting properly and is really annoying. I will explain briefly what I tried and what I actually want to achieve.
What my PS script does:
- ping every computer from a database table
- if ping succeeds, get username logged on from the computer
- insert to database table the following values : username, hostname, online(true/false)
Now here are the problems that keep showing up.
Problem 1: If I use the following command:
$username = Get-WMIObject -ComputerName $workstation -Class Win32_ComputerSystem | Select-Object -ExpandProperty Username;
It will insert the username values like so: SUPER/user1 which is Okay but I want to get rid of the "SUPER/" and just keep the actual username "user1" before inserting.
I tried using the one below but it doesnt work:
$username = Get-WMIObject -ComputerName $workstation -Class Win32_ComputerSystem | Select-Object -ExpandProperty -replace '^.*\\' Username;
The command in problem 1 will give me these TWO errors:
Cannot process argument because the value of argument "obj" is null. Change the value of argument "obj" to a non-null value.
And
Get-WmiObject : The RPC server is unavailable.
How can I ignore those two errors?
I hope someone can help me out because its really annoying. thanks in advance everyone.
Powershell script:
foreach($pcname in $workstation_list){
$command = $connection.CreateCommand();
$command.Connection = $connection;
$command.CommandText = "INSERT INTO workstation_userlogged
(username,hostname,online)
VALUES (@username,@hostname,@status)";
### =============================================================
### values to be assigned as a parameters
### =============================================================
$workstation = $pcname;
$test_ping = Test-Connection -ComputerName $workstation -Count 1 -ErrorAction SilentlyContinue;
$status = [bool](Test-Connection $workstation -Quiet -Count 1 -ErrorAction SilentlyContinue);
#if status is TRUE get username
if($test_ping)
{
$username = Get-WMIObject -ComputerName $workstation -Class Win32_ComputerSystem | Select-Object -ExpandProperty Username;
echo $username;
#echo -replace '^.*\\' $username;
#if ping succeeds but no user is logged
if($username -eq "")
{
$username = "no user logged";
}
} #end if
#if ping DOES NOT succeed set username value to NULL and status to FALSE
elseif(!$test_ping)
{
$username = [DBNull]::Value;
}
### =============================================================
### Assign parameters with appropriate values
### =============================================================
$command.Parameters.Add("@username", $username) | Out-Null
$command.Parameters.Add("@hostname", $workstation) | Out-Null
$command.Parameters.Add("@status", $status) | Out-null
### =============================================================
### Execute command query
### =============================================================
try{
$command.ExecuteNonQuery() | out-null;
Write-Host "Successfully inserted in Database";
}
catch{
Write-Host "Caught the exception";
Write-Host "$_";
}
### =============================================================
### clear parameter values
### =============================================================
$command.Dispose()
$workstation = "";
$username = "";
$status = "";
$test_ping = "";
}#end for each loop