Quantcast
Channel: PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources
Viewing all articles
Browse latest Browse all 6937

Powershell MySql Stored Procedure ERROR

$
0
0

this is my first time posting a question here. I am complete new with Powershell scripting as I am doing new projects. The ERROR I get is Exception calling "ExecuteNonQuery" with "0" argument(s): No Function or Procedure in the Database. I've spent hours trying to figure this out with the help of the internet but can't solve it. The PROCEDURE does exist and I can use it properly with a MySql Query Browser.

Basically I am reading from a database table computer name rows. After reading it will ping each computer and get it's local hard drive information and insert the information in another table to their corresponding fields. Hopefully someone can help me solve this. Keep in mind that I am using MySql and NOT MSSQL. Also, for some reason my call procedure isn't working well when I do $command.CommandText = "nameOfProcedure(@var1,@var2)" Thank you in advance!

 

#."\\filepath" #calling script to connect to Database

#ping the computers from the database to verify if it is online/offline 

function ping_test {

 

    #open sql connection

    $connection.Open()

    #Create a command object

    $command = $connection.CreateCommand()

    #call procedure to execute the command

    $command.CommandText = "call getWorkStations();"

    #Execute the Command by reading each row

    $reader = $command.ExecuteReader();

    #Read values from database

    $pcnames = $( while ($reader.Read() ){

                    $reader.GetValue(0) 

               })

    #close reader                     

    $reader.Close()

    #close connection

    $connection.Close()

 

   #ping each computer if online, obtain information about the local hard drives only

    foreach($read_computer in $pcnames){

        if(Test-Connection -ComputerName $read_computer -Count 1 -ErrorAction SilentlyContinue){

            Write-Host "$read_computer is Online" -ForegroundColor Green 

            #call function to execute the local hard drive information to then insert in DB table 

            hard_disk_info

        }

        else{

            Write-Host "$read_computer is Offline" -ForegroundColor Red

            }

    }#end for each loop   

}#end ping_test function

 

 

 

#hard_disk_info function

function hard_disk_info {

    foreach ($computer in $pcnames){

        #if ER and SC is taken out, it will show two possible errors: Access Denied and/or RPC Server 

        #Open Wmi class Win32_logicaldisk for each computer. Drivetype=3 points to local hard disk

        $list = Get-WmiObject win32_logicaldisk -ComputerName $computer -filter "Drivetype=3" -ErrorAction SilentlyContinue

 

       foreach($item in $list){

            #$command = New-Object MySql.Data.MySqlClient.MySqlCommand("addWorkstations", $connection)

            #initialize connection

            $command.Connection = $connection

            #open connection

            $connection.Open()

            #command type

            $command.CommandType = [System.Data.CommandType]'StoredProcedure' 

            #procedure to execute

           # $command.CommandText = "addWorkstations"

 

           $command.CommandText = "call addWorkstations(@computer, @online, @drive, @maxsize, @currentsize, @percentused)" #THIS ISNT WORKING

            echo $command.CommandText

            #$command.CommandText = "call addWorkstations()"

 

            Try{

                $percentused = ("{0:P2}" -f (1 - ([Int64]$item.FreeSpace / [Int64]$item.Size)))

                $maxsize     = [math]::truncate($item.Size/1.0GB)

                $cursize     = [math]::truncate( ($item.Size - $item.Freespace)/1.0GB)

                $status      = [bool](Test-Connection $computer -Quiet -Count 1)

            }#end try

            Catch{#used in case ping is FALSE and to avoid "attempted to divide by zero error"

                $percentused = "n/a"

                $maxsize     = "n/a"

                $cursize     = "n/a"

                $nodrive     = "n/a"

                $status      = [bool](Test-Connection $computer -Quiet -Count 1)

            }#end catch

 

            $seecomp = $command.Parameters.Add("@p_computer", $computer) | Out-Null

 

               echo $computer

               echo $seecomp

 

            $command.Parameters.Add("@p_online",      $status)        | Out-Null

            if($status){#if ping true, will give drive information

                $command.Parameters.Add("@p_drive",   $item.DeviceID) | Out-Null

            }

            else{#if false, will give nothing

                $command.Parameters.Add("@p_drive",   $nodrive)       | Out-Null

            }

            $command.Parameters.Add("@p_maxsize",     $maxsize)       | Out-Null

            $command.Parameters.Add("@p_currentsize", $cursize)       | Out-Null

            $command.Parameters.Add("@p_percentused", $percentused)   | Out-Null

 

            if($debugmode){

                write-host "after calculating values for parameters"

               }

 

            $result = $command.ExecuteNonQuery() #THIS IS WHERE THE ERROR OCCURS

            write-host "$result" 

            $connection.Close()   

       }#end foreach loop 1            

    }#end foreach loop 2 

 

}#end hard_disk_info func

 

    ping_test #run function

 


Viewing all articles
Browse latest Browse all 6937

Trending Articles