I can run these commands with no problem directly in PowerShell. But trying to get C# to run them, I receive errors.
md d:\ftproot\vdir\test20140317A; New-WebVirtualDirectory -Site "[site]/[a virt directory]" -Name test20140317A -physicalPath d:\ftproot\vdir\test20140317A;
When running in c# through two different means, I receive these errors
Exception:Caught: "A parameter cannot be found that matches parameter name 'physicalPath'." (System.Management.Automation.CmdletInvocationException) A System.Management.Automation.CmdletInvocationException was caught: "A parameter cannot be found that matches parameter name 'physicalPath'." Though in Visual Studio , there's something called InteliTrace and it's showing a whole slew of exceptions starting with Exception:Thrown: "Unable to load DLL 'wldp.dll': The specified module could not be found. (Exception from HRESULT: 0x8007007E)" (System.DllNotFoundException) A System.DllNotFoundException was thrown: "Unable to load DLL 'wldp.dll': The specified module could not be found. (Exception from HRESULT: 0x8007007E)" Exception:Thrown: "Requested registry access is not allowed." (System.Security.SecurityException) A System.Security.SecurityException was thrown: "Requested registry access is not allowed." Exception:Thrown: "Cannot find path 'C:\Users\jpacella\Documents\WindowsPowerShell\Modules' because it does not exist." (System.Management.Automation.ItemNotFoundException) A System.Management.Automation.ItemNotFoundException was thrown: "Cannot find path 'C:\Users\jpacella\Documents\WindowsPowerShell\Modules' because it does not exist." Exception:Thrown: "Could not load file or assembly 'Microsoft.IIS.PowerShell.Framework' or one of its dependencies. The system cannot find the file specified." (System.IO.FileNotFoundException) A System.IO.FileNotFoundException was thrown: "Could not load file or assembly 'Microsoft.IIS.PowerShell.Framework' or one of its dependencies. The system cannot find the file specified." Exception:Thrown: "Cannot find path 'C:\Users\jpacella\Documents\WindowsPowerShell\Modules' because it does not exist." (System.Management.Automation.ItemNotFoundException) A System.Management.Automation.ItemNotFoundException was thrown: "Cannot find path 'C:\Users\jpacella\Documents\WindowsPowerShell\Modules' because it does not exist." Exception:Thrown: "Process should have elevated status to access IIS configuration data." (System.InvalidOperationException) A System.InvalidOperationException was thrown: "Process should have elevated status to access IIS configuration data." Exception:Thrown: "Object reference not set to an instance of an object." (System.NullReferenceException) A System.NullReferenceException was thrown: "Object reference not set to an instance of an object." Exception:Thrown: "Cannot find drive. A drive with the name 'IIS' does not exist." (System.Management.Automation.DriveNotFoundException) A System.Management.Automation.DriveNotFoundException was thrown: "Cannot find drive. A drive with the name 'IIS' does not exist." Exception:Thrown: "" (System.Management.Automation.ParameterBindingException) A System.Management.Automation.ParameterBindingException was thrown: "" Exception:Thrown: "The pipeline has been stopped." (System.Management.Automation.PipelineStoppedException) A System.Management.Automation.PipelineStoppedException was thrown: "The pipeline has been stopped." Exception:Thrown: "A parameter cannot be found that matches parameter name 'physicalPath'." (System.Management.Automation.CmdletInvocationException) A System.Management.Automation.CmdletInvocationException was thrown: "A parameter cannot be found that matches parameter name 'physicalPath'."
These are the two different ways I invoked the script
PowerShell ps = PowerShell.Create(); ps.AddCommand("New-Item") .AddParameter("Path", newPathName) .AddParameter("ItemType", "directory"); ps.AddStatement().AddCommand("New-WebVirtualDirectory") .AddParameter("Site", Properties.Settings.Default.VirtualDirectoryPath) .AddParameter("Name", userName) .AddParameter("physicalPath", newPathName); ps.Invoke();
and (scriptContents is a string holding the
Runspace runspace = RunspaceFactory.CreateRunspace(); runspace.Open(); Pipeline pipeline = runspace.CreatePipeline(); pipeline.Commands.AddScript(scriptContents); pipeline.Commands.Add("Out-String"); Collection<PSObject> results = pipeline.Invoke(); runspace.Close();