Skip to content

Commit 5fcff14

Browse files
Lisa OngHowardWolosky
Lisa Ong
authored andcommitted
skip console messages if the host is non-interactive
consistent casing for $consoleMessage Addressed feedback from @HowardWolosky-MSFT and @danbelcher-MSFT by wrapping Write-Host calls with Write-InteractiveHost, so that calls to Write-Host are only forwarded if the host is interactive. Style feedback from @danbelcher-MSFT Addressed more style feedback from @HowardWolosky-MSFT, bumped patch version Addressed last remaining feedback from @HowardWolosky-MSFT
1 parent aff905a commit 5fcff14

File tree

2 files changed

+72
-15
lines changed

2 files changed

+72
-15
lines changed

StoreBroker/Helpers.ps1

Lines changed: 71 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,6 @@ function Wait-JobWithAnimation
105105
set of configuration options that Wait-Job does.
106106
#>
107107
[CmdletBinding()]
108-
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidUsingWriteHost", "", Justification="This function is intended for human interaction, not for scripting. Write-Host makes the most sense for visible feedback.")]
109108
Param(
110109
[Parameter(Mandatory)]
111110
[string] $JobName,
@@ -125,27 +124,27 @@ function Wait-JobWithAnimation
125124
$iteration = 0
126125
while (((Get-Job -Name $JobName).state -eq 'Running'))
127126
{
128-
Write-Host "`r$($animationFrames[$($iteration % $($animationFrames.Length))]) Elapsed: $([int]($iteration / $framesPerSecond)) second(s) $Description" -NoNewline -f Yellow
127+
Write-InteractiveHost "`r$($animationFrames[$($iteration % $($animationFrames.Length))]) Elapsed: $([int]($iteration / $framesPerSecond)) second(s) $Description" -NoNewline -f Yellow
129128
Start-Sleep -Milliseconds ([int](1000/$framesPerSecond))
130129
$iteration++
131130
}
132131

133132
if ((Get-Job -Name $JobName).state -eq 'Completed')
134133
{
135-
Write-Host "`rDONE - Operation took $([int]($iteration / $framesPerSecond)) second(s) $Description" -NoNewline -f Green
134+
Write-InteractiveHost "`rDONE - Operation took $([int]($iteration / $framesPerSecond)) second(s) $Description" -NoNewline -f Green
136135

137136
# We forcibly set Verbose to false here since we don't need it printed to the screen, since we just did above -- we just need to log it.
138137
Write-Log "DONE - Operation took $([int]($iteration / $framesPerSecond)) second(s) $Description" -Level Verbose -Verbose:$false
139138
}
140139
else
141140
{
142-
Write-Host "`rDONE (FAILED) - Operation took $([int]($iteration / $framesPerSecond)) second(s) $Description" -NoNewline -f Red
141+
Write-InteractiveHost "`rDONE (FAILED) - Operation took $([int]($iteration / $framesPerSecond)) second(s) $Description" -NoNewline -f Red
143142

144143
# We forcibly set Verbose to false here since we don't need it printed to the screen, since we just did above -- we just need to log it.
145144
Write-Log "DONE (FAILED) - Operation took $([int]($iteration / $framesPerSecond)) second(s) $Description" -Level Verbose -Verbose:$false
146145
}
147146

148-
Write-Host ""
147+
Write-InteractiveHost ""
149148
}
150149

