我在这里撞到了一点砖墙。我已经编写了一个相当大的PS脚本,它需要SQLServerProviderSnapin100,这一切都是愉快的。
为了运行脚本,我编写了一个BAT文件,让用户运行,这样他们就不必摆弄execuptionpolicy,所以它会设置它不受限制地调用脚本,并且一旦脚本完成,它就会将executionpolicy设置为受限。
我遇到的问题是,我需要BAT文件来检测用户是否将SqlServerProvider100管理单元注册到32位powershell或64位Powershell。
我知道我可以在powershell中运行这样的程序
$prov = get-pssnapin -registered | where-object{$_.Name -eq "Sqlserverprovidersnapin100"}
if($prov -eq $null){ <call powershell x86>}else{<call powershell 64Bit>}但我如何在球棒中做到这一点,或者我正在尝试做的事情甚至是可能的呢?
我希望我对一个能帮我渡过难关的人说得通!:)
发布于 2014-08-05 19:18:02
好吧,算了!
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-05 09:08:23
尝试以下两个命令:
C:\Windows\Syswow64\WindowsPowerShell\v1.0\powershell.exe "if(Get-PSSnapin -registered "SqlServerCmdletSnapin100" -ea 0){ write-host "sql snapin present in 32bit shell"}"64位:
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe "if(Get-PSSnapin -registered "SqlServerCmdletSnapin100" -ea 0){ write-host "sql snapin present in 64bit shell"}"如果存在sql管理单元,它们将返回一条漂亮的消息。
https://stackoverflow.com/questions/25134679
复制相似问题