0

I want to add users for my FTP site using C# web api so, I have tried this link but when I am using these commands using Powershell then, these commands successfully creating a new user. Irrespective to this when I want to automate this using below code

string scriptText = @"[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.Web.Management')" + "\n" +
                    "[Microsoft.Web.Management.Server.ManagementAuthentication]::CreateUser('" + UserName + "','" + Password + "')" + "\n" +
                     "[Microsoft.Web.Management.Server.ManagementAuthorization]::Grant('" + UserName + "','" + FtpSiteName + "', $FALSE)";

                Runspace runspace = RunspaceFactory.CreateRunspace();

                runspace.Open();

                Pipeline pipeline = runspace.CreatePipeline();
                pipeline.Commands.AddScript(scriptText);

                pipeline.Commands.Add("Out-String");

                Collection<PSObject> results = pipeline.Invoke();
                runspace.Close();

                string strCmdText = "C:/Windows/System32/inetsrv/appcmd.exe set config " +
                    FtpSiteName + " - section:system.ftpServer / security / authorization / +'[accessType='Allow',users='" + UserName + "',permissions='Read, Write']' / commit:apphost";
                System.Diagnostics.Process.Start("CMD.exe", strCmdText);

then, I am receiving an error at Collection<PSObject> results = pipeline.Invoke(); which is

{Exception calling "CreateUser" with "2" argument(s): "Filename: \\?\C:\Program Files\IIS Express\config\administration.config
Error: The configuration section 'system.webServer/management/authentication' cannot be read because it is missing a section declaration

"}  System.Management.Automation.ErrorRecord

According to my observation, my C# code is editing this file (C:\Program Files\IIS Express\config\administration.config) where it could not find a relavent section whereas IIS Manager requires changes in this file (C:\Windows\System32\inetsrv\config\administration.config) for creating new user.

If anything is unclear then, please let me know. Thanks.

Farhan
  • 1
  • 2
  • 1
    The parameter list (on the command line appcmd.exe set config "MyFtpSite" -section:system.ftpServer/security/authorization /+"[accessType='Allow',users='MyUser',permissions='Read, Write']" /commit:apphost) has to be added to Process using StartInfo. The link is adding " "www.northwindtraders.com". See : https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.processstartinfo?view=net-5.0 – jdweng Mar 17 '21 at 11:50
  • Dear Jdweng, thanks for your response but my code is breaking at **Collection results = pipeline.Invoke();** also, I have tried the below code separately string strCmdText = "C:/Windows/System32/inetsrv/appcmd.exe set config " + FtpSiteName + " - section:system.ftpServer / security / authorization / +'[accessType='Allow',users='" + UserName + "',permissions='Read, Write']' / commit:apphost"; System.Diagnostics.Process.Start("CMD.exe", strCmdText); which is not giving an error and working smoothly. – Farhan Mar 17 '21 at 14:19
  • First the command appcmd.exe does not go in the startinfo. It is the command name in the Process.Start() first parameter. Second you have a lot of extra spaces in the pathname. Make sure your variable strCmdText exactly matches the working applications. – jdweng Mar 17 '21 at 14:52
  • Dear Jdweng, I apologize if I couldn't elaborate you actually, I have problem at this line of code Collection results = pipeline.Invoke(); i.e. relavent to powershell but not in the second part which is related to CMD. Thanks for your response. – Farhan Mar 17 '21 at 18:00
  • There are three steps 1) Send Command 2) Receive Response 3) Parse Response. You are getting an error at step 3. You will fail step 3 if step 1 or 2 is bad. You first have to make sure step one is correct. Right now step one is bad. – jdweng Mar 17 '21 at 19:30
  • Dear Jdweng, thanks for your prompt response but unfortunately there is no luck at my side even after trying this string scriptText = "[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.Web.Management');[Microsoft.Web.Management.Server.ManagementAuthentication]::CreateUser('" + UserName + "','" + Password + "')"; I am running same command on Powershell then, its working fine there. _FYI Username and Password are two strings._ – Farhan Mar 17 '21 at 19:54
  • There is no need for reflection in c#!!! I would start by running the Process and putting output into a file by redirecting the output using ">filename". Then checking to see if the text in file is correct : appcmd.exe set config "MyFtpSite" -section:system.ftpServer/security/authorization /+"[accessType='Allow',users='MyUser',permissions='Read, Write']" /commit:apphost >c:\temp\text.txt – jdweng Mar 17 '21 at 23:38
  • you could try to create the PowerShell script and run that file from your web API. //shell.Commands.AddScript(Input.Text); shell.Commands.AddScript("C:\\Scripts\\PowerShell\\PowerShellScript.ps1"); Contents of this test PowerShellScript.ps1 (same as what was used in the Input control): – Jalpa Panchal Mar 19 '21 at 12:06

0 Answers0