Re: PowerShell writing to console out of order
Posted: Fri Jan 11, 2019 3:15 pm
The following function is s utility function. It does not want to be made to look like a CmdLet. We don't need to add the parameter statements and we don't want it to be complicated.
It is also bad coding form to use return to exit a function. This should be reserved for special circumstances. PowerShell always returns all output to the pipeline.
Here is a good set of articles on style, formatting, comment usage and basic overall design of a PS script.
PowerShell Style Guide]
PowerShell Best Practices
Code: Select all
function Get-Index {
param(
[psobject[]]$Services
)
Write-Host 'Returned services:'
$Services | ForEach-Object{ Write-Host $_.Index $_.Status $_.Name -ForegroundColor green }
Read-Host "Which service would you like to restart? (0 - $($Services.Count))"
}
Here is a good set of articles on style, formatting, comment usage and basic overall design of a PS script.
PowerShell Style Guide]
PowerShell Best Practices