C#Powershell脚本失败:“无法获取区域信息”

时间:2013-01-17 16:41:57

标签: c# powershell dns windows-server-2012

我创建了一个小型网站,用于实际创建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。

任何帮助都非常感谢,提前感谢。

1 个答案:

答案 0 :(得分:1)

我发现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+"}");

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

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