gpt4 book ai didi

C# Powershell 脚本失败 : "failed to get zone information"

转载 作者:太空宇宙 更新时间:2023-11-03 13:48:42 24 4
gpt4 key购买 nike

我创建了一个实用的创建 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。

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

最佳答案

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

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

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

关于C# Powershell 脚本失败 : "failed to get zone information",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14383872/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com