{"record":{"id":"58ccefeb99be95cb","repo":"ZyperWave/ZyperWinOptimize","slug":"powershell","errorCode":null,"errorMessage":"PowerShell 错误: ","messagePattern":"PowerShell 错误: ","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"ZyperWin++/ZyperWin++/Appx.cs","lineNumber":81,"sourceCode":"                            p.StartInfo.RedirectStandardError = true;\r\n                            p.StartInfo.CreateNoWindow = true;\r\n\r\n                            p.Start();\r\n\r\n                            string output = p.StandardOutput.ReadToEnd();\r\n                            string error = p.StandardError.ReadToEnd();\r\n                            p.WaitForExit();\r\n\r\n                            if (p.ExitCode == 0)\r\n                            {\r\n                                packages = output.Split(new[] { '\\r', '\\n' }, StringSplitOptions.RemoveEmptyEntries)\r\n                                                 .Select(s => s.Trim())\r\n                                                 .Where(s => !string.IsNullOrEmpty(s))\r\n                                                 .ToList();\r\n                            }\r\n                            else\r\n                            {\r\n                                throw new Exception(\"PowerShell 错误: \" + error);\r\n                            }\r\n                        }\r\n                    }\r\n                    catch (Exception ex)\r\n                    {\r\n                        packages.Clear();\r\n                        packages.Add(\"加载失败: \" + ex.Message);\r\n                    }\r\n                });\r\n\r\n                checkedListBox1.Invoke((MethodInvoker)delegate\r\n                {\r\n                    checkedListBox1.Items.Clear();\r\n                    if (packages.Count > 0 && !packages[0].StartsWith(\"加载失败\"))\r\n                    {\r\n                        foreach (string pkg in packages)\r\n                        {\r\n                            checkedListBox1.Items.Add(pkg, false);\r","sourceCodeStart":63,"sourceCodeEnd":99,"githubUrl":"https://github.com/ZyperWave/ZyperWinOptimize/blob/d20e78bbd906fa179e4cc9f5107b502da5967aa9/ZyperWin++/ZyperWin++/Appx.cs#L63-L99","documentation":"LoadAppListAsync enumerates installed AppX packages by running a Get-AppxPackage PowerShell script. If powershell.exe exits non-zero, it throws \"PowerShell 错误: \" + stderr. In this app the exception is caught locally and shown as a \"加载失败: ...\" list item rather than crashing.","triggerScenarios":"Calling LoadAppListAsync (on Appx control load or refresh button) when powershell.exe returns non-zero: PowerShell blocked by execution policy/AppLocker, Get-AppxPackage cmdlet missing (very old Windows / stripped-down LTSB builds), AppX deployment service (AppXSvc) disabled, or profile/registry corruption making the cmdlet throw.","commonSituations":"Heavily debloated Windows images with AppX services removed; corporate AppLocker/WDAC policies blocking unsigned PowerShell; broken user profile where Get-AppxPackage throws permission errors; running on Windows Server without the AppX module.","solutions":["Check the stderr text after 'PowerShell 错误: ' — it names the real cmdlet/policy failure.","Verify the service: `sc query AppXSvc` and start it if stopped.","Test manually: run `powershell -Command \"Get-AppxPackage\"` in a console; if it errors on execution policy, adjust or use `-ExecutionPolicy Bypass` in Arguments.","On stripped/Server SKUs where Get-AppxPackage does not exist, treat the empty list as normal instead of an error.","Repair the component store / user profile if the cmdlet reports deployment errors (DISM /Online /Cleanup-Image /RestoreHealth)."],"exampleFix":"// before\np.StartInfo.Arguments = $\"-Command \\\"{psScript}\\\"\";\n// after\np.StartInfo.Arguments = $\"-NonInteractive -ExecutionPolicy Bypass -Command \\\"{psScript}\\\"\";","handlingStrategy":"fallback","validationCode":"using var probe = Process.Start(new ProcessStartInfo(\"powershell.exe\", \"-NonInteractive -Command Get-Command Get-AppxPackage\") { UseShellExecute = false, RedirectStandardOutput = true, CreateNoWindow = true });\nstring probeOut = probe.StandardOutput.ReadToEnd();\nprobe.WaitForExit();\nbool appxAvailable = probe.ExitCode == 0 && probeOut.Contains(\"Get-AppxPackage\");\nif (!appxAvailable) { /* show UWP list as unavailable instead of erroring */ }","typeGuard":null,"tryCatchPattern":"try { await LoadAppListAsync(); }\ncatch (Exception ex) when (ex.Message.StartsWith(\"PowerShell 错误\"))\n{ MessageBox.Show($\"无法枚举 UWP 应用：{ex.Message}\\n可能被策略限制或 AppX 服务被禁用。\", \"提示\", MessageBoxButtons.OK, MessageBoxIcon.Warning); }","preventionTips":["Add -ExecutionPolicy Bypass -NonInteractive to PowerShell invocations.","Probe for Get-AppxPackage availability on load and degrade gracefully.","Don't disable AppXSvc on machines where the tool will run.","Check corporate policy environments (AppLocker/WDAC) before shipping PowerShell-based features."],"tags":["powershell","process-exit-code","windows","appx"],"backgroundTag":"http-error-response","analyzedSha":"d20e78bbd906fa179e4cc9f5107b502da5967aa9","analyzedAt":"2026-09-13T15:15:37.168Z","contentChangedAt":"2026-09-13T15:15:37.168Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}