3

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.

2
  • What is your code executing as under IIS? What permissions there are different than your Powershell console? Commented Jan 17, 2013 at 17:43
  • I don't know, I assume whatever the defaults are. This is only my second time using IIS so all I did was create a website and push up the files. Sorry, IIS and powershell are pretty new to me. Commented Jan 17, 2013 at 18:41

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.