mrdon
September 29, 2020, 2:51am
1
Hi
I am trying to view the services in RichTextBox but failed to do so.
Can someone point me the right direction? Below is the code.
function checksqlinfo {
$outputBox.Clear()
$computers = $InputBox.lines.Split("`n")
foreach ($computer in $computers) {
try {
$outputBox.AppendText("$computer`n")
$sqlservices = Get-WmiObject Win32_Service -ComputerName $computer -ErrorAction Stop | Where-Object { ($_.Displayname -like "*sql*") } | Select-Object Displayname, Name, Startmode, State, PSComputerName | Format-Table -AutoSize
$outputBox.AppendText("$sqlservices`n")
}
catch {
$outputBox.Appendtext("$computer`n")
$outputBox.AppendText("Unable to view SQL Services`n")
Write-Output "`n"
}
Finally {
$Error.Clear()
}
}
}
First of all, Format-Table
is used only to format output in the interactive console session.
One if the options would be to loop through your $sqlservices
object and append/format every value manually using spaces new lines and tabs.
Another option would be to use RTF (Reach Text Format) codes to generate a table inside reach text box:
Hope that helps.
mrdon
October 7, 2020, 12:22am
3
Hi Andy,
Do you mind showing me example for the first method? loop through $sqlservices object and append/format every value manually using spaces new lines and tabs.
Thank you
mrdon
October 7, 2020, 3:45am
4
[quote quote=259882]First of all, Format-Table is used only to format output in the interactive console session.
One if the options would be to loop through your $sqlservices object and append/format every value manually using spaces new lines and tabs.
Another option would be to use RTF (Reach Text Format) codes to generate a table inside reach text box:
https://docs.microsoft.com/en-us/openspecs/exchange_server_protocols/ms-oxrtfcp/65dfe2df-1b69-43fc-8ebd-21819a7463fb
Hope that helps.
[/quote]
Hi Andy
I came out with below code. I am trying to use the first method u suggest. It is still not working.
Function sqlinfo {
$outputBox.Clear()
$computers = $InputBox.lines.Split("`n")
foreach ($computer in $computers) {
try {
$outputBox.AppendText("$computer`n")
$sqlservices = Get-WmiObject Win32_Service -ComputerName $computer -ErrorAction Stop | Where-Object { ($_.Displayname -like "*sql*") }
$props = @{
'Server Name' = $computer;
'Service Name' = $sqlservices.Name;
'State' = $sqlservices.StartMode
}
$obj = New-Object -TypeName PSCustomObject -Property $props
$outputBox.AppendText("$obj`n")
}
catch {
$outputBox.AppendText("Error`n")
}
finally {
$Error.Clear()
}
}
}
Hello Muhammad,
Here is quick and dirty way to accomplish this:
Function sqlinfo {
$outputBox.Clear()
$computers = $InputBox.lines.Split("`n")
#Output Table Headers
$C1Header="ComputerName"
$C2Header="ServiceName"
$C3Header="StartMode"
#Output Table Width
$ColumnWidth=25
#Add Headers to OutputBox
$outputBox.AppendText("$C1Header$(' '*($ColumnWidth-$C1Header.length))<code>t$C2Header$(' '*($ColumnWidth-$C2Header.Length))</code>t$C3Header$(' '*($ColumnWidth-$C3Header.Length))`n")
foreach ($computer in $computers) {
try {
$sqlservices = Get-WmiObject Win32_Service -ComputerName $computer -ErrorAction Stop | Where-Object { ($_.Displayname -like "*sql*") }
#Add Values to the output
$outputBox.AppendText("$computer$(' '*($ColumnWidth-$computer.length))`t$($sqlservices.Name)$(' '*$($ColumnWidth-$sqlservices.Name.length))`t$($sqlservices.StartMode)$(' '*$($ColumnWidth-$sqlservices.StartMode.length))`n")
}
catch {
$outputBox.AppendText("Error`n")
}
finally {
$Error.Clear()
}
}
}
Probably it would be better to at least wrap all this formatting stuff in separate function.
Hope that helps.
As discussed added loop for multiple $sqlservices:
Function sqlinfo {
$outputBox.Clear()
$computers = $InputBox.lines.Split("`n")
#Output Table Headers
$C1Header="ComputerName"
$C2Header="ServiceName"
$C3Header="StartMode"
#Output Table Width
$ColumnWidth=25
#Add Headers to OutputBox
$outputBox.AppendText("$C1Header$(' '*($ColumnWidth-$C1Header.length))`t$C2Header$(' '*($ColumnWidth-$C2Header.Length))`t$C3Header$(' '*($ColumnWidth-$C3Header.Length))`n")
foreach ($computer in $computers) {
try {
$sqlservices = Get-WmiObject Win32_Service -ComputerName $computer -ErrorAction Stop | Where-Object { ($_.Displayname -like "*sql*") }
foreach($sqls in $sqlservices){
#Add Values to the output
$outputBox.AppendText("$computer$(' '*($ColumnWidth-$computer.length))`t$($sqlservices.Name)$(' '*$($ColumnWidth-$sqlservices.Name.length))`t$($sqlservices.StartMode)$(' '*$($ColumnWidth-$sqlservices.StartMode.length))`n")
}
}
catch {
$outputBox.AppendText("Error`n")
}
finally {
$Error.Clear()
}
}
}
Hope that helps.