2

Using System.Management.Automation I tried adding a new remote desktop application in C#.

pipeline.Commands.AddScript("new-item -path RDS:\\RemoteApp\\RemoteAppPrograms -name         ‘calc2’ -ApplicationPath \"%SYSTEMDRIVE%\\Windows\\system32\\calc.exe\"");

Above is the code I used to create the application. However it throws a

System.Management.Automation.ParameterBindingException:
  A parameter cannot be found that matches parameter name 'ApplicationPath'.

My guess is new-item cmdlet does not have a ApplicationPath parameter. So C# throws a error. Although this works fine in Powershell prompt.

Any solution is appreciated!

1 Answer 1

2

The New-Item cmdlet has a Name parameter but not an ApplicationPath parameter which is why you're getting that error. New-Item is used to create empty new items like an empty file or directory (on the FileSystem provider at least). Are you trying to create a shortcut? For a shortcut try something like this:

$wshshell = New-Object -ComObject WScript.Shell
$lnk = $wshshell.CreateShortcut("RDS:\\RemoteApp\\RemoteAppPrograms\\calc2.lnk")
$lnk.TargetPath = "\"$env:WINDIR\\system32\\calc.exe\""
$lnk.Save()

In this case, the problem was a custom provider was providing the ApplicationPath dynamic parameter. The OP thought they were loading the associated module but apparently the module was installed for a 64-bit process and the hosting C# exe was compiled as x86 (32-bit). Once the module was made available to 32-bit PowerShell the issue was resolved.

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

5 Comments

Yes, that was my guess. But when creating a remote app in RDS it requires the ApplicationPath parameter. If you enter the command in powershell without ApplicationPath parameter it will prompt you to provide the value.
Ah so it sounds like RDS is an 3rd party provider. In that case, you need to get that provider loaded into the Powershell runspace you're using in C#. How do you load it from the command line? Is this an import-module or add-pssnapin? You could test this by executing "Get-PSDrive" as the command in C# and then look at the results to see if the RDS drive is already created or not.
Its using RemoteDesktopServices module. I tried loading it using InitialSessionState initialSession = initialSessionState.CreateDefault(); initialSession.ImportPSModule(new string[] { "RemoteDesktopServices" }); Runspace runspace = RunspaceFactory.CreateRunspace(initialSession); runspace.Open(); but it results in same result!
I managed to get it running. Seems to be C# is starting a x86 powershell instance where necessary dlls are not loaded. When I copied the relevant dlls it worked. Thanks for all the help!
Thanks for the follow up. I updated the answer to reflect your findings.

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.