Skip to content

Commit 12eda38

Browse files
Improved retry / disabled WhatIf
1 parent fcc2740 commit 12eda38

File tree

3 files changed

+39
-127
lines changed

3 files changed

+39
-127
lines changed

PSWriteColor.psd1

Lines changed: 12 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -1,123 +1,21 @@
1-
#
2-
# Module manifest for module 'PSWriteColor'
3-
#
4-
# Generated by: Przemyslaw Klys
5-
#
6-
# Generated on: 20.07.2020
7-
#
8-
9-
@{
10-
11-
# Script module or binary module file associated with this manifest.
12-
RootModule = 'PSWriteColor.psm1'
13-
14-
# Version number of this module.
15-
ModuleVersion = '0.87.2'
16-
17-
# Supported PSEditions
18-
CompatiblePSEditions = 'Desktop', 'Core'
19-
20-
# ID used to uniquely identify this module
21-
GUID = '0b0ba5c5-ec85-4c2b-a718-874e55a8bc3f'
22-
23-
# Author of this module
1+
@{
2+
AliasesToExport = 'Write-Colour'
243
Author = 'Przemyslaw Klys'
25-
26-
# Company or vendor of this module
4+
CmdletsToExport = @()
275
CompanyName = 'Evotec'
28-
29-
# Copyright statement for this module
30-
Copyright = '(c) 2011 - 2020 Przemyslaw Klys @ Evotec. All rights reserved.'
31-
32-
# Description of the functionality provided by this module
6+
CompatiblePSEditions = @('Desktop', 'Core')
7+
Copyright = '(c) 2011 - 2021 Przemyslaw Klys @ Evotec. All rights reserved.'
338
Description = 'Write-Color is a wrapper around Write-Host allowing you to create nice looking scripts, with colorized output. It provides easy manipulation of colors, logging output to file (log) and nice formatting options out of the box.'
34-
35-
# Minimum version of the Windows PowerShell engine required by this module
36-
PowerShellVersion = '5.1'
37-
38-
# Name of the Windows PowerShell host required by this module
39-
# PowerShellHostName = ''
40-
41-
# Minimum version of the Windows PowerShell host required by this module
42-
# PowerShellHostVersion = ''
43-
44-
# Minimum version of Microsoft .NET Framework required by this module. This prerequisite is valid for the PowerShell Desktop edition only.
45-
# DotNetFrameworkVersion = ''
46-
47-
# Minimum version of the common language runtime (CLR) required by this module. This prerequisite is valid for the PowerShell Desktop edition only.
48-
# CLRVersion = ''
49-
50-
# Processor architecture (None, X86, Amd64) required by this module
51-
# ProcessorArchitecture = ''
52-
53-
# Modules that must be imported into the global environment prior to importing this module
54-
# RequiredModules = @()
55-
56-
# Assemblies that must be loaded prior to importing this module
57-
# RequiredAssemblies = @()
58-
59-
# Script files (.ps1) that are run in the caller's environment prior to importing this module.
60-
# ScriptsToProcess = @()
61-
62-
# Type files (.ps1xml) to be loaded when importing this module
63-
# TypesToProcess = @()
64-
65-
# Format files (.ps1xml) to be loaded when importing this module
66-
# FormatsToProcess = @()
67-
68-
# Modules to import as nested modules of the module specified in RootModule/ModuleToProcess
69-
# NestedModules = @()
70-
71-
# Functions to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no functions to export.
729
FunctionsToExport = 'Write-Color'
73-
74-
# Cmdlets to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no cmdlets to export.
75-
CmdletsToExport = @()
76-
77-
# Variables to export from this module
78-
# VariablesToExport = @()
79-
80-
# Aliases to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no aliases to export.
81-
AliasesToExport = 'Write-Colour'
82-
83-
# DSC resources to export from this module
84-
# DscResourcesToExport = @()
85-
86-
# List of all modules packaged with this module
87-
# ModuleList = @()
88-
89-
# List of all files packaged with this module
90-
# FileList = @()
91-
92-
# Private data to pass to the module specified in RootModule/ModuleToProcess. This may also contain a PSData hashtable with additional module metadata used by PowerShell.
10+
GUID = '0b0ba5c5-ec85-4c2b-a718-874e55a8bc3f'
11+
ModuleVersion = '0.87.3'
12+
PowerShellVersion = '5.1'
9313
PrivateData = @{
94-
9514
PSData = @{
96-
97-
# Tags applied to this module. These help with module discovery in online galleries.
98-
Tags = 'Write-Host', 'Color', 'Colour', 'Terminal', 'Console', 'Logging', 'Prompt',
99-
'Write-Color', 'Windows', 'MacOS', 'Linux'
100-
101-
# A URL to the license for this module.
102-
# LicenseUri = ''
103-
104-
# A URL to the main website for this project.
15+
Tags = @('Write-Host', 'Color', 'Colour', 'Terminal', 'Console', 'Logging', 'Prompt', 'Write-Color', 'Windows', 'MacOS', 'Linux')
10516
ProjectUri = 'https://github.com/EvotecIT/PSWriteColor'
106-
107-
# A URL to an icon representing this module.
10817
IconUri = 'https://evotec.xyz/wp-content/uploads/2018/10/PSWriteColor.png'
109-
110-
# ReleaseNotes of this module
111-
# ReleaseNotes = ''
112-
113-
} # End of PSData hashtable
114-
115-
} # End of PrivateData hashtable
116-
117-
# HelpInfo URI of this module
118-
# HelpInfoURI = ''
119-
120-
# Default prefix for commands exported from this module. Override the default prefix using Import-Module -Prefix.
121-
# DefaultCommandPrefix = ''
122-
18+
}
19+
}
20+
RootModule = 'PSWriteColor.psm1'
12321
}

