0

我在这里碰到了一点砖墙。我编写了一个相当大的 PS 脚本,它需要 SQLServerProviderSnapin100,这一切都很顺利。

为了运行脚本,我编写了一个 BAT 文件供用户运行,这样他们就不必摆弄 execuptionpolicy,因此它设置为不受限制地调用脚本,一旦脚本完成,它将执行策略设置回受限制。

我遇到的问题是我需要 BAT 文件来检测用户是否将 SqlServerProvider100 管理单元注册到 32Bit Powershell 或 64Bit Powershell。

我知道我可以在 powershell 中运行这样的东西

$prov = get-pssnapin -registered | where-object{$_.Name -eq "Sqlserverprovidersnapin100"}
if($prov -eq $null){ <call powershell x86>}else{<call powershell 64Bit>}

但是我如何在 BAT 中做到这一点,或者我正在尝试做的事情是否可能?

我希望我对能够帮助我克服这个障碍的人有意义!:)

4

2 回答 2

0

Try these 2 commands:

C:\Windows\Syswow64\WindowsPowerShell\v1.0\powershell.exe "if(Get-PSSnapin -registered "SqlServerCmdletSnapin100" -ea 0){ write-host "sql snapin present in 32bit shell"}"

for 64bit:

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe "if(Get-PSSnapin -registered "SqlServerCmdletSnapin100" -ea 0){ write-host "sql snapin present in 64bit shell"}"

They will return a pretty message if sql snapin is present.

于 2014-08-05T09:08:23.893 回答
0

好的,解决了!

FOR /F "usebackq delims=" %%i IN (`powershell -command "get-pssnapin -registered | where-object { $_.Name -eq 'sqlserverprovidersnapin100' }"`) DO ( set snapin=%%i )
REM if /i {%snapin:~0,4%}=={%test:~0,4%} (goto :there)
if not defined snapin goto :notthere
(goto :there)
:there
echo %snapin%
pause
exit

:notthere
echo "Not loaded!"
pause
exit

这个练习现在教会了我两件事:显然,当我在 BAT 中针对 NULL 进行测试时,如果值为 NULL,它将删除变量,其次如何将 PS 变量传递回 BAT。

于 2014-08-05T19:18:02.790 回答