Skip to content

Commit

Permalink
Improved retry / disabled WhatIf
Browse files Browse the repository at this point in the history
  • Loading branch information
PrzemyslawKlys committed Apr 12, 2021
1 parent fcc2740 commit 12eda38
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 127 deletions.
126 changes: 12 additions & 114 deletions PSWriteColor.psd1
Original file line number Diff line number Diff line change
@@ -1,123 +1,21 @@
#
# Module manifest for module 'PSWriteColor'
#
# Generated by: Przemyslaw Klys
#
# Generated on: 20.07.2020
#

@{

# Script module or binary module file associated with this manifest.
RootModule = 'PSWriteColor.psm1'

# Version number of this module.
ModuleVersion = '0.87.2'

# Supported PSEditions
CompatiblePSEditions = 'Desktop', 'Core'

# ID used to uniquely identify this module
GUID = '0b0ba5c5-ec85-4c2b-a718-874e55a8bc3f'

# Author of this module
@{
AliasesToExport = 'Write-Colour'
Author = 'Przemyslaw Klys'

# Company or vendor of this module
CmdletsToExport = @()
CompanyName = 'Evotec'

# Copyright statement for this module
Copyright = '(c) 2011 - 2020 Przemyslaw Klys @ Evotec. All rights reserved.'

# Description of the functionality provided by this module
CompatiblePSEditions = @('Desktop', 'Core')
Copyright = '(c) 2011 - 2021 Przemyslaw Klys @ Evotec. All rights reserved.'
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.'

# Minimum version of the Windows PowerShell engine required by this module
PowerShellVersion = '5.1'

# Name of the Windows PowerShell host required by this module
# PowerShellHostName = ''

# Minimum version of the Windows PowerShell host required by this module
# PowerShellHostVersion = ''

# Minimum version of Microsoft .NET Framework required by this module. This prerequisite is valid for the PowerShell Desktop edition only.
# DotNetFrameworkVersion = ''

# Minimum version of the common language runtime (CLR) required by this module. This prerequisite is valid for the PowerShell Desktop edition only.
# CLRVersion = ''

# Processor architecture (None, X86, Amd64) required by this module
# ProcessorArchitecture = ''

# Modules that must be imported into the global environment prior to importing this module
# RequiredModules = @()

# Assemblies that must be loaded prior to importing this module
# RequiredAssemblies = @()

# Script files (.ps1) that are run in the caller's environment prior to importing this module.
# ScriptsToProcess = @()

# Type files (.ps1xml) to be loaded when importing this module
# TypesToProcess = @()

# Format files (.ps1xml) to be loaded when importing this module
# FormatsToProcess = @()

# Modules to import as nested modules of the module specified in RootModule/ModuleToProcess
# NestedModules = @()

# 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.
FunctionsToExport = 'Write-Color'

# 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.
CmdletsToExport = @()

# Variables to export from this module
# VariablesToExport = @()

# 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.
AliasesToExport = 'Write-Colour'

# DSC resources to export from this module
# DscResourcesToExport = @()

# List of all modules packaged with this module
# ModuleList = @()

# List of all files packaged with this module
# FileList = @()

# 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.
GUID = '0b0ba5c5-ec85-4c2b-a718-874e55a8bc3f'
ModuleVersion = '0.87.3'
PowerShellVersion = '5.1'
PrivateData = @{

PSData = @{

# Tags applied to this module. These help with module discovery in online galleries.
Tags = 'Write-Host', 'Color', 'Colour', 'Terminal', 'Console', 'Logging', 'Prompt',
'Write-Color', 'Windows', 'MacOS', 'Linux'

# A URL to the license for this module.
# LicenseUri = ''

# A URL to the main website for this project.
Tags = @('Write-Host', 'Color', 'Colour', 'Terminal', 'Console', 'Logging', 'Prompt', 'Write-Color', 'Windows', 'MacOS', 'Linux')
ProjectUri = 'https://github.com/EvotecIT/PSWriteColor'

# A URL to an icon representing this module.
IconUri = 'https://evotec.xyz/wp-content/uploads/2018/10/PSWriteColor.png'

# ReleaseNotes of this module
# ReleaseNotes = ''

} # End of PSData hashtable

} # End of PrivateData hashtable

# HelpInfo URI of this module
# HelpInfoURI = ''

# Default prefix for commands exported from this module. Override the default prefix using Import-Module -Prefix.
# DefaultCommandPrefix = ''

}
}
RootModule = 'PSWriteColor.psm1'
}
27 changes: 19 additions & 8 deletions Public/Write-Color.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ function Write-Color {
[alias ('L')] [string] $LogFile = '',
[Alias('DateFormat', 'TimeFormat')][string] $DateTimeFormat = 'yyyy-MM-dd HH:mm:ss',
[alias ('LogTimeStamp')][bool] $LogTime = $true,
[int] $LogRetry = 2,
[ValidateSet('unknown', 'string', 'unicode', 'bigendianunicode', 'utf8', 'utf7', 'utf32', 'ascii', 'default', 'oem')][string]$Encoding = 'Unicode',
[switch] $ShowTime,
[switch] $NoNewLine
Expand Down Expand Up @@ -108,14 +109,24 @@ function Write-Color {
for ($i = 0; $i -lt $Text.Length; $i++) {
$TextToFile += $Text[$i]
}
try {
if ($LogTime) {
"[$([datetime]::Now.ToString($DateTimeFormat))] $TextToFile" | Out-File -FilePath $LogFile -Encoding $Encoding -Append -ErrorAction Stop
} else {
"$TextToFile" | Out-File -FilePath $LogFile -Encoding $Encoding -Append -ErrorAction Stop
$Saved = $false
$Retry = 0
Do {
$Retry++
try {
if ($LogTime) {
"[$([datetime]::Now.ToString($DateTimeFormat))] $TextToFile" | Out-File -FilePath $LogFile -Encoding $Encoding -Append -ErrorAction Stop -WhatIf:$false
} else {
"$TextToFile" | Out-File -FilePath $LogFile -Encoding $Encoding -Append -ErrorAction Stop -WhatIf:$false
}
$Saved = $true
} catch {
if ($Saved -eq $false -and $Retry -eq $LogRetry) {
$PSCmdlet.WriteError($_)
} else {
Write-Warning "Write-Color - Couldn't write to log file $($_.Exception.Message). Retrying... ($Retry/$LogRetry)"
}
}
} catch {
$PSCmdlet.WriteError($_)
}
} Until ($Saved -eq $true -or $Retry -ge $LogRetry)
}
}
13 changes: 8 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,21 @@ Write-Color is a wrapper around Write-Host allowing you to create nice looking s

## ChangeLog

- 0.87.3 - 2021.04.12
- 💡 Disabled `WhatIf` functionality for Out-File which would prevent logging to file
- 💡 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)
- 0.87.2 - 2020.07.20
- [x] Added a space for LogTime and ShowTime
- [x] Signed module
- 📦 Added a space for LogTime and ShowTime
- 📦 Signed module
- 0.87.1 - 2020.06.19
- [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.
- 🐛 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.
- 0.87.0 - 2020.01.12
- [x] Throw errors when can't save to file with LogFile. Before it would use Write-Output which could deliver unpredictable results
- 🐛 Throw errors when can't save to file with LogFile. Before it would use Write-Output which could deliver unpredictable results

# Quick install

```powershell
Install-Module -Name "PSWriteColor"
Install-Module -Name "PSWriteColor" -Force
```

# Examples
Expand Down

0 comments on commit 12eda38

Please sign in to comment.