151150
function Format-SimpleTableString
@@ -401,7 +400,6 @@ function Write-Log
401400
[CmdletBinding(SupportsShouldProcess)]
402401
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSShouldProcess", "", Justification="Methods called within here make use of PSShouldProcess, and the switch is passed on to them inherently.")]
403402
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidGlobalVars", "", Justification="We use global variables sparingly and intentionally for module configuration, and employ a consistent naming convention.")]
404-
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidUsingWriteHost", "", Justification="We'd like to use Write-Information instead, but it's not supported on PS 4.0 which we need to support.")]
405403
param(
406404
[Parameter(
407405
Mandatory,
@@ -442,21 +440,21 @@ function Write-Log
442440

443441
switch ($Level)
444442
{
445-
'Error' { Write-Error $ConsoleMessage }
446-
'Warning' { Write-Warning $ConsoleMessage }
447-
'Verbose' { Write-Verbose $ConsoleMessage }
448-
'Debug' { Write-Debug $ConsoleMessage }
443+
'Error' { Write-Error $consoleMessage }
444+
'Warning' { Write-Warning $consoleMessage }
445+
'Verbose' { Write-Verbose $consoleMessage }
446+
'Debug' { Write-Debug $consoleMessage }
449447
'Info' {
450448
# We'd prefer to use Write-Information to enable users to redirect that pipe if
451449
# they want, unfortunately it's only available on v5 and above. We'll fallback to
452450
# using Write-Host for earlier versions (since we still need to support v4).
453451
if ($PSVersionTable.PSVersion.Major -ge 5)
454452
{
455-
Write-Information $ConsoleMessage -InformationAction Continue
453+
Write-Information $consoleMessage -InformationAction Continue
456454
}
457455
else
458456
{
459-
Write-Host $ConsoleMessage
457+
Write-InteractiveHost $consoleMessage
460458
}
461459
}
462460
}
@@ -479,7 +477,7 @@ function Write-Log
479477
# Let's do best effort here and if we can't log something, just report
480478
# it and move on.
481479
$output += "This is non-fatal, and your command will continue. Your log file will be missing this entry:"
482-
$output += $ConsoleMessage
480+
$output += $consoleMessage
483481
Write-Warning ($output -join [Environment]::NewLine)
484482
}
485483
else
@@ -645,4 +643,63 @@ function Send-SBMailMessage
645643
}
646644
}
647645
}
648-
}
646+
}
647+
648+
function Write-InteractiveHost
649+
{
650+
<#
651+
.SYNOPSIS
652+
Forwards to Write-Host only if the host is interactive, else does nothing.
653+
654+
.DESCRIPTION
655+
A proxy function around Write-Host that detects if the host is interactive
656+
before calling Write-Host. Use this instead of Write-Host to avoid failures in
657+
non-interactive hosts.
658+
659+
The Git repo for this module can be found here: http://aka.ms/StoreBroker
660+
661+
.EXAMPLE
662+
Write-InteractiveHost "Test"
663+
Write-InteractiveHost "Test" -NoNewline -f Yellow
664+
665+
.NOTES
666+
Boilerplate is generated using these commands:
667+
# $Metadata = New-Object System.Management.Automation.CommandMetaData (Get-Command Write-Host)
668+
# [System.Management.Automation.ProxyCommand]::Create($Metadata) | Out-File temp
669+
#>
670+
671+
[CmdletBinding(
672+
HelpUri='http://go.microsoft.com/fwlink/?LinkID=113426',
673+
RemotingCapability='None')]
674+
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidUsingWriteHost", "", Justification="This provides a wrapper around Write-Host. In general, we'd like to use Write-Information, but it's not supported on PS 4.0 which we need to support.")]
675+
param(
676+
[Parameter(
677+
Position=0,
678+
ValueFromPipeline,
679+
ValueFromRemainingArguments)]
680+
[System.Object] $Object,
681+
682+
[switch] $NoNewline,
683+
684+
[System.Object] $Separator,
685+
686+
[System.ConsoleColor] $ForegroundColor,
687+
688+
[System.ConsoleColor] $BackgroundColor
689+
)
690+
691+
# Determine if the host is interactive
692+
if ([Environment]::UserInteractive -and `
693+
![Bool]([Environment]::GetCommandLineArgs() -like '-noni*') -and `
694+
(Get-Host).Name -ne 'Default Host')
695+
{
696+
# Special handling for OutBuffer (generated for the proxy function)
697+
$outBuffer = $null
698+
if ($PSBoundParameters.TryGetValue('OutBuffer', [ref]$outBuffer))
699+
{
700+
$PSBoundParameters['OutBuffer'] = 1
701+
}
702+
703+
Write-Host @PSBoundParameters
704+
}
705+
}

StoreBroker/StoreBroker.psd1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
CompanyName = 'Microsoft Corporation'
77
Copyright = 'Copyright (C) Microsoft Corporation. All rights reserved.'
88

9-
ModuleVersion = '1.1.1'
9+
ModuleVersion = '1.1.2'
1010
Description = 'Provides command-line access to the Windows Store Submission REST API.'
1111

1212
RootModule = 'StoreIngestionApi'

0 commit comments

Comments
 (0)