Skip to content

Commit

Permalink
Merge pull request #1246 from ykuijs/master
Browse files Browse the repository at this point in the history
Create event log entry when exception occurs
  • Loading branch information
ykuijs authored Oct 22, 2020
2 parents a821727 + 6b893fa commit 1a51937
Show file tree
Hide file tree
Showing 197 changed files with 5,869 additions and 806 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added

- SharePointDsc
- Added logging to the event log when the code throws an exception

## [4.3.0] - 2020-09-30

### Added
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,17 +108,23 @@ function Set-TargetResource
if ($Ensure -eq "Present")
{
Invoke-SPDscCommand -Credential $InstallAccount `
-Arguments $PSBoundParameters `
-Arguments @($PSBoundParameters, $MyInvocation.MyCommand.Source) `
-ScriptBlock {
$params = $args[0]
$eventSource = $args[1]

$webapp = Get-SPWebApplication -IncludeCentralAdministration | Where-Object -FilterScript {
$_.DisplayName -eq $params.WebAppName
}

if ($null -eq $webapp)
{
throw "Web application was not found. Please check WebAppName parameter!"
$message = "Web application was not found. Please check WebAppName parameter!"
Add-SPDscEvent -Message $message `
-EntryType 'Error' `
-EventID 100 `
-Source $eventSource
throw $message
}

$urlAam = Get-SPAlternateURL -Identity $params.Url `
Expand Down Expand Up @@ -147,8 +153,13 @@ function Set-TargetResource
}
else
{
throw ("Specified URL found on different WebApp/Zone: WebApp " + `
$message = ("Specified URL found on different WebApp/Zone: WebApp " + `
"$($urlAam.PublicUrl) in zone $($urlAam.Zone)")
Add-SPDscEvent -Message $message `
-EntryType 'Error' `
-EventID 100 `
-Source $eventSource
throw $message
}
}
else
Expand Down Expand Up @@ -190,8 +201,13 @@ function Set-TargetResource
}
else
{
throw ("Specified URL ($($params.Url)) found on different WebApp/Zone: " + `
$message = ("Specified URL ($($params.Url)) found on different WebApp/Zone: " + `
"WebApp $($urlAam.PublicUrl) in zone $($urlAam.Zone)")
Add-SPDscEvent -Message $message `
-EntryType 'Error' `
-EventID 100 `
-Source $eventSource
throw $message
}
}
else
Expand All @@ -205,8 +221,13 @@ function Set-TargetResource
}
else
{
throw ("Specified URL found on different WebApp/Zone: WebApp " + `
$message = ("Specified URL found on different WebApp/Zone: WebApp " + `
"$($urlAam.PublicUrl) in zone $($urlAam.Zone)")
Add-SPDscEvent -Message $message `
-EntryType 'Error' `
-EventID 100 `
-Source $eventSource
throw $message
}
}
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,18 +124,23 @@ function Set-TargetResource
Write-Verbose -Message "Setting antivirus configuration settings"

Invoke-SPDscCommand -Credential $InstallAccount `
-Arguments $PSBoundParameters `
-Arguments @($PSBoundParameters, $MyInvocation.MyCommand.Source) `
-ScriptBlock {
$params = $args[0]
$eventSource = $args[1]

try
{
$spFarm = Get-SPFarm
}
catch
{
throw "No local SharePoint farm was detected. Antivirus settings will not be applied"
return
$message = "No local SharePoint farm was detected. Antivirus settings will not be applied"
Add-SPDscEvent -Message $message `
-EntryType 'Error' `
-EventID 100 `
-Source $eventSource
throw $message
}

