Skip to content

Instantly share code, notes, and snippets.

@bgelens
Created April 30, 2017 10:18
Show Gist options
  • Save bgelens/2300f2a72f5fbe2dd576ea70658bea79 to your computer and use it in GitHub Desktop.
Save bgelens/2300f2a72f5fbe2dd576ea70658bea79 to your computer and use it in GitHub Desktop.
#region localizeddata
if (Test-Path "${PSScriptRoot}\${PSUICulture}")
{
Import-LocalizedData `
-BindingVariable LocalizedData `
-Filename MSFT_LMHost.psd1 `
-BaseDirectory "${PSScriptRoot}\${PSUICulture}"
}
else
{
#fallback to en-US
Import-LocalizedData `
-BindingVariable LocalizedData `
-Filename MSFT_LMHost.psd1 `
-BaseDirectory "${PSScriptRoot}\en-US"
}
#endregion
<#
.SYNOPSIS
Returns the current LMHost Lookup setting on the node.
.PARAMETER IsSingleInstance
Specifies the resource is a single instance, the value must be 'Yes'.
.PARAMETER Enable
Specifies if LMHost lookup should be enabled or not.
#>
function Get-TargetResource
{
[CmdletBinding()]
[OutputType([System.Collections.Hashtable])]
param
(
[parameter(Mandatory = $true)]
[ValidateSet("Yes")]
[System.String]
$IsSingleInstance,
[parameter(Mandatory = $true)]
[System.Boolean]
$Enable
)
Write-Verbose -Message ( @(
"$($MyInvocation.MyCommand): "
$($LocalizedData.GettingLMHostMessage)
) -join '' )
return @{
IsSingleInstance = 'Yes'
Enable = Test-LMHostEnabled
}
}
<#
.SYNOPSIS
Sets the desired LMHost Lookup setting on the node.
.PARAMETER IsSingleInstance
Specifies the resource is a single instance, the value must be 'Yes'.
.PARAMETER Enable
Specifies if LMHost lookup should be enabled or not.
#>
function Set-TargetResource
{
[CmdletBinding()]
param
(
[parameter(Mandatory = $true)]
[ValidateSet("Yes")]
[System.String]
$IsSingleInstance,
[parameter(Mandatory = $true)]
[System.Boolean]
$Enable
)
Write-Verbose -Message ( @(
"$($MyInvocation.MyCommand): "
$($LocalizedData.SettingLMHostMessage -f $Enable)
) -join '' )
$Result = Invoke-CimMethod -ClassName Win32_NetworkAdapterConfiguration -MethodName EnableWINS -Arguments @{
WINSEnableLMHostsLookup = $Enable
}
if ($Result.ReturnValue -ne '0')
{
New-TerminatingError `
-errorId 'InvalidLMHostUpdateError' `
-errorMessage ($LocalizedData.InvalidLMHostUpdateError -f $Result.ReturnValue) `
-errorCategory InvalidResult
}
}
<#
.SYNOPSIS
Tests if the current LMHost lookup setting on the node needs to be changed.
.PARAMETER IsSingleInstance
Specifies the resource is a single instance, the value must be 'Yes'.
.PARAMETER Enable
Specifies if LMHost lookup should be enabled or not.
.OUTPUTS
Returns false if the LMHost lookup setting needs to be changed or true if it is correct.
#>
function Test-TargetResource
{
[CmdletBinding()]
[OutputType([System.Boolean])]
param
(
[parameter(Mandatory = $true)]
[ValidateSet("Yes")]
[System.String]
$IsSingleInstance,
[parameter(Mandatory = $true)]
[System.Boolean]
$Enable
)
Write-Verbose -Message ( @(
"$($MyInvocation.MyCommand): "
$($LocalizedData.TestingLMHostMessage)
) -join '' )
if ((Test-LMHostEnabled) -eq $Enable)
{
return $true
}
else
{
return $false
}
}
# Helper Functions
<#
.SYNOPSIS
Throw a custome exception.
.PARAMETER ErrorId
The identifier representing the exception being thrown.
.PARAMETER ErrorMessage
The error message to be used for this exception.
.PARAMETER ErrorCategory
The exception error category.
#>
function New-TerminatingError
{
[CmdletBinding()]
param
(
[Parameter(Mandatory)]
[String] $ErrorId,
[Parameter(Mandatory)]
[String] $ErrorMessage,
[Parameter(Mandatory)]
[System.Management.Automation.ErrorCategory] $ErrorCategory
)
$exception = New-Object `
-TypeName System.InvalidOperationException `
-ArgumentList $errorMessage
$errorRecord = New-Object `
-TypeName System.Management.Automation.ErrorRecord `
-ArgumentList $exception, $errorId, $errorCategory, $null
$PSCmdlet.ThrowTerminatingError($errorRecord)
}
<#
.SYNOPSIS
Checks if LMHost lookup is currently enabled
#>
function Test-LMHostEnabled
{
[CmdletBinding()]
param ()
$CimInstance = Get-CimInstance -ClassName Win32_NetworkAdapterConfiguration -Filter 'IPEnabled = TRUE'
Write-Verbose -Message "LMHost lookup enabled: $($CimInstance[0].WINSEnableLMHostsLookup)"
$CimInstance[0].WINSEnableLMHostsLookup
}
Export-ModuleMember -Function *-TargetResource
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment