Invoke-Command $env:COMPUTERNAME

I wrote a script that browse the virtual directories in a server .

Invoke-Command -ComputerName server-01,server-02 {
Write-Host “`n”
write-host “$env:COMPUTERNAME” -ForegroundColor Green
Import-Module webadministration
$IISSitename = (Get-ChildItem -Path IIS:\Sites | where name -notlike ‘def*’).name
foreach ($IISs in $IISSitename) {
$IISPath = (Get-item -Path IIS:\Sites$IISs).PhysicalPath
$fullport = Get-WebBinding |where ItemXPath -Match $IISs | select bindingInformation -Unique
$port = $fullport -replace “[^0-9]” | select $_ -First 1
$VirtualDName = (Get-ChildItem -Path IIS:\Sites$IISs | where ElementTagName -eq ‘application’).nameforeach ($vdn in $VirtualDName) {
$files= Get-ChildItem -Path $IISPath$vdn | where { $.Name -like ‘.svc’ -or $.Name -like ‘.asmx’ -or $_.Name -like '.ashx’ } |select -ExpandProperty Name
foreach ($f in $files) {
Invoke-WebRequest “http://localhost:$port/$vdn/$f” |select -ExpandProperty statuscode -OutVariable statuscode | Out-Null
Write-Host "$env:COMPUTERNAME $IISs - $vdn - $f , $statuscode "
}
}
}
}

I want to create header before the beginning of each computer output ,

so I added : write-host “$env:COMPUTERNAME” -ForegroundColor Green

the problem is that it shows the headers at the beginning of the output :

server-01

server-02

server-01 External - app1 - service1.svc , 200

server-01 External - app2 - service1.svc , 200

server-02 External - app1 - service1.svc , 200

server-02 External - app2 - service1.svc , 200

 

I want to change it to :

server-01

server-01 External - app1 - service1.svc , 200

server-01 External - app2 - service1.svc , 200

server-02

server-02 External - app1 - service1.svc , 200

server-02 External - app2 - service1.svc , 200

 

 

Could you please format your code as code using the code tag button (pre) on the icon bar of the post editor? Thanks. Example data schould be formatted as code as well.

And try to avoid too much white space. It makes the code harder to read.