2016-08-26 3 views
0

안녕하세요,이 스크립트를 다시 여러 서버에서 실행하고 있습니다. (initially posted here) & 각 특정 서버 이름이 결과에 나타나게하고 싶습니다. 하지만 지금 당장은 각 서버에 대한 사용법 다음에 CPU & Memory Usage &이라는 제목을 사용할 수 있습니다. Pls는 각 서버 이름을 &으로 가져 오는 방법을 알려줍니다. 난 당신이 서버 이름에 루프를 실행하고 볼CPU 및 메모리 사용 스크립트

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

$ScriptBLock = { 
$CPUPercent = @{ 



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


$MemUsage = @{ 
Label ='RAM(MB)' 
Expression = { 
[Math]::Round(($_.WS/1MB),2) 
} 
    } 
Get-Process | Select-Object -Property Name, CPU, $CPUPercent, $MemUsage, 
Description | 
Sort-Object -Property CPUUsed -Descending | 
Select-Object -First 15 | Format-Table -AutoSize 
} 
foreach ($ServerNames in $ServerList) { 
Invoke-Command -ComputerName $ServerNames {Write-Output "CPU & Memory Usage"} 
| Out-File $Output -Append 
Invoke-Command -ScriptBlock $ScriptBLock -ComputerName $ServerNames | 
Out-File $Output -Append 

답변

1

는 ($ ServerNames 각 반복에 대한 각 서버입니다), 이렇게하지 않는 이유는 사용

"Working on $ServerNames.." | Out-File $Output -Append 

첫 번째 줄에 "foreach"문장 다음에? 을 스크립트 블록 추가에 : 당신이 스크립트 블록에이 일단

Write-Output "CPU & Memory Usage" 
hostname | out-file $output -Append # this will throw the server name 

, 당신은 다음과 같이 실행할 수 있습니다 :

Invoke-Command -ScriptBlock $ScriptBLock -ComputerName $ServerList 
어쨌든

, 난 당신이 이런 식으로 스크립트를 변경할 수 있다고 생각

($ ServerList는 "Invoke-Command"가 처리 방법을 알고있는 원래 서버의 배열입니다.)

+0

감사합니다 아미르 ...이 하나의 작업 "Working on $ ServerNames .."| 출력 파일 $ 출력 -Append –

관련 문제