title: Powershell - RPC, OBJ Null and -Replace ERRORS in MySQL insert query
body: <p>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.</p>
  
  <p>What my PS script does: </p>
  
  <ol>
  <li>ping every computer from a database table</li>
  <li>if ping succeeds, get username logged on from the computer</li>
  <li>insert to database table the following values : username, hostname, online(true/false)</li>
  </ol>
  
  <p>Now here are the problems that keep showing up.</p>
  
  <p><strong>Problem 1: If I use the following command:</strong> </p>
  
  <p>$username =  Get-WMIObject -ComputerName $workstation -Class Win32_ComputerSystem | Select-Object -ExpandProperty Username;  </p>
  
  <p>It will insert the username values like so: <strong>SUPER/user1</strong> which is Okay but I want to get rid of the "SUPER/" and just keep the actual username "user1" before inserting.</p>
  
  <p>I tried using the one below but it doesnt work:</p>
  
  <pre><code> $username =  Get-WMIObject -ComputerName $workstation -Class Win32_ComputerSystem | Select-Object -ExpandProperty -replace '^.*\\'  Username;  
  </code></pre>
  
  <p>The command in <strong>problem 1</strong> will give me these TWO errors: </p>
  
  <p><strong>Cannot process argument because the value of argument "obj" is null. Change the 
  value of argument "obj" to a non-null value.</strong></p>
  
  <p>And</p>
  
  <p><strong>Get-WmiObject : The RPC server is unavailable.</strong></p>
  
  <p>How can I ignore those two errors?</p>
  
  <p>I hope someone can help me out because its really annoying. thanks in advance everyone.</p>
  
  <p><strong>Powershell script:</strong></p>
  
  <pre><code>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 
  </code></pre>