在调用PowerShell cmdlet

时间:2016-11-13 09:33:07

标签: c# powershell desktop shortcut import-module

我'试图通过C#代码在桌面上创建一个快捷方式,

  1. 打开PowerShell,
  2. 导入myModule.dll
  3. 清除屏幕,
  4. 显示了myModule.dll
  5. 的所有cmdlet

    执行C#后,快捷方式会显示在dektop上,但出于某种原因,引号会围绕整个shortcut.TargetPath设置。手动删除这些引号后,一切都很好。

    如何阻止设置这些引号?

    我的代码:

    object shDesktop = (object)"Desktop";
    WshShell shell = new WshShell();
    string shortcutAddress = (string)shell.SpecialFolders.Item(ref shDesktop) + @"\´MyModule.lnk";
    IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(shortcutAddress);
    shortcut.Description = "MyModule";
    shortcut.TargetPath = @"%Windir%\\system32\\WindowsPowerShell\\v1.0\\powershell.exe -noexit -command &{ import-module \\srv\PS\MyModule.dll;clear; get-command -Module MyModule}";
    shortcut.Save();
    

1 个答案:

答案 0 :(得分:2)

作为commented by PetSerAl,使用Arguments属性将参数传递给可执行文件:

shortcut.TargetPath = @"%Windir%\\system32\\WindowsPowerShell\\v1.0\\powershell.exe";
shortcut.Arguments = @"-noexit -command &{ import-module \\srv\PS\MyModule.dll;clear; get-command -Module MyModule}";