在桌面上创建快捷方式,调用PowerShell cmdlet



我正在尝试用c#代码在桌面上创建一个快捷方式,

  1. 打开PowerShell,
  2. 进口myModule.dll
  3. 清除屏幕,
  4. 显示myModule.dll的所有cmdlet。

执行c#后,快捷方式出现在桌面,但由于某些原因,整个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 \srvPSMyModule.dll;clear; get-command -Module MyModule}";
shortcut.Save();

正如PetSerAl所评论的那样,使用Arguments属性将参数传递给可执行文件:

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

最新更新