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

How to easily create log files

$
0
0

Hi Mr.Experts,

I need the script to create a log file that will show the whole process and errors if occurred.

how do I do this ? Please find the below script.

 

$Servers= import-csv "C:\testcounter.csv"
$MasterArray = @()
ForEach($Server in $Servers)
{
$ObjectName=$Server.ObjectName
$CounterName=$Server.CounterName
$InstanceName=$Server.InstanceName
$Server=$Server.Server
$connected = Test-Connection $server -count 1 -quiet
If($connected)
{
$server,$connected | Out-File C:\Connected_server.txt
$Result=Get-Counter -Counter "\\$server\$ObjectName($InstanceName)\$CounterName"

$CounterSamples = $Result | % {$_.CounterSamples}

#Splitting the get-counter output to the desired format

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

}
}
else
{
$server,$connected | Out-File C:\NotConnected_Server.txt
}
}
$MasterArray | Export-Csv Performance_All.csv -Notype
}


Viewing all articles
Browse latest Browse all 6937

Trending Articles