Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -214,3 +214,6 @@ pip-log.txt

#Mr Developer
.mr.developer.cfg

# The default build.ps1 output folders
[0-9]*\.[0-9]*\.[0-9]*/
29 changes: 29 additions & 0 deletions Build.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
[CmdletBinding()]
param(
# A version number to update the output with (uses gitversion by default)
$Version = $(gitversion -showvariable nugetversion),

# The output folder (defaults to the version number)
$OutputDirectory = $("$PSScriptRoot\$(($Version -split '[-+]',2)[0])"),

# If set, removes the output folder without prompting!
[switch]$Force
)

$VersionPrefix, $VersionSuffix = $Version -split '[-+]', 2

if (Test-Path $OutputDirectory) {
Remove-Item $OutputDirectory -Recurse -Confirm:(!$Force)
}

Copy-Item $PSScriptRoot\Module\PowerShellLogging -Destination $OutputDirectory -recurse
Set-Content $OutputDirectory\PowerShellLogging.psd1 (
(Get-Content $OutputDirectory\PowerShellLogging.psd1) -replace
"(ModuleVersion\s+=\s+)'.*'", "`$1'$VersionPrefix'" -replace
"(Prerelease\s+=\s+)'.*'", "`$1'$VersionSuffix'"
)

dotnet build -c Release -p:VersionPrefix=$VersionPrefix -p:VersionSuffix=$VersionSuffix

Get-ChildItem bin/Release -recurse -filter *.dll |
Move-Item -Destination $OutputDirectory\
154 changes: 78 additions & 76 deletions HostIOInterceptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ private HostIOInterceptor()
#endregion

#region Properties

public bool Paused
{
get { return paused; }
Expand Down Expand Up @@ -93,13 +93,25 @@ public void AttachToHost(PSHost host)

const BindingFlags flags = BindingFlags.Instance | BindingFlags.NonPublic;

object uiRef = host.GetType().GetField("internalUIRef", flags).GetValue(host);
object ui = uiRef.GetType().GetProperty("Value", flags).GetValue(uiRef, null);

FieldInfo externalUIField = ui.GetType().GetField("externalUI", flags);
// When PowerShell went open source, they renamed the private variables to have _underbarPrefixes
if (host.Version >= new Version(6, 0))
{
object uiRef = host.GetType().GetField("_internalUIRef", flags)?.GetValue(host);
object ui = uiRef.GetType().GetProperty("Value", flags).GetValue(uiRef, null);
FieldInfo externalUIField = ui.GetType().GetField("_externalUI", flags);
externalUI = (PSHostUserInterface)externalUIField.GetValue(ui);
externalUIField.SetValue(ui, this);
}
else
{
// Try the WindowsPowerShell version:
object uiRef = host.GetType().GetField("internalUIRef", flags).GetValue(host);
object ui = uiRef.GetType().GetProperty("Value", flags).GetValue(uiRef, null);
FieldInfo externalUIField = ui.GetType().GetField("externalUI", flags);
externalUI = (PSHostUserInterface)externalUIField.GetValue(ui);
externalUIField.SetValue(ui, this);
}

externalUI = (PSHostUserInterface)externalUIField.GetValue(ui);
externalUIField.SetValue(ui, this);
this.host = host;
}

Expand All @@ -109,14 +121,27 @@ public void DetachFromHost()

const BindingFlags flags = BindingFlags.Instance | BindingFlags.NonPublic;

object uiRef = host.GetType().GetField("internalUIRef", flags).GetValue(host);
object ui = uiRef.GetType().GetProperty("Value", flags).GetValue(uiRef, null);

FieldInfo externalUIField = ui.GetType().GetField("externalUI", flags);

if (externalUIField.GetValue(ui) == this)
// When PowerShell went open source, they renamed the private variables to have _underbarPrefixes
if (host.Version >= new Version(6, 0))
{
externalUIField.SetValue(ui, externalUI);
object uiRef = host.GetType().GetField("_internalUIRef", flags)?.GetValue(host);
object ui = uiRef.GetType().GetProperty("Value", flags).GetValue(uiRef, null);
FieldInfo externalUIField = ui.GetType().GetField("_externalUI", flags);
if (externalUIField.GetValue(ui) == this)
{
externalUIField.SetValue(ui, externalUI);
}
}
else
{
// Try the WindowsPowerShell version:
object uiRef = host.GetType().GetField("internalUIRef", flags).GetValue(host);
object ui = uiRef.GetType().GetProperty("Value", flags).GetValue(uiRef, null);
FieldInfo externalUIField = ui.GetType().GetField("externalUI", flags);
if (externalUIField.GetValue(ui) == this)
{
externalUIField.SetValue(ui, externalUI);
}
}