Write-Verbose -Message "Start update"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,14 @@ function Set-TargetResource
# InstallAccount used
if ($InstallAccount.UserName -eq $farmAccount.UserName)
{
throw ("Specified InstallAccount ($($InstallAccount.UserName)) is the Farm " + `
$message = ("Specified InstallAccount ($($InstallAccount.UserName)) is the Farm " + `
"Account. Make sure the specified InstallAccount isn't the Farm Account " + `
"and try again")
Add-SPDscEvent -Message $message `
-EntryType 'Error' `
-EventID 100 `
-Source $MyInvocation.MyCommand.Source
throw $message
}
}
else
Expand All @@ -93,16 +98,26 @@ function Set-TargetResource
$localaccount = "$($Env:USERDOMAIN)\$($Env:USERNAME)"
if ($localaccount -eq $farmAccount.UserName)
{
throw ("Specified PSDSCRunAsCredential ($localaccount) is the Farm " + `
$message = ("Specified PSDSCRunAsCredential ($localaccount) is the Farm " + `
"Account. Make sure the specified PSDSCRunAsCredential isn't the " + `
"Farm Account and try again")
Add-SPDscEvent -Message $message `
-EntryType 'Error' `
-EventID 100 `
-Source $MyInvocation.MyCommand.Source
throw $message
}
}
}
}
else
{
throw ("Unable to retrieve the Farm Account. Check if the farm exists.")
$message = "Unable to retrieve the Farm Account. Check if the farm exists."
Add-SPDscEvent -Message $message `
-EntryType 'Error' `
-EventID 100 `
-Source $MyInvocation.MyCommand.Source
throw $message
}

# Add the FarmAccount to the local Administrators group, if it's not already there
Expand All @@ -121,18 +136,25 @@ function Set-TargetResource
}

Invoke-SPDscCommand -Credential $farmAccount `
-Arguments $PSBoundParameters `
-Arguments @($PSBoundParameters, $MyInvocation.MyCommand.Source) `
-ScriptBlock {
$params = $args[0]
$eventSource = $args[1]

try
{
Update-SPAppCatalogConfiguration -Site $params.SiteUrl -Confirm:$false
}
catch [System.UnauthorizedAccessException]
{
throw ("This resource must be run as the farm account (not a setup account). " + `
$message = ("This resource must be run as the farm account (not a setup account). " + `
"Please ensure either the PsDscRunAsCredential or InstallAccount " + `
"credentials are set to the farm account and run this resource again")
Add-SPDscEvent -Message $message `
-EntryType 'Error' `
-EventID 100 `
-Source $eventSource
throw $message
}
} | Out-Null

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,20 @@ function Set-TargetResource
Write-Verbose -Message "Setting app store settings of $WebAppUrl"

Invoke-SPDscCommand -Credential $InstallAccount `
-Arguments $PSBoundParameters `
-Arguments @($PSBoundParameters,$MyInvocation.MyCommand.Source) `
-ScriptBlock {
$params = $args[0]
$eventSource = $args[1]

$wa = Get-SPWebApplication -Identity $params.WebAppUrl -ErrorAction SilentlyContinue
if ($null -eq $wa)
{
throw ("Specified web application does not exist.")
$message = "Specified web application does not exist."
Add-SPDscEvent -Message $message `
-EntryType 'Error' `
-EventID 100 `
-Source $eventSource
throw $message
}

if ($params.ContainsKey("AllowAppPurchases"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,12 @@ function Set-TargetResource
($PSBoundParameters.ContainsKey("DatabaseName") -eq $false -or
$PSBoundParameters.ContainsKey("DatabaseServer") -eq $false))
{
throw "Parameter DatabaseName and DatabaseServer are required when Ensure=Present"
$message = "Parameter DatabaseName and DatabaseServer are required when Ensure=Present"
Add-SPDscEvent -Message $message `
-EntryType 'Error' `
-EventID 100 `
-Source $MyInvocation.MyCommand.Source
throw $message
}

$result = Get-TargetResource @PSBoundParameters
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,10 @@ function Get-TargetResource
Write-Verbose -Message "Getting blob cache settings for $WebAppUrl"

$result = Invoke-SPDscCommand -Credential $InstallAccount `
-Arguments $PSBoundParameters `
-Arguments @($PSBoundParameters, $MyInvocation.MyCommand.Source) `
-ScriptBlock {
$params = $args[0]
$eventSource = $args[1]

$webappsi = Get-SPServiceInstance -Server $env:COMPUTERNAME `
-ErrorAction SilentlyContinue `
Expand Down Expand Up @@ -109,7 +110,12 @@ function Get-TargetResource
}
catch
{
throw "Error: $($_.Exception.Message)"
$message = "Error: $($_.Exception.Message)"
Add-SPDscEvent -Message $message `
-EntryType 'Error' `
-EventID 100 `
-Source $eventSource
throw $message
}

try
Expand All @@ -122,7 +128,12 @@ function Get-TargetResource
}
catch
{
throw "Error: $($_.Exception.Message)"
$message = "Error: $($_.Exception.Message)"
Add-SPDscEvent -Message $message `
-EntryType 'Error' `
-EventID 100 `
-Source $eventSource
throw $message
}

$returnval = @{
Expand Down Expand Up @@ -229,10 +240,11 @@ function Set-TargetResource
{
## Perform changes
Invoke-SPDscCommand -Credential $InstallAccount `
-Arguments @($PSBoundParameters, $changes) `
-Arguments @($PSBoundParameters, $MyInvocation.MyCommand.Source, $changes) `
-ScriptBlock {
$params = $args[0]
$changes = $args[1]
$eventSource = $args[1]
$changes = $args[2]

$webappsi = Get-SPServiceInstance -Server $env:COMPUTERNAME `
-ErrorAction SilentlyContinue `
Expand All @@ -243,14 +255,24 @@ function Set-TargetResource

if ($null -eq $webappsi)
{
throw "Server isn't running the Web Application role"
$message = "Server isn't running the Web Application role"
Add-SPDscEvent -Message $message `
-EntryType 'Error' `
-EventID 100 `
-Source $eventSource
throw $message
}

$wa = Get-SPWebApplication -Identity $params.WebAppUrl -ErrorAction SilentlyContinue

if ($null -eq $wa)
{
throw "Specified web application could not be found."
$message = "Specified web application could not be found."
Add-SPDscEvent -Message $message `
-EntryType 'Error' `
-EventID 100 `
-Source $eventSource
throw $message
}

Write-Verbose -Message "Processing changes"
Expand Down Expand Up @@ -305,11 +327,21 @@ function Set-TargetResource
}
catch [DriveNotFoundException]
{
throw "Specified drive does not exist"
$message = "Specified drive does not exist"
Add-SPDscEvent -Message $message `
-EntryType 'Error' `
-EventID 100 `
-Source $MyInvocation.MyCommand.Source
throw $message
}
catch
{
throw "Error creating Blob Cache folder: $($_.Exception.Message)"
$message = "Error creating Blob Cache folder: $($_.Exception.Message)"
Add-SPDscEvent -Message $message `
-EntryType 'Error' `
-EventID 100 `
-Source $MyInvocation.MyCommand.Source
throw $message
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,13 +166,21 @@ function Set-TargetResource

$PSBoundParameters.SetWebAppPolicy = $SetWebAppPolicy

Invoke-SPDscCommand -Credential $InstallAccount -Arguments $PSBoundParameters -ScriptBlock {
Invoke-SPDscCommand -Credential $InstallAccount `
-Arguments @($PSBoundParameters, $MyInvocation.MyCommand.Source) `
-ScriptBlock {
$params = $args[0]
$eventSource = $args[1]

$wa = Get-SPWebApplication -Identity $params.WebAppUrl -ErrorAction SilentlyContinue
if ($null -eq $wa)
{
throw [Exception] "The web applications $($params.WebAppUrl) can not be found to set cache accounts"
$message = "The web applications $($params.WebAppUrl) can not be found to set cache accounts"
Add-SPDscEvent -Message $message `
-EntryType 'Error' `
-EventID 100 `
-Source $eventSource
throw $message
}

if ($wa.UseClaimsAuthentication -eq $true)
Expand Down
Loading

0 comments on commit 1a51937

Please sign in to comment.