Public/Write-Color.ps1

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ function Write-Color {
6868
[alias ('L')] [string] $LogFile = '',
6969
[Alias('DateFormat', 'TimeFormat')][string] $DateTimeFormat = 'yyyy-MM-dd HH:mm:ss',
7070
[alias ('LogTimeStamp')][bool] $LogTime = $true,
71+
[int] $LogRetry = 2,
7172
[ValidateSet('unknown', 'string', 'unicode', 'bigendianunicode', 'utf8', 'utf7', 'utf32', 'ascii', 'default', 'oem')][string]$Encoding = 'Unicode',
7273
[switch] $ShowTime,
7374
[switch] $NoNewLine
@@ -108,14 +109,24 @@ function Write-Color {
108109
for ($i = 0; $i -lt $Text.Length; $i++) {
109110
$TextToFile += $Text[$i]
110111
}
111-
try {
112-
if ($LogTime) {
113-
"[$([datetime]::Now.ToString($DateTimeFormat))] $TextToFile" | Out-File -FilePath $LogFile -Encoding $Encoding -Append -ErrorAction Stop
114-
} else {
115-
"$TextToFile" | Out-File -FilePath $LogFile -Encoding $Encoding -Append -ErrorAction Stop
112+
$Saved = $false
113+
$Retry = 0
114+
Do {
115+
$Retry++
116+
try {
117+
if ($LogTime) {
118+
"[$([datetime]::Now.ToString($DateTimeFormat))] $TextToFile" | Out-File -FilePath $LogFile -Encoding $Encoding -Append -ErrorAction Stop -WhatIf:$false
119+
} else {
120+
"$TextToFile" | Out-File -FilePath $LogFile -Encoding $Encoding -Append -ErrorAction Stop -WhatIf:$false
121+
}
122+
$Saved = $true
123+
} catch {
124+
if ($Saved -eq $false -and $Retry -eq $LogRetry) {
125+
$PSCmdlet.WriteError($_)
126+
} else {
127+
Write-Warning "Write-Color - Couldn't write to log file $($_.Exception.Message). Retrying... ($Retry/$LogRetry)"
128+
}
116129
}
117-
} catch {
118-
$PSCmdlet.WriteError($_)
119-
}
130+
} Until ($Saved -eq $true -or $Retry -ge $LogRetry)
120131
}
121132
}

README.md

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,21 @@ Write-Color is a wrapper around Write-Host allowing you to create nice looking s
2727

2828
## ChangeLog
2929

30+
- 0.87.3 - 2021.04.12
31+
- 💡 Disabled `WhatIf` functionality for Out-File which would prevent logging to file
32+
- 💡 Added `LogRetry` (2) to provide option to retry logging to file up to X number of times in case issue with saving to file occurs (race condition)
3033
- 0.87.2 - 2020.07.20
31-
- [x] Added a space for LogTime and ShowTime
32-
- [x] Signed module
34+
- 📦 Added a space for LogTime and ShowTime
35+
- 📦 Signed module
3336
- 0.87.1 - 2020.06.19
34-
- [x] Resolves issue with nuget download (changed version from 0.87 to 0.87.1). As per [link](https://devblogs.microsoft.com/devops/versioning-nuget-packages-cd-1/) it requires version to be longer.
37+
- 🐛 Resolves issue with nuget download (changed version from 0.87 to 0.87.1). As per [link](https://devblogs.microsoft.com/devops/versioning-nuget-packages-cd-1/) it requires version to be longer.
3538
- 0.87.0 - 2020.01.12
36-
- [x] Throw errors when can't save to file with LogFile. Before it would use Write-Output which could deliver unpredictable results
39+
- 🐛 Throw errors when can't save to file with LogFile. Before it would use Write-Output which could deliver unpredictable results
3740

3841
# Quick install
3942

4043
```powershell
41-
Install-Module -Name "PSWriteColor"
44+
Install-Module -Name "PSWriteColor" -Force
4245
```
4346

4447
# Examples

0 commit comments

Comments
 (0)