externalUI = null;
Expand All @@ -129,7 +154,7 @@ public override Dictionary<string, PSObject> Prompt(string caption,
{
if (externalUI == null)
{
throw new InvalidOperationException();
throw new InvalidOperationException("Unable to prompt user in headless session");
}

Dictionary<string, PSObject> result = externalUI.Prompt(caption, message, descriptions);
Expand All @@ -146,7 +171,7 @@ public override int PromptForChoice(string caption,
{
if (externalUI == null)
{
throw new InvalidOperationException();
throw new InvalidOperationException("Unable to prompt user for choice in headless session");
}

int result = externalUI.PromptForChoice(caption, message, choices, defaultChoice);
Expand All @@ -163,7 +188,7 @@ public override PSCredential PromptForCredential(string caption,
{
if (externalUI == null)
{
throw new InvalidOperationException();
throw new InvalidOperationException("Unable to prompt user for credential in headless session");
}

PSCredential result = externalUI.PromptForCredential(caption, message, userName, targetName);
Expand All @@ -182,7 +207,7 @@ public override PSCredential PromptForCredential(string caption,
{
if (externalUI == null)
{
throw new InvalidOperationException();
throw new InvalidOperationException("Unable to prompt user for credential in headless session");
}

PSCredential result = externalUI.PromptForCredential(caption,
Expand All @@ -201,7 +226,7 @@ public override string ReadLine()
{
if (externalUI == null)
{
throw new InvalidOperationException();
throw new InvalidOperationException("Unable to ReadLine from host in headless session");
}

string result = externalUI.ReadLine();
Expand All @@ -215,7 +240,7 @@ public override SecureString ReadLineAsSecureString()
{
if (externalUI == null)
{
throw new InvalidOperationException();
throw new InvalidOperationException("Unable to ReadLineAsSecureString from host in headless session");
}

return externalUI.ReadLineAsSecureString();
Expand Down Expand Up @@ -246,13 +271,11 @@ public void RemoveSubscriber(IHostIOSubscriber subscriber)

public override void Write(string value)
{
if (externalUI == null)
if (externalUI != null)
{
throw new InvalidOperationException();
externalUI.Write(value);
}

externalUI.Write(value);

if (!paused)
{
writeCache.Append(value);
Expand All @@ -261,13 +284,11 @@ public override void Write(string value)

public override void Write(ConsoleColor foregroundColor, ConsoleColor backgroundColor, string value)
{
if (externalUI == null)
if (externalUI != null)
{
throw new InvalidOperationException();
externalUI.Write(foregroundColor, backgroundColor, value);
}

externalUI.Write(foregroundColor, backgroundColor, value);

if (!paused)
{
writeCache.Append(value);
Expand All @@ -276,45 +297,36 @@ public override void Write(ConsoleColor foregroundColor, ConsoleColor background

public override void WriteDebugLine(string message)
{
if (externalUI == null)
{
throw new InvalidOperationException();
}

string[] lines = message.Split(new[] { "\r\n", "\n" }, StringSplitOptions.None);
foreach (string line in lines)
{
string temp = line;
SendToSubscribers(s => s.WriteDebug(temp.TrimEnd() + "\r\n"));
}

externalUI.WriteDebugLine(message);
if (externalUI != null)
{
externalUI.WriteDebugLine(message);
}
}

public override void WriteErrorLine(string message)
{
if (externalUI == null)
{
throw new InvalidOperationException();
}

string[] lines = message.Split(new[] { "\r\n", "\n" }, StringSplitOptions.None);
foreach (string line in lines)
{
string temp = line;
SendToSubscribers(s => s.WriteError(temp.TrimEnd() + "\r\n"));
}

externalUI.WriteErrorLine(message);
if (externalUI != null)
{
externalUI.WriteErrorLine(message);
}
}

public override void WriteLine()
{
if (externalUI == null)
{
throw new InvalidOperationException();
}

string[] lines = writeCache.ToString().Split(new[] { "\r\n", "\n" }, StringSplitOptions.None);
foreach (string line in lines)
{
Expand All @@ -323,16 +335,13 @@ public override void WriteLine()
}

writeCache.Length = 0;
externalUI.WriteLine();
if (externalUI != null) {
externalUI.WriteLine();
}
}

public override void WriteLine(string value)
{
if (externalUI == null)
{
throw new InvalidOperationException();
}

string[] lines = (writeCache + value).Split(new[] { "\r\n", "\n" }, StringSplitOptions.None);
foreach (string line in lines)
{
Expand All @@ -341,16 +350,13 @@ public override void WriteLine(string value)
}

writeCache.Length = 0;
externalUI.WriteLine(value);
if (externalUI != null) {
externalUI.WriteLine(value);
}
}

public override void WriteLine(ConsoleColor foregroundColor, ConsoleColor backgroundColor, string value)
{
if (externalUI == null)
{
throw new InvalidOperationException();
}

string[] lines = (writeCache + value).Split(new[] { "\r\n", "\n" }, StringSplitOptions.None);
foreach (string line in lines)
{
Expand All @@ -359,53 +365,49 @@ public override void WriteLine(ConsoleColor foregroundColor, ConsoleColor backgr
}

writeCache.Length = 0;
externalUI.WriteLine(foregroundColor, backgroundColor, value);
if (externalUI != null){
externalUI.WriteLine(foregroundColor, backgroundColor, value);
}
}

public override void WriteProgress(long sourceId, ProgressRecord record)
{
if (externalUI == null)
{
throw new InvalidOperationException();
}

SendToSubscribers(s => s.WriteProgress(sourceId, record));

externalUI.WriteProgress(sourceId, record);
if (externalUI != null)
{
externalUI.WriteProgress(sourceId, record);
}
}

public override void WriteVerboseLine(string message)
{
if (externalUI == null)
{
throw new InvalidOperationException();
}

string[] lines = message.Split(new[] { "\r\n", "\n" }, StringSplitOptions.None);
foreach (string line in lines)
{
string temp = line;
SendToSubscribers(s => s.WriteVerbose(temp.TrimEnd() + "\r\n"));
}

externalUI.WriteVerboseLine(message);
if (externalUI != null)
{
externalUI.WriteVerboseLine(message);
}
}

public override void WriteWarningLine(string message)
{
if (externalUI == null)
{
throw new InvalidOperationException();
}

string[] lines = message.Split(new[] { "\r\n", "\n" }, StringSplitOptions.None);
foreach (string line in lines)
{
string temp = line;
SendToSubscribers(s => s.WriteWarning(temp.TrimEnd() + "\r\n"));
}

externalUI.WriteWarningLine(message);
if (externalUI != null)
{
externalUI.WriteWarningLine(message);
}
}

#endregion
Expand Down
9 changes: 6 additions & 3 deletions Module/PowerShellLogging/PowerShellLogging.psd1
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
@{
ModuleToProcess = 'PowerShellLogging.psm1'
ModuleVersion = '1.3.0'
NestedModules = @( "PowerShellLoggingModule.dll" )
ModuleVersion = '1.4.0'
GUID = '345320b5-bdc3-4ab6-a13e-fcb019362fe6'
CompanyName = 'Home'
CopyRight = 'Copyright 2017 David Wyatt'
Expand All @@ -14,12 +15,14 @@
AliasesToExport = '*'

FileList = @('PowerShellLogging.psm1','PowerShellLogging.psd1','PowerShellLoggingModule.dll','en-US\about_PowerShellLogging.help.txt','en-US\PowerShellLoggingModule.dll-help.xml')

PrivateData = @{
PSData = @{
Prerelease = ''
LicenseUri = 'http://www.apache.org/licenses/LICENSE-2.0.txt'
ProjectUri = 'https://github.com/dlwyatt/PowerShellLoggingModule'
ReleaseNotes = 'Added DateTimeFormat parameter to Enable-LogFile'
ReleaseNotes = 'Added support for PowerShell 6 & 7
Added support for logging from runspaces in pools (i.e. without an actual host)'
}
}
}
Loading