% RAM livre de máquinas remotas

0

Lendo em superusuário / stackoverflow Não consegui criar um script que produza de forma ativa o% -free ram (como visto no gerenciador de tarefas do Windows) de várias máquinas remotas (por exemplo, server1-server4) . Aqui está o que eu tenho em termos de código, a plataforma deve ser Windows, ou CMD ou PowerShell (ou similar):

1) CMD, não conseguiu o% de RAM livre (ou seja, não pôde acessar o ram 'ocupado' para calcular 'ocupado / total * 100'. source ):
wmic /NODE:"servername" /USER:"yourdomain\administrator" OS GET FreePhysicalMemory

2) powershell ( source ), não conseguiu a memória de máquinas remotas (ou seja, não é possível obter Get-WmiObject da máquina remota):

$system = Get-WmiObject win32_OperatingSystem
$totalPhysicalMem = $system.TotalVisibleMemorySize
$freePhysicalMem = $system.FreePhysicalMemory
$usedPhysicalMem = $totalPhysicalMem - $freePhysicalMem
$usedPhysicalMemPct = [math]::Round(($usedPhysicalMem / $totalPhysicalMem) * 100,1)

qualquer ajuda apreciada

    
por user2305193 05.09.2017 / 13:33

2 respostas

1

Eu criei este script bastante generalizável, que dá uma porcentagem de RAM livre. Para porcentagem de memória RAM usada, simplesmente adicione $a=$b-$a .

$a=0
$b=0
$strComputer = "localhost"
$a=Get-WmiObject Win32_OperatingSystem -ComputerName $strComputer | fl *freePhysical* | Out-String
$b=Get-WmiObject Win32_OperatingSystem -ComputerName $strComputer | fl *totalvisiblememory* | Out-String
$a = $a -replace '\D+(\d+)','$1'
$b = $b  -replace '\D+(\d+)','$1'
[math]::Round($a/$b*10000)/100
    
por 05.09.2017 / 18:42
1

Usando o método de Conectando-se ao WMI remotamente com o PowerShell de link

Eu usei o .NET para formatar números exemplo daqui. link

$Servers = @("localhost")

foreach ($Server in $Servers){

$OS = get-wmiobject -Namespace "root\cimv2" -Class Win32_OperatingSystem -Impersonation 3 -computername $Server

foreach ($Item in $OS){
        $RAM = "{0:N6}" -f ($Item.TotalVisibleMemorySize)/1kB
        $FREE = "{0:N6}" -f ($Item.FreePhysicalMemory)/1kB
        $Server + "'t" + "{0:P0}" -f ($FREE/$RAM)
        }
}

EDITAR

Para resolver problemas com formatos numéricos nas versões PS e .net, você pode usar o formato de ponto decimal ou fixo em vez do número para corrigir. link

$Servers = @("localhost")

foreach ($Server in $Servers){

$OS = get-wmiobject -Namespace "root\cimv2" -Class Win32_OperatingSystem -Impersonation 3 -computername $Server

foreach ($Item in $OS){
        $RAM = "{0:D6}" -f ($Item.TotalVisibleMemorySize)/1kB
        $FREE = "{0:D6}" -f ($Item.FreePhysicalMemory)/1kB
        $Server + "'t" + "{0:P0}" -f ($FREE/$RAM)
        }
}

OR

$Servers = @("localhost")

foreach ($Server in $Servers){

$OS = get-wmiobject -Namespace "root\cimv2" -Class Win32_OperatingSystem -Impersonation 3 -computername $Server

foreach ($Item in $OS){
        $RAM = "{0:F6}" -f ($Item.TotalVisibleMemorySize)/1kB
        $FREE = "{0:F6}" -f ($Item.FreePhysicalMemory)/1kB
        $Server + "'t" + "{0:P0}" -f ($FREE/$RAM)
        }
}
    
por 07.09.2017 / 13:58