dsccommunity / UpdateServicesDsc

This module contains community maintained DSC resources for deployment and configuration of Windows Server Update Services.
MIT License
31 stars 27 forks source link

UpdateServicesServer - Languages #36

Closed jcoconnor closed 5 years ago

jcoconnor commented 5 years ago

The Languages field is unable to handle multiple languages when configuring WSUS. The code sample at the bottom of this issue causes the following error to be returned: failed to execute Set-TargetResource functionality with error message: Test-TargetResouce returned false after calling set

Have done some investigation of this with @RandomNoun7 and will submit a PR shortly to address it.

Multiple languages (e.g. "en","ja","fr") are being set correctly, but the subsequent test fails to validate the setting because of a data mismatch in the comparison which causes the language test to fail if more than one language is set.

The code sample that demonstrates this fault is below:

$script:ErrorActionPreference = 'Stop'
$script:WarningPreference     = 'SilentlyContinue'

function new-pscredential{
  [CmdletBinding()]
  param (
    [parameter(Mandatory=$true, ValueFromPipelineByPropertyName=$true)]
    [string]$user,
    [parameter(Mandatory=$true, ValueFromPipelineByPropertyName=$true)]
    [string]$password
  )

  $secpasswd   = ConvertTo-SecureString $password -AsPlainText -Force
  $credentials = New-Object System.Management.Automation.PSCredential ($user, $secpasswd)
  return $credentials
}

$response = @{
  indesiredstate = $false
  rebootrequired = $false
  errormessage   = ''
}

$invokeParams = @{
Name       = 'UpdateServicesServer'
ModuleName = 'UpdateServicesDsc'
Method     = 'set'
Property   = @{
'ensure' = 'present';
'contentdir' = 'C:\WSUS';
'languages' = @('en', 'fr', 'ja');
'products' = @('Windows 10 LTSB', 'Windows 10', 'Windows 7', 'Windows 8.1', 'Windows 8', 'Windows Server 2008 R2', 'Windows Server 2008', 'Windows Server 2012 R2', 'Windows Server 2012', 'Windows Server 2016', 'Windows Server 2019');
'classifications' = @('E6CF1350-C01B-414D-A61F-263D14D133B4', 'E0789628-CE08-4437-BE74-2495B842F43B', '0FA1201D-4330-4FA8-8AE9-B877473B6441', '68C5B0A3-D1A6-4553-AE49-01D3A7827828', '28BC880E-0592-4CBF-8F95-C79B17911D5F');
'synchronizeautomatically' = $true;
'synchronizeautomaticallytimeofday' = '15:30:00'
}
}

try{
  $result = Invoke-DscResource @invokeParams
}catch{
  $response.errormessage = $_.Exception.Message
  return ($response | ConvertTo-Json -Compress)
}

# keep the switch for when Test passes back changed properties
switch($invokeParams.Method){
  'Test'{
    $response.indesiredstate = $result.InDesiredState
    return ($response | ConvertTo-Json -Compress)
  }
  'Set'{
    $response.indesiredstate = $true
    $response.rebootrequired = $result.RebootRequired
    return ($response | ConvertTo-Json -Compress)
  }
}
Droncz commented 5 years ago

Hello, I´ve actually run into the same problem and I believe the problem is in this line as it tries to compare the number of languages set using a ".count" object property. But I think the object in question ($Wsus.Languages) is not an array and hence is not countable, so it always returns 1. The number of languages set must be found out some other way...

jcoconnor commented 5 years ago

@Droncz its not an array because the Out-String in this line converts it into a string. If you look at the PR I have submitted, correcting this resolves the problem

gaelcolas commented 5 years ago

This PR is merged. Please let me know if further issues.