Charlie
Charlie

Reputation: 129

Ouput Server Name Along With InstallState of Service

I'm pretty new to Powershell. I have the following simple little script:

$serversall = (Get-Content ".\Servers.txt")  
foreach($vm in $serversall){ 
  Get-WindowsFeature Web-Server -ComputerName $vm | 
  Select-Object InstallState |
  Export-Csv -path E:\Scripts\Output\IISWebServersStatus.csv -Append
}

This just gives me one column to let me know if the status is "Installed" or "Available". I want another column that would also show the server name next to the status of the service. Any idea how I would accomplish that?

Upvotes: 1

Views: 233

Answers (1)

arco444
arco444

Reputation: 22861

I would recommend the following approach:

$serversall = (Get-Content ".\Servers.txt")
$output = @()
foreach($vm in $serversall) { 
  $installed = (Get-WindowsFeature Web-Server -ComputerName $vm).InstallState
  $output += New-Object PSCustomObject -Property @{'ComputerName'=$vm; 'Status'=$installed }
}

$output | Export-Csv -path E:\Scripts\Output\IISWebServersStatus.csv -Append

Create an array, $output that you can use to store all the information. On each loop iteration, build an object that holds the the server name and the install state and append it to the output array.

After the loop is done, write the output array to the csv file.

Doing one file write at the end will save on i/o operations, so could save time. In this example, Get-WindowsFeature is a relatively slow operation so it probably makes little difference, but good to understand the theory nonetheless.

Upvotes: 1

Related Questions