I
I
Ivan Zhukov2015-01-28 12:49:44
MySQL
Ivan Zhukov, 2015-01-28 12:49:44

How to pass function value to variable in powershell?

How to transfer the numerical value of the result of the function execution to a variable in powershell for performing mathematical operations in the future (addition and subtraction)?
Here is the actual query function in mySQL via Powershell:

function allCalls {

$allCalls = "select distinct calldate, src, uniqueid, disposition, dstchannel from cdr where dcontext = 'reception' group by uniqueid having count(*) > 0;"

Try {
  [void][System.Reflection.Assembly]::LoadWithPartialName("MySql.Data")
  $Connection = New-Object MySql.Data.MySqlClient.MySqlConnection
  $Connection.ConnectionString = $ConnectionString
  $Connection.Open()

  $Command = New-Object MySql.Data.MySqlClient.MySqlCommand($allCalls, $Connection)
  $DataAdapter = New-Object MySql.Data.MySqlClient.MySqlDataAdapter($Command)
  $DataSet = New-Object System.Data.DataSet
  $RecordCount = $dataAdapter.Fill($dataSet, "data")
  }

Catch {
  Write-Host "ERROR : Unable to run query : $query `n$Error[0]"
 }

Finally {
  $Connection.Close()
  }
  
Write-Host ""$DataSet.Tables[0].rows.count""

}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
Y
Yuri Goncharuk, 2015-01-28
@yukon39

Replace
with
Write-Host - outputs a string to the screen
Write-Output - passes the value further, if there is nothing further, then outputs the textual representation of the value to the screen, something like Write-Host $Value.toString().

A
Alexander Ruchkin, 2015-01-28
@VoidEx

This function does not return anything, but only prints the result to the console.
If you change the last line to just $DataSet.Tables[0].rows.count, then you can write
$x = allCalls

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question