locked
Creating DNS entry with Powershell and credential RRS feed

  • Question

  • Hi, 

    I've found a post (http://blogs.technet.com/b/heyscriptingguy/archive/2010/09/13/manage-dns-in-a-windows-environment-by-using-powershell.aspx), to create DNS entries, the script works very well, but as a major caveat, you cannot send credential to connect to a DNS server out of your current Domain.

    I did some research and found out that the only solution known to send credential in this case is to use Set-WMIInstance.

    So the code below works flawlessly to create an A record

    $DNSserver = "mydc1.mydomain.local"
    $Domain = "mydomain.local"
    $CN = "help7"
    $IP = "10.0.2.56"
    
    Set-WMIInstance `
    -Namespace "root\MicrosoftDNS" `
    -class MicrosoftDNS_AType `
    -argument @{ `
    DnsServerName = $DNSServer; `
    ContainerName = $Domain; `
    OwnerName = $CN;`
    DomainName = $CN;`
    RecordData = $IP;`
    RecordClass = 1;`
    TTL = 3600 `
    } `
    -computername $DNSServer `
    -credential (get-credential)

    Now my goal is to create TXT record so, I came up with this code which kind of work but return an weird error:

    $DNSserver = "mydc1.mydomain.local"
    $Domain = "mydomain.local"
    $CN = "help4"
    $TXT = "This is a test"
    
    Set-WMIInstance `
    -Namespace root\MicrosoftDNS `
    -class MicrosoftDNS_TXTType `
    -argument @{ `
    DnsServerName = $DNSServer; `
    ContainerName = $Domain; `
    DomainName = $CN; `
    OwnerName = $CN; `
    RecordData = $TXT; `
    TTL = 3600; 
    } `
    -computername $DNSServer `
    -credential (get-credential)

    2 things the $TXT variables contains spaces, and when the entry is created I got only the first word contained in $TXT, this is a problem of course, anyway with or without spaces in $TXT, I got this error after execution, but the TXT DNS entry seems to be created correctly.

    format-default : The following exception occurred while retrieving members: "Not found "
        + CategoryInfo          : NotSpecified: (:) [format-default], ExtendedTypeSystemException
        + FullyQualifiedErrorId : CatchFromBaseGetMembers,Microsoft.PowerShell.Commands.FormatDefaultCommand

    Any ideas ?

    If you can answer the question below, I will surely not need the codes above

    As you will see, the post I mentionned at the very top, doesn't use the class MicrosoftDNS_TXTType or MicrosoftDNS_AType, but use a superclass called: MicrosoftDNS_ResourceRecord, which is able to create any type of record using the Method: CreateInstanceFromTextRepresentation.

    Could one of you show me how to use MicrosoftDNS_ResourceRecord with Set-WMIInstance ?

    My code so far (Not working !)

    #Test to create a PTR record using MicrosoftDNS_ResourceRecord

    $DNSServer = "mydc1.mydomain.local" $Computer = "zzzzz-todelete" $IP = "10.0.2.100" $rzone = '2.0.10.in-addr.arpa' $addr = $IP -split "\." $domain = "mydomain.local" $text = "$($addr[3]).$rzone IN PTR $computer.$domain" Set-WMIInstance ` -Namespace "root\MicrosoftDNS" ` -class MicrosoftDNS_ResourceRecord ` -argument @{ ` DnsServerName = $DNSServer ; ` TextRepresentation = $text ` } ` -computername $DNSServer



    Cyreli


    • Edited by Cyreli Thursday, March 22, 2012 6:32 PM
    Thursday, March 22, 2012 6:30 PM

Answers

  • So are you saying the Set-WMIInstance used the CreateInstance() of MicrosoftDNS_TXTType, but cannot use CreateInstanceFromTextRepresentation() of MicrosoftDNS_ResourceRecord

    This is the very first time I use Set-WMIInstance, and haven't done much reading on it, but the  Set-WMI"Instance" refers to CreateInstance() ?


    Cyreli

    In dll (you can view using ildasm) - ((ManagementClass) instance.InputObject).CreateInstance();

    IL_007d:  callvirt   instance class [System.Management]System.Management.ManagementObject [System.Management]System.Management.ManagementClass::CreateInstance()

    To call method,you may using Invoke-WmiMethod:

    invoke-wmimethod -Namespace "root\MicrosoftDNS"  -Class MicrosoftDNS_ResourceRecord `
    	-Name  CreateInstanceFromTextRepresentation -argumentlist $ContainerName,$DNSServer,$TextRepresentation

    • Edited by Kazun Thursday, March 22, 2012 11:48 PM
    • Proposed as answer by Kazun Friday, March 23, 2012 2:16 PM
    • Marked as answer by Yan Li_ Tuesday, March 27, 2012 6:30 AM
    Thursday, March 22, 2012 11:16 PM

