问题
how can we get cpu usage of azure vm to local machine through csv file is it possible..? example:-i would like to get current cpu usage is 50%.
i am able to get single task usage example : task1=0.20 task2=6.98 but i am searching whole thing to get Can any one please help me
i am able to get cpu usage in base machine "wmic cpu get loadpercentage" like the same i am trying for azure vm
Thanks In Advance Giri
回答1:
We can use this command to get the CPU usage:
$cpu = gwmi win32_Processor
$Havecpu = "{0:0.0} %" -f $cpu.LoadPercentage
$Havecpu
Also if you want to remote run this script, we can use Winrm to run it and copy it to local PC:
$username = 'user'
$pass = ConvertTo-SecureString -string 'password' -AsPlainText -Force
$cred = New-Object -typename System.Management.Automation.PSCredential -argumentlist $username, $pass
$s = New-PSSession -ConnectionUri 'http://xx.xx.xx.xx:5985' -Credential $cred -SessionOption (New-PSSessionOption -SkipCACheck -SkipCNCheck -SkipRevocationCheck)
Invoke-Command -Session $s -ScriptBlock {powershell c:\test.ps1 > c:\jason2.csv}
Copy-Item -Path C:\jason2.csv -Destination D:\test\test12.csv -fromSession $s
By the way, here is the test.ps1
script:
$cpu = gwmi win32_Processor
$men = gwmi win32_OperatingSystem
$Disks = gwmi win32_logicaldisk -filter "drivetype=3"
$Havecpu = "{0:0.0} %" -f $cpu.LoadPercentage
$Allmen = "{0:0.0} MB" -f ($men.TotalVisibleMemorySize / 1KB)
$Freemen = "{0:0.0} MB" -f ($men.FreePhysicalMemory / 1KB)
$Permem = "{0:0.0} %" -f ((($men.TotalVisibleMemorySize-$men.FreePhysicalMemory)/$men.TotalVisibleMemorySize)*100)
Write-Host "CPU: $Havecpu"`r`n
Write-Host "Total Mem:$Allmen"`r`n
Write-Host "Left Mem:$Freemen"`r`n
Write-Host "Used Mem:$Permem"`r`n
$IpAdd = (Get-WmiObject -class win32_NetworkAdapterConfiguration -Filter 'ipenabled = "true"').ipaddress[0]
Write-Host "Ipaddress:$IpAdd"`r`n
来源:https://stackoverflow.com/questions/46729247/it-is-possbile-to-get-the-cpu-usage-of-azurevm-throught-powershell-like-metrics