2016-08-26 71 views
2

我使用這個腳本從多個服務器腳本獲得

$Output = 'C:\temp\Result.txt' 
$ServerList = Get-Content 'C:\temp\Serverlist.txt' 
$CPUPercent = @{ 
    Label = 'CPUUsed' 
    Expression = { 
    $SecsUsed = (New-Timespan -Start $_.StartTime).TotalSeconds 
    [Math]::Round($_.CPU * 10/$SecsUsed) 
    } 
} 
Foreach ($ServerNames in $ServerList) { 
    Invoke-Command -ComputerName $ServerNames -ScriptBlock { 
    Get-Process | Select-Object -Property Name, CPU, $CPUPercent, Description | Sort-Object -Property CPUUsed -Descending | Select-Object -First 15 | Format-Table -AutoSize | Out-File $Output -Append 
    } 
} 

獲得CPU使用率CPU使用率和我收到錯誤

無法綁定參數參數「文件路徑」,因爲它是空。 + CategoryInfo:InvalidData:(:) [出文件],ParameterBindingValidationException + FullyQualifiedErrorId:ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.OutFileCommand + PSComputerName:服務器

你能請幫助我在這... ?

回答

2

的問題是,你在你通過Invoke-Command在遠程計算機上調用,因此,當該腳本塊在遠程會話中執行不定義腳本塊使用$Output
要修復它,您可以將它作爲參數傳遞給腳本塊,或者在腳本塊中定義它,但我想您寧願將文件寫入啓動客戶端而不是遠程計算機上。因此,而不是在腳本塊使用Out-File你可能想使用它的腳本塊以外,像這樣

$Output = 'C:\temp\Result.txt' 
$ServerList = Get-Content 'C:\temp\Serverlist.txt' 

$ScriptBlock = { 

    $CPUPercent = @{ 
     Label = 'CPUUsed' 
     Expression = { 
     $SecsUsed = (New-Timespan -Start $_.StartTime).TotalSeconds 
     [Math]::Round($_.CPU * 10/$SecsUsed) 
     } 
    } 

    Get-Process | 
     Select-Object -Property Name, CPU, $CPUPercent, Description | 
     Sort-Object -Property CPUUsed -Descending | 
     Select-Object -First 15 
} 

foreach ($ServerNames in $ServerList) { 
    Invoke-Command -ComputerName $ServerNames -ScriptBlock $ScriptBlock | 
    Out-File $Output -Append 
} 

還請注意,我感動的$CPUPercent定義到腳本塊,因爲這從同一個問題的困擾。

+0

謝謝DAXaholic, –

+0

非常感謝...... –