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

Test Connection

$
0
0

Hi Team,

The below Script creating the performance counters for the multiple server. I need to modify the same If server is offline, Skip the task and continue the same to the next server in the list. But here script creating the csv file without any information if the server is off-line.

*********************************************

I need to add the Test-conncetion command to check server on-line or off-line , Only online server need to check the performance counters.

$PingResult=Test-Connection -quiet -computer $ServerName
if( $PingResult -eq "true")

 

**********Main Script********************************

$ScriptBlock = {

    param
    (
        $Server,
        $ExportCSV
    )

$Counters= import-csv "C:\testcounter.csv"
foreach($Counter in $Counters)
{
$ObjectName=$Counter.ObjectName
$CounterName=$Counter.CounterName
$InstanceName=$Counter.InstanceName
$Result=Get-Counter -Counter "\\$server\$ObjectName($InstanceName)\$CounterName"
}
    $CounterSamples = $Result | % {$_.CounterSamples}

    $MasterArray = @()
    foreach ($CounterSample in $CounterSamples)
    {
        $TempArray = @()
        $TempArray = "" | Select Server, ObjectName, CounterName, InstanceName, SampleValue, DateTime

        $Split = $CounterSample.Path.Remove(0,2)
        $Split = $Split.Split("\")

        $TempArray.Server = $Split[0]
        $TempArray.ObjectName = $Split[1].Split("(")[0]
        $TempArray.CounterName = $Split[2]
        $TempArray.InstanceName = $CounterSample.InstanceName
        $TempArray.SampleValue = $CounterSample.CookedValue
        $TempArray.DateTime = $CounterSample.TimeStamp.ToString("yyyy-MM-dd HH:mm:ss")

        $MasterArray += $TempArray
    }

    $MasterArray | Export-Csv $ExportCSV -NoType
}

$Servers = import-csv "G:\testcounter.csv"
foreach ($Server in $Servers)
{
$server=$server.server
    $ExportCSVFile = "C:\$Server" + "_PerformaneData.csv"
    Start-Job -ScriptBlock $ScriptBlock -ArgumentList @($Server, $ExportCSVFile)
}

 


Viewing all articles
Browse latest Browse all 6937

Trending Articles