سؤال

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"

My PS script is:

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

My C# code is:

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 . 

without an issue.

Powershell version is 3.0, server is Windows Server 2012 running IIS 6.2.

Any help is greately appreciated, thanks in advance.

هل كانت مفيدة؟

المحلول

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.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top