收集内存信息(总量、可用、已用、百分比)导出到csv

#############################脚本功能及说明#################################################
#该脚本用来在各台ERP服务器上创建性能数据收集器收集cpu、memory、disk、netcard per。
############################################################################################

$file_server = "Serverlist.txt"
$result = "MemInfor.csv"
$CurrentPath = $MyInvocation.MyCommand.Path.substring(0,$MyInvocation.MyCommand.Path.LastIndexOf('\')+1)
$server_list = $CurrentPath + $file_server
$memPath = $currentpath + $result
$servers = Get-Content $server_list
$UserName = "administrator"
#$UserName = "erpadmin"
$results=@()
Foreach ($server in $servers)
{
$servername = $server.split(",")[0]
$serverpass = $server.split(",")[1]
#net use \\$servername $serverpass /user:$UserName |out-null
$Password = ConvertTo-SecureString $serverpass -AsPlainText –Force
$cred = New-Object System.Management.Automation.PSCredential($UserName,$Password)
Write-Host $servername -ForegroundColor Green

$ic = invoke-command -ComputerName $servername -Credential $cred -ScriptBlock {
$memorys = Get-WmiObject -Class Win32_PhysicalMemory
$memorysize_sum = $null
Foreach ($memory in $memorys)
{
#$memorylist += ($memory.capacity/1024/1024/1024).tostring("F1")+"GB + "
$memorysize_sum_n += $memory.capacity/1024/1024/1024
}
$memorysize_sum = $memorysize_sum_n

$counter = New-Object Diagnostics.PerformanceCounter
$counter.CategoryName = "Memory"
$counter.CounterName = "Available MBytes"
#$counter.InstanceName = "_Total"
$memorysize_a = ($counter.NextValue()/1024).tostring("f1")
$memorysize_used = $memorysize_sum-$memorysize_a
$percent = "{0:p}" -f ($memorysize_a/$memorysize_sum)
#[string]$memorysize_sum + "," + [string]$memorysize_a + "," + [string]$memorysize_used + "," + [string]$percent
return [string]$memorysize_sum ,[string]$memorysize_a , [string]$memorysize_used , [string]$percent
}
$result=""|select name,mems,mema,memu,memp
$result.name = $servername
$result.mems = $ic[0]
$result.mema = $ic[1]
$result.memu = $ic[2]
$result.memp = $ic[3]
$results +=$result
#$servername + "," + $ic
}
$results |Export-Csv $memPath -NoTypeInformation
#net use * /del /y |out-null
cmd /c pause

 

posted on 2014-05-21 16:01  momingliu11  阅读(421)  评论(0编辑  收藏  举报