我有一个脚本,我将在下面发布,控制台反馈有点丑陋,我也将在脚本下方发布。我想知道如何以及在哪里放置换行符以确保我得到的控制台反馈看起来更好。我正在使用 PowerShell ISE。
# Service Name and Server Name Variables
$SvcName = 'LoneStar - MessageRouter',
'LoneStar - TransactionHandler'
$SvrName = 'HQDEVAPP004'
#Initialize variables:
[string]$WaitForIt = ""
[string]$Verb = ""
[string]$Result = "FAILED"
$svc = (get-service -computername $SvrName -name $SvcName)
Write-host "$SvcName on $SvrName is $($svc.status)"
Switch ($svc.status) {
'Stopped' {
Write-host "Starting $SvcName..."
$Verb = "start"
$WaitForIt = 'Running'
$svc.Start()}
'Running' {
Write-host "Stopping $SvcName..."
$Verb = "stop"
$WaitForIt = 'Stopped'
$svc.Stop()}
Default {
Write-host "$SvcName is $($svc.status). Taking no action."}
}
if ($WaitForIt -ne "") {
Try {
$svc.WaitForStatus($WaitForIt,'00:00:05')
} Catch {
Write-host "After waiting for 2 minutes, $SvcName failed to $Verb."
}
$svc = (get-service -computername $SvrName -name $SvcName)
if ($svc.status -eq $WaitForIt) {$Result = 'SUCCESS'}
Write-host "$Result`: $SvcName on $SvrName is $($svc.status)"
}
控制台反馈如下:
SUCCESS: LoneStar - MessageRouter LoneStar - TransactionHandler on HQQAAPP004 is Running Running
我希望它看起来更像这样:
SUCCESS:
LoneStar - MessageRouter on HQQAAPP004 is Running
LoneStar - TransactionHandler on HQQAAPP004 is Running
答案1
您只需在字符串中添加 `n 即可添加新行。
问题似乎是
$SvcName = 'LoneStar - MessageRouter',
'LoneStar - TransactionHandler'
#Here the $SvcNAme just prints as a single string 'LoneStar - MessageRouter LoneStar - TransactionHandler'
Try this:
Write-host "$Result`:`n`n $($SvcName[0]) on $SvrName is $($svc[0].status) `n`n$($SvcName[1]) on $SvrName is $($svc[1].status)"