0

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:

  1. ping every computer from a database table
  2. if ping succeeds, get username logged on from the computer
  3. 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 

1 Answer 1

1

I see two things. first, this line:

$username = Get-WMIObject -ComputerName $workstation -Class Win32_ComputerSystem | Select-Object Username #-ExpandProperty

Should probably be this:

$username = Get-WMIObject -ComputerName $workstation -Class Win32_ComputerSystem | Select-Object -ExpandProperty Username 

Second, you didn't use any quotes when embedding the username variable in the SQL String. Try this. I quoted all three values since I don't know what types the other columns are.

$command.CommandText = "INSERT INTO workstation_userlogged
                                    (username,hostname,online)
                                    VALUES ('$username','$pc_db','$ping_status')";
Sign up to request clarification or add additional context in comments.

10 Comments

+1 The domain part can be removed from the username like this: $Username -replace '^.*\\'
the reason why I removed -ExpandProperty is because it keeps giving me this error : Cannot process argument because the value of argument "obj" is null. Change the value of argument "obj" to a non-null value.
@AnsgarWiechers I tried to do it but gives me } at the end? What am I doing wrong? thanks. I updated my code above
@mario The parameter -ExpandProperty expects exactly one argument: the name of the property you want to expand, so you MUST use select -Expand property. Using select property -Expand will NOT work. And you get the trailing }, because you didn't expand the property before removing the domain part.
After you get $username, do this: `$username=$username -replace '^.*\\' The RPC error means that you're having trouble accessing the WMI registry on one of the remote servers. Troubleshooting that issue is a different story altogether. Looks like you did the parameters thing correctly at first glance.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.