問題描述
我正在從 Visual Studio 2013 項目的 PostBuild 事件運行 PowerShell 構建腳本.
我為此使用的命令是(添加新行只是為了提高可讀性):
I'm running a PowerShell build script from the PostBuild event of a Visual Studio 2013 project.
The command I'm using for that is (new-lines added only for readability):
PowerShell -ExecutionPolicy ByPass -File "$(SolutionDir)..\build\build.ps1"
-SolutionFolder "$(SolutionDir)." -ProjectName "$(ProjectName)"
-OutputFolder "..\build\output" -ConfigurationName "$(ConfigurationName)"
此腳本的任務之一是查找本地 SQL Server 實例,以允許用戶附加所需的數據庫(如果它們不存在).
我正在使用以下 PowerShell 代碼來檢索本地實例:
One of the tasks of this script is to find local SQL Server instances to allow the user to attach required databases if they don't exist.
I'm using the following PowerShell code to retrieve the local instances:
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SqlWmiManagement") | out-null
$m = New-Object ("Microsoft.SqlServer.Management.Smo.Wmi.ManagedComputer") "."
if ($m.ServerInstances -eq $null -or $m.ServerInstances.Length -eq $null -or $m.ServerInstances.Length -eq 0)
{
throw New-Object [System.Exception] "No instances found."
}
$instances = $m.ServerInstances | % { ($_.Parent.Name + "\" + $_.Name) }
當我從命令行執行腳本時,這工作得很好,但是當我從 PostBuild 事件運行腳本時 $m.ServerInstances
返回 $null
.
This is working perfectly fine when I execute the script from the command-line, but when I run the script from the PostBuild event $m.ServerInstances
returns $null
.
首先想到的是用戶權限,但我檢查了哪個用戶正在執行腳本,它在命令行和 VS PostBuild 中都是一樣的.
First thing that came to mind was user rights, but I checked which user is executing the script and it's the same in both command-line and VS PostBuild.
我還嘗試了一種從注冊表中檢索可用實例的不同方法(如此處),結果相同;從命令行運行時它工作正常,但從 VS PostBuild 運行時不返回任何實例.
I've also tried a different approach that retrieves the available instances from the registry (as described here), with the same result; it works fine when running from command-line, but returns no instances when running from VS PostBuild.
所以問題是,從命令行運行和導致這種不同行為的 VS PostBuild 有什么區別?
So the question is, what's the difference between running from command-line and VS PostBuild that is causing this different behaviour?
更新:
從 Visual Studio 運行時,腳本的其他部分會停止運行.
例如,從 cmd.exe 運行腳本時創建 IIS 網站工作正常,但從 VS PostBuild 運行時拋出異常:
UPDATE:
There are other parts of the script that stop functioning when running from Visual Studio.
For example creating an IIS website works fine when running the script from cmd.exe but throws an exception when ran from VS PostBuild:
Exception occurred while creating IIS site :
Retrieving the COM class factory for component with CLSID {688EEEE5-6A7E-422F-B2E1-6AF00DC944A6}
failed due to the following error :
80040154 Class not registered (Exception from HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG)).
我嘗試了各種瘋狂的解決方法,例如使用 VS PostBuild 中的 cmd.exe/C
來執行 PowerShell 創建一個新的 PowerShell 腳本,該腳本調用 cmd.exe 來運行 PowerShell.
都給出相同的結果;該腳本在從命令提示符調用時有效,但在從 Visual Studio PostBuild 事件調用時無效.
I've tried all kinds of crazy workaround, like using cmd.exe /C
from VS PostBuild to execute PowerShell create a new PowerShell script that calls cmd.exe to run PowerShell.
All give the same result; the script works when called from a command prompt, but not when called from a Visual Studio PostBuild event.
Visual Studio 本身以提升的權限運行.
也在沒有提升權限的情況下嘗試過,結果是同樣的問題.
Visual Studio itself is running with elevated permissions.
Also tried it without elevated permissions and that results in the same issue.
更新 2:
當我從命令提示符使用 MSBuild 構建解決方案以觸發調用腳本的 PostBuild 事件時,會出現同樣的問題
UPDATE 2:
The same problem occurs when I build the solution with MSBuild from a command prompt to trigger the PostBuild event that calls the script
推薦答案
事實證明,Visual Studio 始終執行 32 位 PowerShell 而不是 64 位.
即使我在 PostBuild 事件中指定了 PowerShell 的完整路徑,它仍然執行 32 位版本.
It turns out that Visual Studio always executes the 32-bit PowerShell instead of the 64-bit.
Even when I specified the full path to PowerShell in the PostBuild event, it still executed the 32-bit version.
這導致很多命令不起作用.
我通過使用以下命令調用 PowerShell 解決了這個問題:
That caused a lot of the commands not to work.
I solved it by calling PowerShell with this command:
%WINDIR%\SysNative\WindowsPowerShell\v1.0\PowerShell.exe
這會在我的機器上執行 64 位 PowerShell 并正常運行腳本.
That executes the 64-bit PowerShell on my machine and runs the script fine.
這篇關于從 Visual Studio 運行時,PowerShell 腳本結果不同的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!