从 powershell 使用 WMI 制作 DNS 记录条目
免责声明:我对 powershell 非常陌生,对 WMI 几乎一无所知。
我正在尝试通过 powershell 在远程服务器上创建 dns 条目。我用谷歌搜索了一下,发现 WMI 是唯一的方法。
因此,以下代码片段对我有用。
$dnsAType = [wmiclass]"\\$dnsServer\root\MicrosoftDNS:MicrosoftDNS_AType"
$dnsAType.CreateInstanceFromPropertyData($dnsServer, $dnsZone, $domainName, $class, $ttl, $ipaddress)
问题是我必须以不同的用户身份输入这些条目。我登录的用户没有足够的权限。所以我必须传递凭据。 Get-WmiObject 似乎是传递不同凭据的唯一方法。但我无法让代码适用于 Get-WmiObject 。
以下代码片段可以让我获得 wmi_objects。
$wmi_object = Get-WmiObject -Class MicrosoftDNS_AType -Namespace "root\MicrosoftDNS" -computerName "192.168.1.5" -Credential $creds
但这似乎是一个数组,并且元素似乎没有我期望的 CreateInstanceFromPropertyData 方法。我对如何解决这个问题有点困惑。任何帮助将不胜感激。
谷歌搜索仅给我使用 wmi 文字的结果(我猜这就是它们是什么?)
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?)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果您使用的是 PowerShell V2,则可以使用
Set-WMIInstance
来实现。If you are using PowerShell V2, you can use
Set-WMIInstance
for that.检查 $dnsAType.psbase.Scope.Options 的属性,您可以设置用户名和密码以及其他一些连接选项:
Check the properties of $dnsAType.psbase.Scope.Options, you can set username and password along with some other connection options:
我们通过以下方式使其工作:
我们在本地和远程计算机上启用了 PowerShell 远程处理,并通过 Invoke-Command 使其工作。
只有一件事您必须以提升的权限运行。希望这对其他人有帮助。
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
There is only one thing that you have to be running with elevated permissions. Hope this helps others.
只是为了分享知识:
如果您想在本地服务器 (server.contoso.com) 上执行此操作并且您至少有一个 SRV 记录实例
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
今天仍然相关...
至于备用凭据,在之前的答案中: -Credential
但是,在返回正确的接口时,Powershell 可能有点“做作”。
以下命令将返回 MicrosoftDNS_ResourceRecord(即使我们要求 MicrosoftDNS_AType)
作为替代方案,以下命令将返回 MicrosoftDNS_AType
来完成您的任务:
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)
As an alternative the following command will return MicrosoftDNS_AType
To accomplish your task: