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: 0 additions & 3 deletions src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -148,12 +148,9 @@ internal static int Start(

try
{
// We need to read the settings file before we create the console host
string[] tempArgs = new string[args.GetLength(0)];
args.CopyTo(tempArgs, 0);

CommandLineParameterParser.EarlyParse(tempArgs);

// We might be able to ignore console host creation error if we are running in
// server mode, which does not require a console.
HostException hostException = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ public sealed class UnmanagedPSEntry
public static int Start(string consoleFilePath, [MarshalAs(UnmanagedType.LPArray, ArraySubType = UnmanagedType.LPWStr, SizeParamIndex = 2)]string[] args, int argc)
#pragma warning restore 1573
{
// We need to read the settings file before we create the console host
Microsoft.PowerShell.CommandLineParameterParser.EarlyParse(args);
System.Management.Automation.Runspaces.EarlyStartup.Init();

#if !UNIX
Expand Down
61 changes: 60 additions & 1 deletion test/powershell/Host/Logging.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,11 @@ function WriteLogSettings
$values['LogLevel'] = $LogLevel.ToString()
}

if($IsWindows)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this need to be conditional?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The setting is only valid on windows

{
$values["Microsoft.PowerShell:ExecutionPolicy"] = "RemoteSigned"
}

if($ScriptBlockLogging.IsPresent)
{
$powerShellPolicies = @{
Expand Down Expand Up @@ -222,7 +227,7 @@ $pid
}
}

Describe 'Basic os_log tests on MacOS' -Tag @('Feature','RequireSudoOnUnix') {
Describe 'Basic os_log tests on MacOS' -Tag @('CI','RequireSudoOnUnix') {
BeforeAll {
[bool] $IsSupportedEnvironment = $IsMacOS
[bool] $persistenceEnabled = $false
Expand Down Expand Up @@ -358,3 +363,57 @@ $pid
}
}
}

Describe 'Basic EventLog tests on Windows' -Tag @('CI','RequireAdminOnWindows') {
BeforeAll {
[bool] $IsSupportedEnvironment = $IsWindows
[string] $powershell = Join-Path -Path $PSHome -ChildPath 'pwsh'
$scriptBlockLoggingCases = @(
@{
name = 'normal script block'
script = "Write-Verbose 'testheader123' ;Write-verbose 'after'"
expectedText="Write-Verbose 'testheader123' ;Write-verbose 'after'`r`n"
}
)

if ($IsSupportedEnvironment)
{
& "$PSHome\RegisterManifest.ps1"
}
}

BeforeEach {
if ($IsSupportedEnvironment)
{
# generate a unique log application id
[string] $logId = [Guid]::NewGuid().ToString('N')

$logName = 'PowerShellCore'

# get log items after current time.
[DateTime] $after = [DateTime]::Now
Clear-PSEventLog -Name "$logName/Operational"
}
}

It 'Verifies scriptblock logging: <name>' -Skip:(!$IsSupportedEnvironment) -TestCases $scriptBlockLoggingCases {
param(
[string] $script,
[string] $expectedText,
[string] $name
)
$configFile = WriteLogSettings -ScriptBlockLogging -LogId $logId
$testFileName = 'test01.ps1'
$testScriptPath = Join-Path -Path $TestDrive -ChildPath $testFileName
$script | Out-File -FilePath $testScriptPath -Force
$null = & $powershell -NoProfile -SettingsFile $configFile -Command $testScriptPath

$created = Wait-PSWinEvent -FilterHashtable @{ ProviderName=$logName; Id = 4104 } `
-PropertyName Message -PropertyValue $expectedText

$created | Should -Not -BeNullOrEmpty
$created.Properties[0].Value | Should -Be 1
$created.Properties[1].Value | Should -Be 1
$created.Properties[2].Value | Should -Be $expectedText
}
}
24 changes: 16 additions & 8 deletions test/tools/Modules/PSSysLog/PSSysLog.psd1
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
@{
ModuleVersion = '1.0.0'
GUID = '56b63338-045c-4697-a24b-5a756268c8b2'
Author = 'PowerShell'
CompanyName = 'Microsoft Corporation'
Copyright = 'Copyright (c) Microsoft Corporation. All rights reserved.'
Description = 'Provides a reader for powershell syslog and os_log entries'
RootModule = 'PSSysLog.psm1'
FunctionsToExport = @('Get-PSSysLog', 'Get-PSOsLog', 'Export-PSOsLog', 'Get-OsLogPersistence', 'Set-OsLogPersistence')
ModuleVersion = '1.0.0'
GUID = '56b63338-045c-4697-a24b-5a756268c8b2'
Author = 'PowerShell'
CompanyName = 'Microsoft Corporation'
Copyright = 'Copyright (c) Microsoft Corporation. All rights reserved.'
Description = 'Provides a reader for powershell syslog and os_log entries'
RootModule = 'PSSysLog.psm1'
FunctionsToExport = @(
'Get-PSSysLog'
'Get-PSOsLog'
'Export-PSOsLog'
'Get-OsLogPersistence'
'Set-OsLogPersistence'
'Clear-PSEventLog'
'Wait-PSWinEvent'
)
}
103 changes: 102 additions & 1 deletion test/tools/Modules/PSSysLog/PSSysLog.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -1030,4 +1030,105 @@ function Get-OsLogPersistence

#region os_log support

Export-ModuleMember -Function Get-PSSysLog, Get-PSOsLog, Export-PSOsLog, Get-OsLogPersistence, Set-OsLogPersistence

#region eventlog support

# utility function to clear a particular log
function Clear-PSEventLog
{
param([Parameter(Mandatory=$true)][string]$Name)

wevtutil cl $Name 2>$null
}

# Waits on a windows event with a property with a particular value
function Wait-PSWinEvent
{
param(
[Parameter()]
$FilterHashtable,

[Parameter(ParameterSetName = "ByPropertyName")]
$propertyName,

[Parameter(ParameterSetName = "ByPropertyIndex")]
$propertyIndex,

[Parameter()]
$propertyValue,

[Parameter()]
$timeout = 30,

[Parameter()]
$pause = 1,

[Parameter()]
[Switch]
$All
)

    $startTime = [DateTime]::Now
$lastFoundCount = 0;

    do
    {
        sleep $pause

$recordsToReturn = @()

        foreach ($thisRecord in (get-winevent -FilterHashtable $filterHashtable -Oldest 2>$null))
        {
if($PsCmdlet.ParameterSetName -eq "ByPropertyName")
{
if ($thisRecord."$propertyName" -like "*$propertyValue*")
{
if($All)
{
$recordsToReturn += $thisRecord
}
else
{
return $thisRecord
}
}
}

if($PsCmdlet.ParameterSetName -eq "ByPropertyIndex")
{
if ($thisRecord.Properties[$propertyIndex].Value -eq $propertyValue)
{
if($All)
{
$recordsToReturn += $thisRecord
}
else
{
return $thisRecord
}
}
}
        }

if($recordsToReturn.Count -gt 0)
{
if($recordsToReturn.Count -eq $lastFoundCount)
{
return $recordsToReturn
}

$lastFoundCount = $recordsToReturn.Count
}
    } while (([DateTime]::Now - $startTime).TotalSeconds -lt $timeout)
}
#endregion eventlog support

Export-ModuleMember -Function @(
'Clear-PSEventLog'
'Export-PSOsLog'
'Get-OsLogPersistence'
'Get-PSOsLog'
'Get-PSSysLog'
'Set-OsLogPersistence'
'Wait-PSWinEvent'
)