2017-08-05 27 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

相關問題