从 powershell 使用 WMI 制作 DNS 记录条目

发布于 2024-12-05 05:06:14 字数 776 浏览 1 评论 0原文

免责声明:我对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

无名指的心愿 2024-12-12 05:06:15

如果您使用的是 PowerShell V2,则可以使用 Set-WMIInstance 来实现。

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"

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"
情独悲 2024-12-12 05:06:15

检查 $dnsAType.psbase.Scope.Options 的属性,您可以设置用户名和密码以及其他一些连接选项:

PS > $dnsAType.psbase.Scope.Options    

Locale           :
Username         :
Password         :
SecurePassword   :
Authority        :
Impersonation    : Impersonate
Authentication   : Unchanged
EnablePrivileges : False
Context          : {}
Timeout          : 10675199.02:48:05.4775807

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
姜生凉生 2024-12-12 05:06:15

我们通过以下方式使其工作:

我们在本地和远程计算机上启用了 PowerShell 远程处理,并通过 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

只有一件事您必须以提升的权限运行。希望这对其他人有帮助。

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.

白馒头 2024-12-12 05:06:15

只是为了分享知识:

如果您想在本地服务器 (server.contoso.com) 上执行此操作并且您至少有一个 SRV 记录实例

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")

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")
等数载,海棠开 2024-12-12 05:06:15

今天仍然相关...

至于备用凭据,在之前的答案中: -Credential

但是,在返回正确的接口时,Powershell 可能有点“做作”。

以下命令将返回 MicrosoftDNS_ResourceRecord(即使我们要求 MicrosoftDNS_AType)

GWMI -Class MicrosoftDNS_AType -ComputerName $dns `
     -Namespace root\MicrosoftDNS -Credential username | GM

作为替代方案,以下命令将返回 MicrosoftDNS_AType

GWMI -Class MicrosoftDNS_AType -ComputerName $dns `
     -Namespace root\MicrosoftDNS -Credential username -List | GM

来完成您的任务:

(GWMI -Class MicrosoftDNS_AType -ComputerName $dns `
      -Namespace root\MicrosoftDNS -Credential username `
      -List).CreateInstanceFromPropertyData($dnsFqdn,$zone, $host, 1, 3600, $ip)

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文