2017-01-25 2 views
0

몇 가지 특정 서버에 대한 특정 서비스를 확인하고 출력을 동일한 형식의 테이블에 표시하려고합니다. 여러 테이블을 만들거나 내가 원하는 방식으로 마지막 테이블 만 표시 할 수있었습니다. 내 의도는 모두 같은 테이블에 표시하는 것입니다.여러 소스에서 제대로 작동하기 위해 형식 테이블 가져 오기

Get-Service "ServiceA", "ServiceB", "ServiceC" -ComputerName SERVER1 
Get-Service "ServiceD", "ServiceE", "ServiceF" -ComputerName SERVER2 | 
    Format-Table -Property MachineName, Status, Name, DisplayName -Auto 

동일한 형식의 테이블에 SERVER1 및 SERVER2를 포함하려면 어떻게합니까? 위의 예에서는 SERVER2에 대해 형식이 지정된 테이블 만 표시됩니까? 내가 해봤

다른 방법은

Get-Service "ServiceA", "ServiceB", "ServiceC" -ComputerName SERVER1 | 
    Format-Table -Property MachineName, Status, Name, DisplayName -Auto 
Get-Service "ServiceD", "ServiceE", "ServiceF" -ComputerName SERVER2 | 
    Format-Table -Property MachineName, Status, Name, DisplayName -Auto 

이었다하지만 난 싶습니다처럼 그런 식으로는 하나의 모든 정보가 생성 된 두 개의 서로 다른 테이블, 그리고 없습니다.

여섯 개의 다른 서버에서 differente 서비스를 확인해야하지만, 두 가지만 있으면이 스크립트에서 내 어려움을 예시하는 것으로 충분합니다.

+0

+ (가져 오기 서비스 "("ServiceA ","ServiceB ","ServiceC "-computername SERVER1 서비스를 받기) ServiceD ","ServiceE ","ServiceF "-computername SERVER2) | ft는 작동합니다. –

답변

1

당신이 그들 모두가 한 테이블에 표시 할 경우, 결과의 모든를 보낼 필요 동시에 Format-Table에 넘어서. Format-Table을 두 번 호출하면 두 개의 별도 테이블이 생성됩니다.

다행히도 PowerShell을 사용하면 명령 결과를 변수에 저장 한 다음 나중에 쉽게 사용할 수 있습니다. 당신이해야 할 일은

은과 같이, 그 안에 당신의 Get-Service 모든 명령을 저장 한 후 결과를 보유하고있는 변수를 만들 수 있습니다 :

#take the output and store it in $services 
$services = get-service bits,winrm -computername ServerA 
#add the output of the next to $services as well 
$services += get-service AdobeARMservice,ALG -computername ServerB 

#finally, make one big table to display it all 
$services |Format-Table -Property MachineName, Status, Name, DisplayName -auto 

MachineName Status Name   DisplayName        
----------- ------ ----   -----------        
ServerA  Running bits   Background Intelligent Transfer Service 
ServerA  Running winrm   Windows Remote Management (WS-Management) 
ServerB  Running AdobeARMservice Adobe Acrobat Update Service    
ServerB  Stopped ALG    Application Layer Gateway Service  

당신이 너무 멀리이 토끼 구멍 아래로 가기 전에를, Format-Table은 콘솔에서 볼 때만 의미가 있습니다. FT으로 만든 테이블을 가져 와서 .csv 파일로 내보낼 수는 없습니다. 콘솔에서보기 만해도 괜찮다면이 방법이 효과적입니다. 대신 배열이나 ArrayList를 채우기의

+1

'$ s'는'$ services'가 아니겠습니까? – sodawillow

+0

네, 맞습니다! 여기에 게시하는 것을 잊어 버렸습니다. – FoxDeploy

+0

예제를 사용하면 출력에 색상을 지정할 수 있습니까? 멈추다가 빨강을, 달리기 위해 녹색을 좋아합니까? 그렇다면 어떻게? 미안하지만, 정말 멍청한 질문 이군요.하지만 여기서 아기 발판을 잡고 있습니다. – biocoma

2

이처럼 할 수있는 :

# create array 
$services = @() 

# add items to array 
$services += Get-Service spooler 
$services += Get-Service wsearch 

# format array 
$services | Format-Table -Property MachineName, Status, Name, DisplayName -AutoSize 

또는 같은

:

# create list 
$services = New-Object System.Collections.ArrayList 

# add items to list 
$services.Add($(Get-Service spooler)) | Out-Null 
$services.Add($(Get-Service wsearch)) | Out-Null 

# display list 
$services | Format-Table -Property MachineName, Status, Name, DisplayName -AutoSize 
+0

첫 번째 예를 통해 해봤지만 예상대로 작동했습니다. 다른 질문을 할 수 있습니까? 출력 첫 번째 예제를 사용하여 출력을 빨간색으로, 빨간색으로, 녹색으로 실행하려면 녹색으로 표시 할 수 있습니까? – biocoma

+0

PS 콘솔은 실제로 이런 식으로 사용되지 않습니다. 대신 Excel 또는 HTML 형식으로 결과를 내보내고 예쁜 형식을 지정할 수 있습니다. – sodawillow

1

, 당신은 단순히 하나의 형식 - 테이블에 모두 출력을 대조하는 하위 표현을 사용할 수 있습니다

$(
    get-service "ServiceA", "ServiceB", "ServiceC" -computername SERVER1 
    get-service "ServiceD", "ServiceE", "ServiceF" -computername SERVER2 
) | Format-Table -Property MachineName, Status, Name, DisplayName -auto 
2

귀하의 예 때문에 들어, 원하는 결과를 생성하지 않는 첫 번째 예제는 두 번째 Get-Service의 출력 만 Format-Table에 들어가고 두 번째 예제에서는 두 개의 별도 테이블이 생성됩니다.

Get-Service cmdlet의 documentation을 살펴 경우에 당신은 당신이 간단하게 할 수있는 모든 컴퓨터에서 동일한 서비스를 확인하고 싶은 경우에, 그래서 모두 -Name-ComputerName가 입력으로 문자열 배열을 가지고 있음을 알 수 있습니다 이 같은 :

$servers = 'SERVER1', 'SERVER2' 
$services = 'ServiceA', 'ServiceB', 'ServiceC' 

Get-Service $services -ComputerName $servers | 
    Format-Table -Property MachineName, Status, Name, DisplayName -AutoSize 

당신은 내가 해시 테이블

$services = @{ 
    'SERVER1' = 'ServiceA', 'ServiceB', 'ServiceC' 
    'SERVER2' = 'ServiceD', 'ServiceE', 'ServiceF' 
} 

으로 서버에 서비스를 매핑하고 Get-Service을 실행하려는 각 서버에서 다른 서비스를 확인하려면 이 같은 ForEach-Object 루프에서 :

$services.Keys | ForEach-Object { 
    Get-Service $services[$_] -ComputerName $_ 
} | Format-Table -Property MachineName, Status, Name, DisplayName -AutoSize 

또는 같은

:

$services.GetEnumerator() | ForEach-Object { 
    Get-Service $_.Value -ComputerName $_.Name 
} | Format-Table -Property MachineName, Status, Name, DisplayName -AutoSize 
관련 문제