简体   繁体   中英

Get-AppxPackage command doesn't work when being launched by my application (VB.NET)

My application's purpose is to run the GetAppxPackage code in PowerShell to install an user preferenced package (so for example feedback hub).

The Powershell command is as follows:

get-appxpackage -allusers feedback | Foreach {Add-AppxPackage -DisableDevelopmentMode -Register "$($_.InstallLocation)\\AppXManifest.xml"}

(There are asterisks left and right of feedback, but stackoverflow makes it italicized)

The above code runs normally in Powershell, when manually opening Powershell and running it.

The following code Iam using to run in it VB.NET

Dim w3 As String = "feedback"

Clipboard.SetText("get-appxpackage -allusers *" & w3 & "* | Foreach {Add-AppxPackage -DisableDevelopmentMode -Register " & Chr(34) & "$($_.InstallLocation)\AppXManifest.xml" & Chr(34) & "}")

Process.Start("powershell.exe", "Get-AppxPackage -allusers *" & w3 & "* | Foreach {Add-AppxPackage -DisableDevelopmentMode -Register " & Chr(34) & "$($_.InstallLocation)\AppXManifest.xml" & Chr(34) & "}")

The clipboard is not neccesary obviously, but I used it to confirm the code is exactly the same as it should be.

Powershell outputs the following (when being launched from my application):

Add-AppxPackage : A Positional Parameter cannot be found that accepts argument '\\AppXManifest.xml' At line:1 char:49 + ... | Foreach {Add-AppxPackage -Disabledevelopmentmode -Register $($_.In ... + + Categoryinfo : InvalidArgument: (:) [Add-AppxPackage]. ParameterBindingexception + FullyQualifiedErrorId : PositionalParameterNotFound.Microsoft.Windows.Appx.PackageManager.Commands.AddAppxPackageCommand

My application is running with Administrative rights, and so is Powershell.

Do not know what the problem is, or could be, since the code is exactly the same. Would really appreciate any help towards solving this problem.

Thank you.

Process-Start and the native Start-Process all have very specific quoting requirements, especially when multiple arguments are needed.

For example:

Process.Start("Powershell.exe", @"""ScriptwithArguments.ps1"" 'arg1' 'arg2 asdf'")

You will also see arguments that use comma separation.

I am also not real sure as to why you have all the ASCII codes, for a single PowerShell one-liner command and the normal string quotes. Which is native PowerShell would be just this.

Start-Process -FilePath Powershell -ArgumentList "Get-AppxPackage -AllUsers | ForEach{ Add-AppxPackage -DisableDevelopmentMode -Register $($_.InstallLocation)\\AppxManifest.xml -WhatIf}"

So, your...

Process.Start("powershell.exe", "Get-AppxPackage -allusers *" & w3 & "* | Foreach {Add-AppxPackage -DisableDevelopmentMode -Register " & Chr(34) & "$($_.InstallLocation)\AppXManifest.xml" & Chr(34) & "}")

… should just be this, because you only have one argument, that entire command string.

Process.Start('powershell.exe',"Get-AppxPackage -AllUsers | ForEach{ Add-AppxPackage -DisableDevelopmentMode -Register $($_.InstallLocation)\\AppxManifest.xml}")

See also these discussions as well for more edification...

How to run Powershell scripts in VB.Net

Running powershell scripts from within a .NET windows app

Calling a PowerShell Script From Your .NET Code

Executing PowerShell scripts from C# This would be similar in VB.Net of course.

Process.​Start Method

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM