1. I updated the script so that it accepts a path to a CSV file. The script assumes that the CSV file is in the format:
Server, Instance
someServer, someInstance
2. I'm not exactly sure what you mean when you say "same output". If you mean that you don't want "11" being outputted, pipe $adapter.Fill() to Out-Null. I updated, the script with this assumption.
3. I don't know what the effect is when you put a comma after the return keyword. All I know is that when you precede a variable with a comma, it wraps the variable into an array.
param (
[String]$Path
)
function Get-OraTableSpace
{
param (
[String]$Server,
[String]$Instance,
[String]$Port = "1527"
)
[System.Reflection.Assembly]::LoadWithPartialName("System.Data.OracleClient") | Out-Null
$connection = New-Object System.Data.OracleClient.OracleConnection( `
"Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=$server)(PORT=$port)) `
(CONNECT_DATA=(SERVICE_NAME=$instance)));User Id=myuser;Password=mypass;");
$set = New-Object System.Data.DataSet
$query = "select TABLESPACE_NAME , round ( USED_SPACE / (1024 * 1024),2) as USED_SPACE_MB , TABLESPACE_SIZE/1024 TABLESPACE_SIZE_MB , round(USED_PERCENT,2) USED_SPACE_PERCENTAGE from dba_tablespace_usage_metrics"
$adapter = New-Object System.Data.OracleClient.OracleDataAdapter ($query, $connection)
$adapter.Fill($set) | Out-Null
return $set.Tables[0]
}
function Main
{
Import-CSV -Path $Path | ForEach-Object {
$server = $_.Server
$instance = $_.Instance
(Get-OraTableSpace -Server $server -Instance $instance).Rows | ForEach-Object {
if ($_["USED_SPACE_PERCENTAGE"] -gt 30.00)
{
Write-host
("Table Space Name {0} is crossed the threshold current value is {1} and {2} for the database {3} and on the server {4}" -f $_["TABLESPACE_NAME"], $_["USED_SPACE_PERCENTAGE"], $_["USED_SPACE_MB"], $instance, $server)
}
}
}
}
Main