Skip to content

Set-PSWUSettings: Using -SendReport and -SmtpServer paramaters throws error: Set-PSWUSettings : Missing SmtpServer; "Use -PSWUSettings or declare PSWUSettings.xml in ModuleBase path." #55

@jww-csisd

Description

@jww-csisd

Any time I try to use Set-PSWUSettings -SendReport -SmtpServer 'smtp-relay.gmail.com', I get the below error:

Set-PSWUSettings : Missing SmtpServer; Use -PSWUSettings or declare PSWUSettings.xml in ModuleBase path.
At X:\Scripts\PowerShell\Test-PSWUSettings.ps1:17 char:1
+ Set-PSWUSettings -SendReport -SmtpServer 'smtp-relay.gmail.com' -Smtp ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : CloseError: (:) [Set-PSWUSettings], Exception
    + FullyQualifiedErrorId : SmtpServer,PSWindowsUpdate.SetPSWUSettings

The command appears to generate the PSWUSettings.xml file with the correct values, I just can't figure out why the error happens.

So here's the script I made to test with:

     PS>Get-Content .\Test-PSWUSettings.ps1 ;  .\Test-PSWUSettings.ps1 -Verbose

    [CmdletBinding()]
    param ()
    $Imported = Import-Module -Name PSWindowsUpdate -PassThru 4>$null
    If ($VerbosePreference -eq 'Continue') { Write-Host ''; Write-Host "`n$($Imported | Out-Host)" }
    $PSWUSettingsPath = (Get-Module PSWindowsUpdate).Path.Replace('PSWindowsUpdate.dll', 'PSWUSettings.xml')
    $LocalParams = @{
        SmtpServer    = 'smtp-relay.gmail.com'    
        SendReport    = $true
        SmtpPort      = 587
        SmtpEnableSsl = $true
        SmtpTo        = 'alerts@contoso.com'
        SmtpFrom      = 'Update@contoso.com'
        SmtpSubject   = "Windows Update Report - $env:COMPUTERNAME"
        Style         = 'List'
    }

    If (Test-Path -Path $PSWUSettingsPath) 
    {
        $PSWUSettings = Import-Clixml -Path $PSWUSettingsPath
        $SettingsMatch = Compare-Object -ReferenceObject ($LocalParams.Values | Sort-Object) -DifferenceObject ($PSWUSettings.Values | Sort-Object) -PassThru
        If ( $SettingsMatch -eq $true)
        {
            Write-Verbose "PSWUSettings previously set correctly"
        }
        Else
        {
            Write-Verbose "Existing PSWUSettings: $($PSWUSettings | Out-Host)"
            Write-Verbose "$env:COMPUTERNAME - PSWUSettings are incorrect. Setting them to correct values now"
            Set-PSWUSettings @LocalParams
        }
    }
    Else
    {
        Write-Verbose "Initializing PSWUSettings: $($PSWUSettings | Out-Host)"
        Set-PSWUSettings @LocalParams
    }

    $PSWUSettings = Import-Clixml -Path $PSWUSettingsPath
    $SettingsMatch = Compare-Object -ReferenceObject ($LocalParams.Values | Sort-Object) -DifferenceObject ($PSWUSettings.Values | Sort-Object) -PassThru
    If ( $SettingsMatch -eq $true)
    {
        Write-Host "$env:COMPUTERNAME - PSWUSettings have been set correctly"
    }
    Else
    {
        Write-Host "$env:COMPUTERNAME - Unable to set PSWUSettings correctly"
    }

And here's the output it gives me:

ModuleType Version    Name                                ExportedCommands
---------- -------    ----                                ----------------
Binary     2.2.1.5    PSWindowsUpdate                     {Add-WUServiceManager, Enable-WURemoting, Get-WindowsUpdate, Get-WUApiVersion...}

VERBOSE: Initializing PSWUSettings:
Set-PSWUSettings : Missing SmtpServer; Use -PSWUSettings or declare PSWUSettings.xml in ModuleBase path.
At X:\Scripts\PowerShell\Test-PSWUSettings.ps1:39 char:5
+     Set-PSWUSettings @LocalParams
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : CloseError: (:) [Set-PSWUSettings], Exception
    + FullyQualifiedErrorId : SmtpServer,PSWindowsUpdate.SetPSWUSettings

VERBOSE: Save PSWUSetting to file: C:\Program Files\WindowsPowerShell\Modules\PSWindowsUpdate\2.2.1.5\PSWUSettings.xml

ComputerName : MyComputer
SmtpServer   : smtp-relay.gmail.com
Port         : 587
EnableSsl    : True
To           : alerts@contoso.com
From         : Update@contoso.com
Subject      : Windows Update Report - 726VMWA09951
Style        : List

MyComputer - PSWUSettings have been set correctly

PS>

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions