C# Powershell 脚本失败:“无法获取区域信息" [英] C# Powershell script failing: "failed to get zone information"
问题描述
我创建了一个实用的创建 DNS NS 记录的小型站点.当我在 PS(在我的 site.com 服务器上)执行我的代码时,它运行良好,但是当我从网站(托管在我的 site.com 服务器上)运行它时,它失败并显示错误:创建 IP 1.1.1.1 时出错.错误:无法在服务器 myServer 上获取 site.com 的区域信息"
I've created a small site which pragmatically creates DNS NS records. When I execute my code in PS (on my site.com server) it works perfectly, but when I run it from the website (hosted on my site.com server) it fails with the error: "Error creating IP 1.1.1.1. Error: Failed to get the zone information for site.com on server myServer"
我的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();;
}
我已确认该站点将运行诸如
I've confirmed that the site will run PS commands such as
Get-WmiObject Win32_BIOS -Namespace 'root\\CIMV2' -computername .
没有问题.
Powershell 版本为 3.0,服务器为运行 IIS 6.2 的 Windows Server 2012.
Powershell version is 3.0, server is Windows Server 2012 running IIS 6.2.
非常感谢任何帮助,提前致谢.
Any help is greately appreciated, thanks in advance.
推荐答案
我发现 Power Shell 需要增强权限才能更改 DNS(以及关联的文件夹和文件).经过一番研究,我最终使用了此代码:
I found that Power Shell needed enhanced permissions to make changes to DNS (and associated folders and files). After some research I ended up using this code:
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+"}");
我认识到硬编码我的管理员密码的形式很糟糕,但时间已经不多了,我找不到更好的解决方案.
I recognize that hard coding my admin password is bad form, but time was running out and I was unable to find a better solution.
如果其他人有更好的解决方案,我愿意接受.
If anyone else has a better solution, I am open to it.
这篇关于C# Powershell 脚本失败:“无法获取区域信息"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!