【发布时间】:2011-10-25 07:22:02
【问题描述】:
因此我们可以在 NuGet 包中包含安装/卸载 powershell 脚本。我试过了,但我的 install.ps1 不起作用。有没有可能找出原因?调试、记录,什么?
更新
请注意,该脚本是作为 Nuget 包安装过程的一部分执行的。它可能是非常特定于 Nuget 的。
【问题讨论】:
标签: c# visual-studio powershell nuget
因此我们可以在 NuGet 包中包含安装/卸载 powershell 脚本。我试过了,但我的 install.ps1 不起作用。有没有可能找出原因?调试、记录,什么?
更新
请注意,该脚本是作为 Nuget 包安装过程的一部分执行的。它可能是非常特定于 Nuget 的。
【问题讨论】:
标签: c# visual-studio powershell nuget
也许我迟到了,但这里有一个调试 NuGet 特定脚本的解决方案,NuGet 包NuGetDebugTools。它的脚本Add-Debugger.ps1 为 NuGet 包管理器控制台添加了一个简单而有效的调试器。
示例场景:
打开 NuGet 控制台并输入命令
PM> Add-Debugger [-ReadHost]
PM> Set-PSBreakpoint -Command init
PM> Set-PSBreakpoint -Command install
(或设置更具体的断点,见help Set-PSBreakpoint)
类型?作为调试器输入,看看你能做什么:
s, StepInto Step to the next statement into functions, scripts, etc.
v, StepOver Step to the next statement over functions, scripts, etc.
o, StepOut Step out of the current function, script, etc.
c, Continue Continue operation (also on empty input).
q, Quit Stop operation and exit the debugger.
?, h Display this help message.
r Display PowerShell command history.
k Display call stack (Get-PSCallStack).
<number> Show debug location in context of <number> lines.
+<number> Set location context preference to <number> lines.
<command> Invoke any PowerShell <command> and write its output.
键入其他调试器和 PowerShell 命令并在 NuGet 控制台中观察输出
v1.4.0 - 新开关 ReadHost 告诉使用 Read-Host 进行输入,而不是默认的 GUI 输入框。
【讨论】:
Add-Debugger 之后可以定义自己的全局函数Read-Debugger。例如,试试这个非常简单:function Read-Debugger($prompt) { Read-Host $prompt }。默认情况下不这样做,因为:a) 该工具适用于任何主机,某些主机不实现Read-Host; b) 即使实现了Read-Host,也可能出现一些问题。使用的对话框很傻,但它可以正常工作,总是没有问题,“除了烦恼”。
Add-Debugger 但我收到此错误消息:The term 'Add-Debugger' is not recognized as the name of a cmdlet, function, script file, or operable program.
path\Add-Debugger.ps1。
这就是我使用 PowerShell ISE 逐步完成 install.ps1 的方式:
要能够使用 PowerShell ISE 逐步执行安装脚本,请执行以下步骤: 启用使用 .Net 4 构建的程序集的执行
要么
C:\Windows\System32\WindowsPowerShell\v1.0 或者
C:\Windows\SysWOW64\WindowsPowerShell\v1.0
取决于您使用的 PS 版本 如果文件不存在,则创建它们
要么 C:\Windows\System32\WindowsPowerShell\v1.0 要么 C:\Windows\SysWOW64\WindowsPowerShell\v1.0
取决于您使用的 PS 版本
如果配置文件不存在,则创建它们
powershell.exe.config:
<configuration>
<startup useLegacyV2RuntimeActivationPolicy="true">
<supportedRuntime version="v4.0.30319"/>
<supportedRuntime version="v2.0.50727"/>
</startup>
</configuration>
powershell_ise.exe.config:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup>
<supportedRuntime version="v4.0.30319" />
</startup>
</configuration>
为了能够运行 NuGet 包中包含的 PowerShell 脚本,执行 需要更改政策:
Set-ExecutionPolicy RemoteSigned -Scope 进程
复制你要调试的install.ps1,修改内容如下:
删除参数块
param(
[Parameter(Mandatory=$true)] [string] $installPath,
[Parameter(Mandatory=$true)] [string] $toolsPath,
[Parameter(Mandatory=$true)] $package,
[Parameter(Mandatory=$true)] $project
)
导入一个允许在 VS 主机进程之外使用 nuget cmdlet 的模块
下载http://community.sharpdevelop.net/blogs/mattward/NuGet/NuGetOutsideVisualStudio.zip 将 bin 文件夹的内容提取到某个位置,然后导入 PackageManagement.Cmdlets.dll
像这样:
import-module "C:\dev\NuGetOutsideVisualStudio\bin\PackageManagement.Cmdlets.dll"
现在您可以像这样手动设置所有参数:
$toolsPath="C:\dev\demo-solution\packages\X1.Registration.DbUpdate.0.4\tools"
$installPath="C:\dev\demo-solution\packages\X1.Registration.DbUpdate.0.4"
set-project DemoSolution.Logic C:\dev\demo-solution\DemoSolution.sln
$project = Get-Project -name DemoSolution.Logic
这仍然使 $package 对象未设置,但我发现脚本并没有真正引用该参数
【讨论】:
使用Set-PsDebug -trace 2 看看发生了什么。
【讨论】:
通过 VS 中的包管理器控制台运行您的脚本(控制台上的详细信息,https://docs.nuget.org/ndocs/tools/package-manager-console)——在此过程中导致错误的任何内容都将以红色写出。
此外,您可以使用 Write-Host 将诊断跟踪类型信息写入同一控制台。
【讨论】:
您可以在安装脚本的开头调用Start-Transcript,在结尾调用Stop-Transcript。您可能会像这样包装安装代码:
try {
$ErrorActionPreference = 'stop' # stop on error
Start-Transcript c:\a.txt
...
}
catch {
write-host $_
}
finally {
Stop-Transcript
}
$ErrorActionPreference = 'inquire'(而不是停止)也可能有效。但是,现在没有机会尝试。见http://tasteofpowershell.blogspot.com/2008/07/handling-errors-in-powershell.html
【讨论】: