我可以在PowerShell脚本中使用Resource.Schema.Mof文件中的数据吗?

时间:2015-07-07 14:09:21

标签: powershell dry dsc

我正在编写用于管理Windows DNS配置项(BootMethod,EventLogLevel等)的DSC资源。我的Get-TargetResource函数需要遍历资源将能够管理的所有配置项。目前,我在Resource.psm1文件中有一个描述所有这些的哈希表,但我希望能够使用Resource.Schema.Mof文件中已存在的描述,首先需要构建资源。这是可能的,或者由于某种原因我可能不明白?

Schema.mof:

[ClassVersion("1.0.0"), FriendlyName("GO_DnsServerConfig")]
class GO_DnsServerConfig : OMI_BaseResource
{
[Key] string Name;
[Write,ValueMap{"Present","Absent"},Values{"Present","Absent"}]
string Ensure;
[Write] UInt32 AddressAnswerLimit;
[Write,ValueMap{"0","1"},Values{"0","1"}] UInt32 BindSecondaries;
[Write,ValueMap{"0","1","2","3"},Values{"0","1","2","3"}]
UInt32 BootMethod;
[Write,ValueMap{"0","1"},Values{"0","1"}] UInt32 DefaultAgingState;
[Write] UInt32 DefaultNoRefreshInterval;
[Write] UInt32 DefaultRefreshInterval;
[Write,ValueMap{"0","1"},Values{"0","1"}] UInt32 DisableAutoReverseZones;
[Write,ValueMap{"0","1"},Values{"0","1"}]
UInt32 DisableNsRecordsAutoCreation;
[Write] UInt32 DsPollingInterval;
[Write] UInt32 DsTombstoneInterval;
[Write] UInt32 EDnsCacheTimeout;
/* Does not appear to be fully implemented in dnscmd
[Write,ValueMap{"0","1"},Values{"0","1"}] UInt32 EnableDnsProbes;
*/
[Write,ValueMap{"0","1"},Values{"0","1"}] UInt32 EnableDnsSec;
[Write,ValueMap{"0","1"},Values{"0","1"}]
UInt32 EnableGlobalNamesSupport;
[Write,ValueMap{"0","1"},Values{"0","1"}]
UInt32 EnableGlobalQueryBlocklist;
[Write,ValueMap{"0","1","2","4"},Values{"0","1","2","4"}]
UInt32 EventLogLevel;
[Write,ValueMap{"0","1"},Values{"0","1"}] UInt32 ForwardDelegations;
[Write] UInt32 ForwardingTimeout;
[Write,ValueMap{"0","1"},Values{"0","1"}] UInt32 GlobalNamesQueryOrder;
/*
[Read,ValueMap{"isatap","wpad"},Values{"isatap","wpad"}]
String GlobalQueryBlocklist;
*/
[Write,ValueMap{"0","1"},Values{"0","1"}] UInt32 IsSlave;
[Write,ValueMap{"0","1"},Values{"0","1"}] UInt32 LocalNetPriority;
[Write] UInt32 LogFileMaxSize;
[Write] string LogFilePath;
[Write] string LogIpFilterList;
[Write] uint32 LogLevel;
[Write] uint64 MaxCacheSize;
[Write]
uint32 MaxCacheTtl;
[Write,ValueMap{"0","1","2","3"},Values{"0","1","2","3"}]
uint16 NameCheckFlag;
[Write,ValueMap{"0","1"},Values{"0","1"}] UInt32 NoRecursion;
[Write] uint32 RecursionRetry;
[Write] uint32 RecursionTimeout;
[Write,ValueMap{"0","1"},Values{"0","1"}] UInt32 RoundRobin;
[Write] uint32 RpcProtocol;
[Write] uint32 ScavengingInterval;
[Write,ValueMap{"0","1"},Values{"0","1"}] UInt32 SecureResponses;
[Write] uint32 SendPort;
[Write] string ServerLevelPluginDll;
[Write,ValueMap{"0","1"},Values{"0","1"}] UInt32 StrictFileParsing;
[Write] UInt32 UpdateOptions;
[Write,ValueMap{"0","1"},Values{"0","1"}] UInt32 WriteAuthorityNs;
[Write] uint32 XfrConnectTimeout;
};

