gpt4 book ai didi

sql-server - 从 Visual Studio 运行时,PowerShell 脚本结果不同

转载 作者:行者123 更新时间:2023-12-02 18:49:42 30 4
gpt4 key购买 nike

我正在从 Visual Studio 2013 项目的 PostBuild 事件运行 PowerShell 构建脚本。
我使用的命令是(添加新行只是为了便于阅读):

PowerShell -ExecutionPolicy ByPass -File "$(SolutionDir)..\build\build.ps1" 
-SolutionFolder "$(SolutionDir)." -ProjectName "$(ProjectName)"
-OutputFolder "..\build\output" -ConfigurationName "$(ConfigurationName)"

此脚本的任务之一是查找本地 SQL Server 实例,以允许用户附加所需的数据库(如果它们不存在)。
我使用以下 PowerShell 代码来检索本地实例:

[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 .

首先想到的是用户权限,但我检查了哪个用户正在执行脚本,并且在命令行和 VS PostBuild 中都是相同的。

我还尝试了一种不同的方法,从注册表中检索可用实例(如here所述),得到相同的结果;从命令行运行时它工作正常,但从 VS PostBuild 运行时不返回任何实例。

所以问题是,从命令行运行和 VS PostBuild 之间有什么区别导致了这种不同的行为?

更新:
从 Visual Studio 运行时,脚本的其他部分将停止运行。
例如,从 cmd.exe 运行脚本时,创建 IIS 网站工作正常,但从 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 事件调用时无效。

Visual Studio 本身正在以提升的权限运行。
也在没有提升权限的情况下尝试过,结果出现了同样的问题。

更新2:
当我从命令提示符使用 MSBuild 构建解决方案以触发调用脚本的 PostBuild 事件时,会出现同样的问题

最佳答案

事实证明,Visual Studio 始终执行 32 位 PowerShell,而不是 64 位。
即使我在 PostBuild 事件中指定了 PowerShell 的完整路径,它仍然执行 32 位版本。

这导致很多命令无法工作。
我通过使用以下命令调用 PowerShell 解决了这个问题:

%WINDIR%\SysNative\WindowsPowerShell\v1.0\PowerShell.exe

这会在我的计算机上执行 64 位 PowerShell 并正常运行脚本。

关于sql-server - 从 Visual Studio 运行时,PowerShell 脚本结果不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24546488/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com