3

我创建了一个实用地创建 DNS NS 记录的小站点。当我在 PS(在我的 site.com 服务器上)中执行我的代码时,它运行良好,但是当我从网站(托管在我的 site.com 服务器上)运行它时,它失败并出现错误:“创建 IP 1.1.1.1 时出错。错误:无法在服务器 myServer 上获取 site.com 的区域信息”

我的 PS 脚本是:

Add-DnsServerZoneDelegation site.com –childzonename lab00012 –IPAddress 1.1.1.1 –nameserver 1.1.1.1

我的 C# 代码是:

var labString = "lab" + tenantString;
var scriptText = "Add-DnsServerZoneDelegation site.com –childzonename " + labString
            + " –IPAddress " + ipAddress + " –nameserver " + ipAddress;
using (var runspace = RunspaceFactory.CreateRunspace())
{
    runspace.Open();
    Pipeline pipeline = runspace.CreatePipeline();
    pipeline.Commands.AddScript(scriptText);
    var results = pipeline.Invoke();;
}

我已经确认该站点将运行 PS 命令,例如

Get-WmiObject Win32_BIOS -Namespace 'root\\CIMV2' -computername . 

没有问题。

Powershell 版本是 3.0,服务器是运行 IIS 6.2 的 Windows Server 2012。

非常感谢任何帮助,在此先感谢。

4

1 回答 1

2

我发现 Power Shell 需要增强权限才能更改 DNS(以及相关的文件夹和文件)。经过一番研究,我最终使用了这段代码:

pipeline.Commands.AddScript("$pw= convertto-securestring 'adminPassword' -asplaintext –force");
pipeline.Commands.AddScript("$user = New-Object Management.Automation.PSCredential('machine\\Administrator', $pw)");
pipeline.Commands.AddScript("Invoke-Command -ComputerName . -Credential $user -ScriptBlock {"+scriptText+"}");

我认识到硬编码我的管理员密码是错误的形式,但是时间不多了,我无法找到更好的解决方案。

如果其他人有更好的解决方案,我愿意接受。

于 2013-02-07T18:40:22.983 回答