Get-TargetResource函数:

function Get-TargetResource
{
[CmdletBinding()]
[OutputType([Hashtable])]
param
(
    [Parameter(Mandatory = $true)]
    [string]$Name
)

Set-StrictMode -Version 2.0
$ErrorActionPreference = 'Stop'

$ConfigItems =
@{
    Name                         = $Name;
    Ensure                       = $null;
    AddressAnswerLimit           = $null;
    BindSecondaries              = $null;
    BootMethod                   = $null;
    DefaultAgingState            = $null;
    DefaultNoRefreshInterval     = $null;
    DefaultRefreshInterval       = $null;
    DisableAutoReverseZones      = $null;
    DisableNsRecordsAutoCreation = $null;
    DsPollingInterval            = $null;
    DsTombstoneInterval          = $null;
    EDnsCacheTimeout             = $null;
    #EnableDnsProbes              = $null; #Disabled; see schema.
    EnableDnsSec                 = $null;
    EnableGlobalNamesSupport     = $null;
    EnableGlobalQueryBlocklist   = $null;
    EventLogLevel                = $null;
    ForwardDelegations           = $null;
    ForwardingTimeout            = $null;
    GlobalNamesQueryOrder        = $null;
    GlobalQueryBlocklist         = $null;
    IsSlave                      = $null;
    LocalNetPriority             = $null;
    LogFileMaxSize               = $null;
    LogFilePath                  = $null;
    LogIpFilterList              = $null;
    LogLevel                     = $null;
    MaxCacheSize                 = $null;
    MaxCacheTtl                  = $null;
    NameCheckFlag                = $null;
    NoRecursion                  = $null;
    RecursionRetry               = $null;
    RecursionTimeout             = $null;
    RoundRobin                   = $null;
    RpcProtocol                  = $null;
    ScavengingInterval           = $null;
    SecureResponses              = $null;
    SendPort                     = $null;
    ServerLevelPluginDll         = $null;
    StrictFileParsing            = $null;
    UpdateOptions                = $null;
    WriteAuthorityNs             = $null;
    XfrConnectTimeout            = $null;
}

Write-Verbose "Check to see if there is a DNS server present on the box."
$ConfigItems.Ensure = Get-EnsureValue
If ($ConfigItems.Ensure -eq 'Present')
{
    Write-Verbose "A DNS Server was found on this machine."
    $ConfigItems.Ensure = 'Present'

    Write-Verbose "Create a new hashtable to store the result."
    $result = @{}

    Write-Verbose "Add manually enumerated values to the resultset."
    $result.Add('Name', $ConfigItems.Name)
    $result.Add('Ensure', $ConfigItems.Ensure)

    Write-Verbose "Get the current value for any item that is null."
    $ConfigItems.GetEnumerator() `
    | Where-Object {$_.Value -eq $null} `
    | ForEach-Object `
    {
        $itemName = $_.Name
        Write-Verbose "Getting: '$itemName'"
        $itemValue =
            Get-DnsConfigurationItem -Item $itemName
        Write-Debug "Value: '$itemValue'"

        if ($itemValue -ne $null)
        {
            Write-Verbose "Adding item to resultset."
            $result.Add($itemName, $itemValue)
        }
    }
}
Else
{
    Write-Verbose "No DNS server was found on this machine."
    Write-Verbose "Returning with Ensure = '$($ConfigItems.Ensure)'."
    $result = @{ Name = $ConfigItems.Name; Ensure = $ConfigItems.Ensure }
}

return $result
}

0 个答案:

没有答案