pixel0815
Goto Top

Get-PerformanceCounterNameByID - CSV schreiben und nach 10 Stunden per Mail versenden

Guten Tag,

ich habe ein sehr schönes Werk entdeckt.

Skript zum Messen der CPU-Last für Windows

Ich würde dieses gerne nutzen, allerdings bekomme ich es nicht hin das eine CSV vernünftig geschrieben wird.
Das Skript sollte alle 5 min laufen, eine CSV schreiben mit -append und der inhalt wird im zweifel auch per 2. Task per Mail versendet zur Auswertung.
Kann mir jemand helfen?

Gruß
Pixel

Function Get-PerformanceCounterNameByID {
  param([UInt32]$id)
  $buff = New-Object System.Text.StringBuilder(1024)
  [UInt32]$bSize = $buff.Capacity
  Add-Type -MemberDefinition '[DllImport("pdh.dll", SetLastError=true, CharSet=CharSet.Unicode)] public static extern UInt32 PdhLookupPerfNameByIndex(string szMachineName, uint dwNameIndex, System.Text.StringBuilder szNameBuffer, ref uint pcchNameBufferSize);' -Name PerfCounter -Namespace Util  
  $result = [Util.PerfCounter]::PdhLookupPerfNameByIndex($env:COMPUTERNAME, $id, $buff, [Ref]$bSize)
  if ($result -eq 0){
    $buff.ToString().Substring(0, $bSize-1)
  }else{
    Throw 'Get-PerformanceCounterNameById : Konnte lokalisierten Namen nicht finden. Überprüfen sie die übergebene ID.'  
  }
}


function sendmail($body)
{
    $SmtpClient = new-object system.net.mail.smtpClient
    $MailMessage = New-Object system.net.mail.mailmessage
    $SmtpClient.Host = "xxxxxxxi"  
    $mailmessage.from = "xxx"  
    $mailmessage.To.add("xxxxxx")  
    $mailmessage.Subject = "CPU Werte"  
    $MailMessage.IsBodyHtml = $true
    $mailmessage.Body = $body
    $smtpclient.Send($mailmessage)
}



function Get-PerformanceCounterIdByName {
    param([Parameter(Mandatory=$true)][string]$Name)
    $counters = (Get-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Perflib\CurrentLanguage' -Name Counter).Counter  
    $index = [array]::IndexOf($counters,$Name)
    if ($index -ne -1){
        return $counters[($index-1)]
    }else{
        throw 'Get-PerformanceCounterIdByName: ID für diesen Namen wurde nicht gefunden'  
    }
}

$root = Get-PerformanceCounterIdByName -Name 'Prozess'  
$sub = Get-PerformanceCounterIdByName -Name 'Prozessorzeit (%)'  

$counter = "\$(Get-PerformanceCounterNameById $root)(*)\$(Get-PerformanceCounterNameByID $sub)"  

Get-Counter -Counter $counter -EA Ignore | select  -Expand CounterSamples |  sort CookedValue -Desc | select -first 10 | ft InstanceName,@{n='CPU';e={($_.Cookedvalue/100).toString('P')}} -AutoSize  

Content-Key: 311449

Url: https://administrator.de/contentid/311449

Ausgedruckt am: 19.03.2024 um 07:03 Uhr

Mitglied: 129813
Lösung 129813 01.08.2016 aktualisiert um 13:15:01 Uhr
Goto Top
Where's the problem face-smile, simply switch the format-table expression to a select statement and then export ...
Get-Counter -Counter $counter -EA Ignore | select  -Expand CounterSamples |  sort CookedValue -Desc | select -first 10 | select InstanceName,@{n='CPU';e={($_.Cookedvalue/100).toString('P')}} | export-csv 'c:\data.csv' -delimiter ';' -NoType -Append -Encoding UTF8  
Regards
Mitglied: pixel0815
pixel0815 01.08.2016 um 13:33:21 Uhr
Goto Top
Hi Highload,

nooo i have this in the csv file

"ClassId2e4f51ef21dd47e99d3c952918aff9cd";"pageHeaderEntry";"pageFooterEntry";"autosizeInfo";"shapeInfo";"groupingEntry"  
"033ecb2bc07a4d43b5ef94ed5a35d280";;;;"Microsoft.PowerShell.Commands.Internal.Format.TableHeaderInfo";  
"9e210fe47d09416682b841769c78b8a3";;;;;  
"27c87ef9bbda4f709f6b4002fa4af63c";;;;;  
"27c87ef9bbda4f709f6b4002fa4af63c";;;;;  
"27c87ef9bbda4f709f6b4002fa4af63c";;;;;  
"27c87ef9bbda4f709f6b4002fa4af63c";;;;;  
"27c87ef9bbda4f709f6b4002fa4af63c";;;;;  
"27c87ef9bbda4f709f6b4002fa4af63c";;;;;  
"27c87ef9bbda4f709f6b4002fa4af63c";;;;;  
"27c87ef9bbda4f709f6b4002fa4af63c";;;;;  
"27c87ef9bbda4f709f6b4002fa4af63c";;;;;  
"27c87ef9bbda4f709f6b4002fa4af63c";;;;;  
"4ec4f0187cb04f4cb6973460dfe252df";;;;;  
"cf522b78d86c486691226b40aa69e95c";;;;;  
Mitglied: 129813
129813 01.08.2016 aktualisiert um 13:35:28 Uhr
Goto Top
Then you did it wrong! Look carefully above at my code, dont use format-table cmdlets before exporting in the pipeline!!
Mitglied: pixel0815
pixel0815 02.08.2016 um 08:40:29 Uhr
Goto Top
Yes !! face-smile))))) Thank you face-smile))))