2017-08-05 71 views
-1

我正在C#中工作,用于清理Windows 10开始菜单并分配自定义布局的程序。为此,我需要从powershell运行一个命令,并且在运行时遇到错误。运行过程中的Powershell错误

我是如何完成这项任务的。

我开始C:\ \ \ powershell.exe和传球的-command参数:。进口StartLayout -LayoutPath C:\ StartMenu.xml -MountPath C:\

Process process = new Process(); 
process.StartInfo.FileName = @"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe"; 
process.StartInfo.Arguments = @" -command Import-StartLayout -LayoutPath C:\StartMenu.xml -MountPath C:\"; 
process.Start(); 

以下是错误我收到:

Import-StartLayout : The term 'Import-StartLayout' is not recognized as the name of a cmdlet, function, script file, 
or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and 
try again. 
At line:1 char:1 
+ Import-StartLayout -LayoutPath C:\StartMenu.xml -MountPath C:\; Start ... 
+ ~~~~~~~~~~~~~~~~~~ 
    + CategoryInfo   : ObjectNotFound: (Import-StartLayout:String) [], CommandNotFoundException 
    + FullyQualifiedErrorId : CommandNotFoundException 

任何想法,为什么CMD或PowerShell中不会采取进口StartLayout的外部cmdlet的?

+0

哪个版本的windows是测试电脑 –

+1

PS脚本是否从PowerShell CLI界面运行? – zwork

+1

“*我如何完成任务*” - 那个位在哪?你如何运行PowerShell? – TessellatingHeckler

回答

-1

Import-StartLayout在Windows 7和更早的版本中不存在,如果您知道,请继续。
你可以尝试使用System.Diagnostics.Process类似如下:

Process powerShell = new Process() 
{ 
    StartInfo = 
    { 
     Arguments = "Import-StartLayout -LayoutPath C:\\StartMenu.xml -MountPath C:\\", 
     FileName = "powershell" 
    } 
}; 
powerShell.Start(); 

另一种方法是使用System.Management.Automation这是不是一个正式的由微软支持的包。

using (Runspace runSpace = RunspaceFactory.CreateRunspace()) 
{ 
    runSpace.Open(); 
    using (Pipeline pipeline = runSpace.CreatePipeline()) 
    { 
     Command importStartLayout = new Command("Import-StartLayout"); 
     importStartLayout.Parameters.Add("LayoutPath", "C:\\StartMenu.xml"); 
     importStartLayout.Parameters.Add("MountPath", "C:\\"); 
     pipeline.Commands.Add(importStartLayout); 
     Collection<PSObject> resultsObjects = pipeline.Invoke(); 

     StringBuilder resultString = new StringBuilder(); 
     foreach (PSObject obj in resultsObjects) 
     { 
      resultString.AppendLine(obj.ToString()); 
     } 
    } 
} 
+0

/C或/ K不起作用,因为它是Powershell命令而不是CMD命令。 – Throdne

相关问题