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
开发者_如何学Python$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?)
If you are using PowerShell V2, you can use Set-WMIInstance
for that.
Set-WMIInstance -Namespace "root\MicrosoftDNS" -class MicrosoftDNS_AType -argument @{DnsServerName="srventr2.societe.fr"; ContainerName="societe.fr" ; OwnerName="t1.societe.fr"; RecordData="192.168.10.10" ; RecordClass=1 ; TTL=3600 } -credential (get-credential) -computername "192.168.183.138"
Check the properties of $dnsAType.psbase.Scope.Options, you can set username and password along with some other connection options:
PS > $dnsAType.psbase.Scope.Options
Locale :
Username :
Password :
SecurePassword :
Authority :
Impersonation : Impersonate
Authentication : Unchanged
EnablePrivileges : False
Context : {}
Timeout : 10675199.02:48:05.4775807
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.
Just to share knowledge:
If you want to do that on local server (server.contoso.com) and you have at least one instance of SRV record
PS C:\> $record = Get-WmiObject -namespace "root\MicrosoftDNS" -class MicrosoftDNS_SRVType
PS C:\> $record[0].CreateInstanceFromTextRepresentation("server.contoso.com", "contoso.com", "_mysvc._tcp.contoso.com IN SRV 0 100 8000 host1.contoso.com")
Still relevant today...
As for alternate credentials, in previous answers: -Credential
However, Powershell can be a bit "hokey" when it comes to returning the correct Interface.
The following command will return MicrosoftDNS_ResourceRecord (even though we asked for MicrosoftDNS_AType)
GWMI -Class MicrosoftDNS_AType -ComputerName $dns `
-Namespace root\MicrosoftDNS -Credential username | GM
As an alternative the following command will return MicrosoftDNS_AType
GWMI -Class MicrosoftDNS_AType -ComputerName $dns `
-Namespace root\MicrosoftDNS -Credential username -List | GM
To accomplish your task:
(GWMI -Class MicrosoftDNS_AType -ComputerName $dns `
-Namespace root\MicrosoftDNS -Credential username `
-List).CreateInstanceFromPropertyData($dnsFqdn,$zone, $host, 1, 3600, $ip)
精彩评论