DSCResources/xPrinterPort/xPrinterPort.ps1
Import-LocalizedData -BindingVariable localizedData -FileName xResources.psd1; function Get-TargetResource { [CmdletBinding()] [OutputType([System.Collections.Hashtable])] param ( ## Specifies the name of the printer [Parameter(Mandatory)] [System.String] $Name, ## Specifies the name of the printer driver for the printer [Parameter(Mandatory)] [ValidateSet('Local','TCP/IP')] [System.String] $Type, ## Specifies whether or not the printer is published in the network directory service [Parameter()] [System.String] $PrinterHostAddress, [Parameter()] [ValidateSet('Present','Absent')] [System.String] $Ensure = 'Present' ) process { Import-Module -Name PrintManagement -Verbose:$false; $port = Get-PrinterPort -Name $Name -ErrorAction SilentlyContinue; $targetResource = @{ PortName = $Name; Description = $port.Description; PortMonitor = $port.PortMonitor; PrinterHostAddress = $port.PrinterHostAddress; PrinterHostIP = $port.PrinterHostIP; PortNumber = $port.PortNumber; Ensure = if ($port) { 'Present' } else { 'Absent' }; } return $targetResource; } #end process } #end function Get-TargetResource function Test-TargetResource { [CmdletBinding()] [OutputType([System.Boolean])] param ( ## Specifies the name of the printer [Parameter(Mandatory)] [System.String] $Name, ## Specifies the name of the printer driver for the printer [Parameter(Mandatory)] [ValidateSet('Local','TCP/IP')] [System.String] $Type, ## Specifies whether or not the printer is published in the network directory service [Parameter()] [System.String] $PrinterHostAddress, [Parameter()] [ValidateSet('Present','Absent')] [System.String] $Ensure = 'Present' ) process { $PSBoundParameters['Ensure'] = $Ensure; $targetResource = Get-TargetResource @PSBoundParameters; $inDesiredState = $true; foreach ($propertyName in 'PortMonitor','PrinterHostAddress','PrinterHostIP','PortNumber','Ensure') { if ($PSBoundParameters.ContainsKey($propertyName)) { $propertyValue = (Get-Variable -Name $propertyName).Value; if ($propertyValue -ne $targetResource.$propertyName) { Write-Verbose ($localizedData.IncorrectPropertyState -f $propertyName, $propertyValue, $targetResource.$propertyName); $inDesiredState = $false; } } } if ($inDesiredState) { Write-Verbose ($localizedData.ResourceInDesiredState -f $Name); return $true; } else { Write-Verbose ($localizedData.ResourceNotInDesiredState -f $Name); return $false; } } #end process } #end function Test-TargetResource function Set-TargetResource { [CmdletBinding()] param ( ## Specifies the name of the printer [Parameter(Mandatory)] [System.String] $Name, ## Specifies the name of the printer driver for the printer [Parameter(Mandatory)] [ValidateSet('Local','TCP/IP')] [System.String] $Type, ## Specifies whether or not the printer is published in the network directory service [Parameter()] [System.String] $PrinterHostAddress, [Parameter()] [ValidateSet('Present','Absent')] [System.String] $Ensure = 'Present' ) process { Import-Module -Name PrintManagement -Verbose:$false; $port = Get-PrinterPort -Name $Name -ErrorAction SilentlyContinue; if ($Ensure -eq 'Present') { $portParams = @{ Name = $Name; } if ($PSBoundParameters.ContainsKey('PrinterHostAddress')) { $portParams['PrinterHostAddress'] = $PrinterHostAddress; $portParams['SNMP'] = 1; $portParams['SNMPCommunity'] = "public"; } if ($port) { Write-Verbose ($localizedData.UpdatingPort -f $Name); [ref] $null = Set-PrinterPort @portParams; } else { Write-Verbose ($localizedData.AddingPort -f $Name); [ref] $null = Add-PrinterPort @portParams; } } elseif ($Ensure -eq 'Absent') { Write-Verbose ($localizedData.RemovingPort -f $Name); [ref] $null = $port | Remove-PrinterPort -Confirm:$false; } } #end process } #end function Test-TargetResource |