qfarm /load
Befehl zeigt mir die Last von meinen Servern. Ausgabe:
PS> qfarm /load
Server Name Server Load Load Throttling Load Logon Mode
-------------------- ----------- -------------------- ------------------
SERVER-01 400 0 AllowLogons
SERVER-02 1364 OFF AllowLogons
SERVER-03 1364 OFF AllowLogons
SERVER-04 1000 0 AllowLogons
SERVER-05 700 0 AllowLogons
SERVER-06 1200 0 AllowLogons
Ich muss nur die erste Spalte (Servername) und die zweite Spalte (Serverlast) anzeigen und diese durchlaufen, um später eine Logik zu erstellen, aber es scheint, dass die Powershell sie nicht als Objekt mit Eigenschaften sieht:
PS> qfarm /load | Select -ExpandProperty "Server Name"
Select-Object : Property "Server Name" cannot be found.
Gibt es eine andere Möglichkeit, wie einen Tisch oder so?
Eine Möglichkeit, dies zu tun, besteht darin, Objekte aus der Ausgabe des Befehls zu erstellen. Folgendes getestet:
#requires -version 3
# sample data output from command
$sampleData = @"
Server Name Server Load Load Throttling Load Logon Mode
-------------------- ----------- -------------------- ------------------
SERVER-01 400 0 AllowLogons
SERVER-02 1364 OFF AllowLogons
SERVER-03 1364 OFF AllowLogons
SERVER-04 1000 0 AllowLogons
SERVER-05 700 0 AllowLogons
SERVER-06 1200 0 AllowLogons
"@ -split "`n"
$sampleData | Select-Object -Skip 2 | ForEach-Object {
$len = $_.Length
[PSCustomObject] @{
"ServerName" = $_.Substring(0, 22).Trim()
"ServerLoad" = $_.Substring(22, 13).Trim() -as [Int]
"LoadThrottlingLoad" = $_.Substring(35, 22).Trim()
"LogonMode" = $_.Substring(57, $len - 57).Trim()
}
}
In Ihrem Fall sollten Sie in der Lage sein, durch $sampleData
Ihren qfarm load
Befehl zu ersetzen . z.B:
qfarm /load | Select-Object -Skip 2 | ForEach-Object {
...
Dies setzt natürlich keine Leerzeilen in der Ausgabe voraus und dass meine Spaltenpositionen für den Anfang jedes Elements korrekt sind.
PowerShell Version 2-Äquivalent:
#requires -version 2
function Out-Object {
param(
[Collections.Hashtable[]] $hashData
)
$order = @()
$result = @{}
$hashData | ForEach-Object {
$order += ($_.Keys -as [Array])[0]
$result += $_
}
New-Object PSObject -Property $result | Select-Object $order
}
# sample data output from command
$sampleData = @"
Server Name Server Load Load Throttling Load Logon Mode
-------------------- ----------- -------------------- ------------------
SERVER-01 400 0 AllowLogons
SERVER-02 1364 OFF AllowLogons
SERVER-03 1364 OFF AllowLogons
SERVER-04 1000 0 AllowLogons
SERVER-05 700 0 AllowLogons
SERVER-06 1200 0 AllowLogons
"@ -split "`n"
$sampleData | Select-Object -Skip 2 | ForEach-Object {
$len = $_.Length
Out-Object `
@{"ServerName" = $_.Substring(0, 22).Trim()},
@{"ServerLoad" = $_.Substring(22, 13).Trim() -as [Int]},
@{"LoadThrottlingLoad" = $_.Substring(35, 22).Trim()},
@{"LogonMode" = $_.Substring(57, $len - 57).Trim()}
}
Dieser Artikel stammt aus dem Internet. Bitte geben Sie beim Nachdruck die Quelle an.
Bei Verstößen wenden Sie sich bitte [email protected] Löschen.
Lass mich ein paar Worte sagen