我正在学习如何用c#开发PowerShell模块,但没有Visual Studio集成来启动导入模块的PowerShell。
我想配置 MSBuild 以启动 PowerShell 并运行 Import-Module $(TargetFileName)。
我将我的项目配置为库项目,并设置调试选项以启动PowerShell并将命令行参数设置为-NoExit -Command "Import-Module .\PowerShellModule.dll"。但是我对这个解决方案不满意。
我的目标是启动PowerShell,加载我正在处理的模块,并在Visual Studio中按F5时附加调试器。理想情况下,我想使用 MSBuild 宏来创建可重用的模板并避免使用 csproj.user 文件。
以下是相关的csproj.user文件:
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="12.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|AnyCPU'">
<StartArguments>-NoExit -Command "Import-Module .PowerShellModule.dll "</StartArguments>
<StartAction>Program</StartAction>
<StartProgram>C:WindowsSystem32WindowsPowerShellv1.0powershell.exe</StartProgram>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|AnyCPU'">
<StartAction>Program</StartAction>
<StartProgram>C:WindowsSystem32WindowsPowerShellv1.0powershell.exe</StartProgram>
<StartArguments>
</StartArguments>
</PropertyGroup>
</Project>
我能够通过在我的 csproj 中的 PropertyGroup 下添加以下行来实现我想要的。
<StartAction>Program</StartAction>
<StartProgram>C:WindowsSystem32WindowsPowerShellv1.0powershell.exe</StartProgram>
<StartArguments>-NoExit -Command "Import-Module .$(AssemblyName).dll"</StartArguments>