Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
Fix PSAvoidTrailingWhitespace; Log file path
  • Loading branch information
aaronparker committed Jun 30, 2024
1 parent e71de65 commit c618d72
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 16 deletions.
34 changes: 19 additions & 15 deletions src/Install-Defaults.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,19 @@ function Write-ToEventLog {
[Parameter()]$Property,

[Parameter()]
[ValidateNotNullOrEmpty()] $Object,

[Parameter()]
[ValidateNotNullOrEmpty()]
[System.String] $LogPath = "$Env:ProgramData\image-customise\CustomisedDefaults.log"
[ValidateNotNullOrEmpty()] $Object
)
begin {
if (-not(Test-Path -Path $(Split-Path -Path $LogPath -Parent))) {
New-Item -Path $(Split-Path -Path $LogPath -Parent) -ItemType "Directory" -Force | Out-Null
if (Test-Path -Path "$Env:ProgramData\Microsoft\IntuneManagementExtension\Logs") {
# If we're running under Intune, put the log file in the IntuneManagementExtension folder
$LogPath = "$Env:ProgramData\Microsoft\IntuneManagementExtension\Logs\CustomisedDefaults.log"
}
else {
# Otherwise, put the log file in the ProgramData\image-customise folder
$LogPath = "$Env:ProgramData\image-customise\CustomisedDefaults.log"
if (-not(Test-Path -Path "$Env:ProgramData\image-customise")) {
New-Item -Path "$Env:ProgramData\image-customise" -ItemType "Directory" -Force | Out-Null
}
}
}
process {
Expand All @@ -78,15 +82,15 @@ function Write-ToEventLog {

# Create a log entry
$Message = "$Property; $($Item.Name), $($Item.Value), $($Item.Result)"
$Content = "<![LOG[$Message]LOG]!>" +`
"<time=`"$(Get-Date -Format "HH:mm:ss.ffffff")`" " +`
"date=`"$(Get-Date -Format "M-d-yyyy")`" " +`
"component=`"Customised defaults`" " +`
"context=`"$([System.Security.Principal.WindowsIdentity]::GetCurrent().Name)`" " +`
"type=`"$($Item.Result)`" " +`
"thread=`"$([Threading.Thread]::CurrentThread.ManagedThreadId)`" " +`
$Content = "<![LOG[$Message]LOG]!>" + `
"<time=`"$(Get-Date -Format "HH:mm:ss.ffffff")`" " + `
"date=`"$(Get-Date -Format "M-d-yyyy")`" " + `
"component=`"Customised defaults`" " + `
"context=`"$([System.Security.Principal.WindowsIdentity]::GetCurrent().Name)`" " + `
"type=`"$($Item.Result)`" " + `
"thread=`"$([Threading.Thread]::CurrentThread.ManagedThreadId)`" " + `
"file=`"`">"

# Write the line to the log file
Add-Content -Path $LogPath -Value $Content
}
Expand Down
8 changes: 7 additions & 1 deletion tests/Install-Defaults.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Describe "Install script execution validation" {
$Script = Get-ChildItem -Path $([System.IO.Path]::Combine($Path, "src")) -Include "Install-Defaults.ps1" -Recurse
}

Context "Validate <script.Name>." {
Context "Validate <script.Name>" {
It "<script.Name> should execute OK" {
Push-Location -Path $([System.IO.Path]::Combine($Path, "src"))
$params = @{
Expand All @@ -34,6 +34,12 @@ Describe "Install script execution validation" {
Pop-Location
}
}

Context "Validate log file" {
It "Log file should exist" {
Test-Path -Path "$Env:ProgramData\image-customise\CustomisedDefaults.log" | Should -BeTrue
}
}
}

Describe "Feature update script copy works" {
Expand Down

0 comments on commit c618d72

Please sign in to comment.