All replies

  • $DNSserver = "mydc1.mydomain.local"
    $Domain = "mydomain.local"
    $CN = "help4.mydomain.local"
    $TXT = """This is a test"""
    
    Set-WMIInstance -Namespace root\MicrosoftDNS -class MicrosoftDNS_TXTType -argument @{ 
    	DnsServerName = $DNSServer; 
    	ContainerName = $Domain; 
    	DomainName = $Domain; 
    	OwnerName = $CN; 
    	RecordClass=1;
    	RecordData = $TXT; 
    	TTL = 3600; 
    } 


    with MicrosoftDNS_ResourceRecord

    $DnsServerName = "mydc1.mydomain.local"
    $ContainerName = "mydomain.local"
    $TextRepresentation = "help4.mydomain.local IN TXT ""This is a text"""
    
    $wmi = [wmiclass]"\root\MicrosoftDNS:MicrosoftDNS_ResourceRecord"
    $wmi.CreateInstanceFromTextRepresentation($DnsServerName,$ContainerName,$TextRepresentation)

    • Edited by Kazun Thursday, March 22, 2012 8:36 PM
    • Proposed as answer by Kazun Friday, March 23, 2012 2:16 PM
    Thursday, March 22, 2012 8:18 PM
  • Kazun,

    Thanks for the Set-WMIInstance -Namespace root\MicrosoftDNS -class MicrosoftDNS_TXTType.

    Anyway how about using MicrosoftDNS_ResourceRecord with Set-WMIInstace such as:

    #This code doesn't work :-(

    $DNSServer = "mydc1.mydomain.local" $Computer = "zzzzz-todelete" $IP = "10.0.2.100" $rzone = '2.0.10.in-addr.arpa' $addr = $IP -split "\." $domain = "mydomain.local" $textforPTR = "$($addr[3]).$rzone IN PTR $computer.$domain" $textforA = "$computer IN A $IP"  

    Set-WMIInstance ` -Namespace "root\MicrosoftDNS" ` -class MicrosoftDNS_ResourceRecord ` -argument @{ ` DnsServerName = $DNSServer ; ` TextRepresentation = $textforPTR ` } ` -computername $DNSServer



    Cyreli

    Thursday, March 22, 2012 9:35 PM
  • You can't using Set-WMIInstace,because provider doesn't support this syntax.
    Thursday, March 22, 2012 10:28 PM
  • Where do I learn about this ? I mean about the provider not supporting this syntax ?

    I would like to understand why Set-WMIInstance -Namespace root\MicrosoftDNS -class MicrosoftDNS_TXTType is OK and why Set-WMIInstance -Namespace root\MicrosoftDNS -class MicrosoftDNS_ResourceRecord cannot work

    Thank for your help


    Cyreli

    Thursday, March 22, 2012 10:32 PM
  • Where do I learn about this ? I mean about the provider not supporting this syntax ?

    I would like to understand why Set-WMIInstance -Namespace root\MicrosoftDNS -class MicrosoftDNS_TXTType is OK and why Set-WMIInstance -Namespace root\MicrosoftDNS -class MicrosoftDNS_ResourceRecord cannot work

    Thank for your help


    Cyreli


    MicrosoftDNS_TXTType  - to creating instance you may using method  CreateInstance(),but MicrosoftDNS_ResourceRecord  doesn't support this method,instead   using CreateInstanceFromTextRepresentation
    Thursday, March 22, 2012 10:55 PM
  • So are you saying the Set-WMIInstance used the CreateInstance() of MicrosoftDNS_TXTType, but cannot use CreateInstanceFromTextRepresentation() of MicrosoftDNS_ResourceRecord

    This is the very first time I use Set-WMIInstance, and haven't done much reading on it, but the  Set-WMI"Instance" refers to CreateInstance() ?


    Cyreli

    Thursday, March 22, 2012 11:00 PM
  • So are you saying the Set-WMIInstance used the CreateInstance() of MicrosoftDNS_TXTType, but cannot use CreateInstanceFromTextRepresentation() of MicrosoftDNS_ResourceRecord

    This is the very first time I use Set-WMIInstance, and haven't done much reading on it, but the  Set-WMI"Instance" refers to CreateInstance() ?


    Cyreli

    In dll (you can view using ildasm) - ((ManagementClass) instance.InputObject).CreateInstance();

    IL_007d:  callvirt   instance class [System.Management]System.Management.ManagementObject [System.Management]System.Management.ManagementClass::CreateInstance()

    To call method,you may using Invoke-WmiMethod:

    invoke-wmimethod -Namespace "root\MicrosoftDNS"  -Class MicrosoftDNS_ResourceRecord `
    	-Name  CreateInstanceFromTextRepresentation -argumentlist $ContainerName,$DNSServer,$TextRepresentation

    • Edited by Kazun Thursday, March 22, 2012 11:48 PM
    • Proposed as answer by Kazun Friday, March 23, 2012 2:16 PM
    • Marked as answer by Yan Li_ Tuesday, March 27, 2012 6:30 AM
    Thursday, March 22, 2012 11:16 PM