Reputation: 303
I'm trying to modify a script that is supposed to list out all the computers within a domain, and if there is someone logged in on said computer it should show the username for the account.
The script works fine, but i got a little esthetic problem. Is there a way to turn the return data (for those computers and servers thats online) into another color?
Here is my current script:
function Check-Domain {
Get-ADComputer -Filter * |
Select-Object -ExpandProperty Name |
ForEach-Object {
$computer = $_
$pingme = Test-Connection -ComputerName $computer -Quiet -Count 1
if ($pingme -eq $true) {
Invoke-Command -ComputerName $computer -ScriptBlock {
Get-WmiObject Win32_ComputerSystem |
Select-Object Username, Name }
} else {
Write-Host "$computer - OFF" -ForegroundColor Red
}
} | Format-Table
}
Upvotes: 1
Views: 2209
Reputation: 13176
You can edit this part - this stores the result of Invoke-Command
in a variable, and outputs it with the color you like with Write-Host
:
if ($pingme -eq $true) {
$result = Invoke-Command -ComputerName $computer -ScriptBlock {
Get-WmiObject Win32_ComputerSystem | Select-Object Username, Name
}
Write-Host $result -ForegroundColor Green
}
Upvotes: 0
Reputation: 36297
Sure, that's easy enough. Just wrap the command up in a sub-expression and use Write-Host
just like you do with the offline servers.
{Write-Host $(Invoke-Command -ComputerName $computer -ScriptBlock { Get-WmiObject win32_computersystem | Select-Object username, name}) -ForegroundColor Green}
It'll execute the script inside the $()
first, and then apply it's output to the Write-Host
so that you can format it as desired. For output consistency, especially when it's just outputting text to the host, I personally like to use formatted strings. When used in combination with the -NoNewLine
switch for Write-Host
you can get some really sharp looking results.
Upvotes: 2