2007年9月18日星期二

[Tips]Powershell分享(1) - 操作DNS

author: long#ph4nt0m.org
date: 2007-09-18
http://www.ph4nt0m.org

Text Mode

Powershell出来了一段时间了,学习了一下。和大家分享一下学习经验:

Powershell中,可以通过WMI来对DNS Server进行操作,具体方法如下:

(1) 创建一个WMIClass的实例,当然也可以用\\Servername\Root\MicrosoftDNS:MicrosoftDNS_ResourceRecord创建远程服务器的WMIClass实例:

PS> $DNS = [WMIClass]"ROOT\MicrosoftDNS:MicrosoftDNS_ResourceRecord"

(2) 调用CreateInstanceFromTextRepresentation方法:
PS> $DNS.CreateInstanceFromTextRepresentation("dnssrv.foo.com","foo.com","www.foo.com IN A 1.1.1.1")

CreateInstanceFromTextRepresentation方法的参数如下:
PS> $DNS | Get-Member -MemberType Method | Where {$_.Name -eq "CreateInstanceFromTextRepresentation"} | Select Definition | FL *

Definition : System.Management.ManagementBaseObject CreateInstanceFromTextRepresentation(System.String DnsServerName, System.String ContainerName, System.String TextRepresentation)

(3) 当然你也可以使用foreach来批量创建记录:
PS> cat dnsrecord.txt | % {$DNS.CreateInstanceFromTextRepresentation($DNSServe, $Domainname, $_)}

发散思维:

IIS,SQL Server,Exchange也都有WMI Provider。通过Powershell,可以降低脚本复杂度,更方便的调用WMI Provider提供的方法来操作企业应用程序。

没有评论: