Search code examples
powershellwmi

Make DNS Record entries using WMI from powershell


Disclaimer: I am very new to powershell and almost ignorant of WMI.

I am trying to make dns entries on a remote server through powershell. I have googled and found WMI to be the only way.

So the following code snippet works for me

$dnsAType = [wmiclass]"\\$dnsServer\root\MicrosoftDNS:MicrosoftDNS_AType"
    $dnsAType.CreateInstanceFromPropertyData($dnsServer, $dnsZone, $domainName, $class, $ttl, $ipaddress)

The problem is that I have to make these entries as a different user. The user that I am logged in as does not have enough privileges. So I have to pass in credentials. Get-WmiObject seems to be the only way of passing in different credentials. But I am not able to get the code working for Get-WmiObject .

The following snippet works gets me the wmi_objects.

$wmi_object = Get-WmiObject -Class MicrosoftDNS_AType -Namespace "root\MicrosoftDNS" -computerName "192.168.1.5" -Credential $creds

But that seems to be an array and the elements do not seem to have the CreateInstanceFromPropertyData method that I was expecting. I am kind of confused as to how to go about this. Any help would be appreciated.

Googling for this only gives me results for using wmi literals(I guess that is what they are?)


Solution

  • We got this to work the following way:

    We enabled PowerShell Remoting on both local and remote machines and got it working through Invoke-Command

        PS>Enable-PSRemoting
        PS>Invoke-Command {
        $dnsAType = [wmiclass]"root\MicrosoftDNS:MicrosoftDNS_AType"
            $dnsAType.CreateInstanceFromPropertyData($dnsServer, $dnsZone, $domainName, $class, $ttl, $ipaddress)
        } -Credentials $cred -ComputerName $remotemachineName
    

    There is only one thing that you have to be running with elevated permissions. Hope this helps others.