PowerShell到批处理脚本

时间:2013-11-19 06:48:43

标签: powershell batch-file

你能帮我把下面的PowerShell脚本转换成批处理文件吗?我有他们没有PowerShell的系统。请帮助。

$inst = (get-itemproperty 'HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server').InstalledInstances
'Host Name: '+$env:COMPUTERNAME
foreach ($i in $inst)
{
'SQL Server Instance Name: '+$i
 $p = (Get-ItemProperty 'HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server\Instance Names\SQL').$i
(Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server\$p\Setup").Edition
(Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server\$p\Setup").Version
}

转化

@echo off 
echo %COMPUTERNAME%
setlocal enableDelayedExpansion
for /f "skip=2 tokens=1,3 delims= " %%S in ('reg query "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SQL Server\Instance Names\SQL"') do ( 
SET "COREEDITION=%%~S" 
SET "COREVERSION=%%~T"
SET "KEY_NAME=HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SQL Server\!COREVERSION!\Setup" 
SET "VALUE_NAME=Edition"
echo Instance Name : !COREVERSION!  
rem echo EDITION 
for /f "skip=2 tokens=3 delims= " %%E in ('reg query "!KEY_NAME!" /v !VALUE_NAME!') do echo Edition: %%E 
SET VALUES_NAME=Version 
rem echo VERSION
for /f "skip=2 tokens=3 delims= " %%V in ('reg query "!KEY_NAME!" /v !VALUES_NAME!') do echo Version: %%V 
)

endlocal

1 个答案:

答案 0 :(得分:1)

如果您有REG.EXE(Windows的家庭版没有reg.exe)

@echo off
for /f "skip=2 tokens=1,3 delims= " %%S in ('reg query "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SQL Server\Instance Names\SQL"') do (
  echo EDITION: %%S
  echo VERSION: %%T
)

<强>更新

@echo off 
setlocal enableDelayedExpansion
for /f "skip=2 tokens=1,3 delims= " %%S in ('reg query "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SQL Server\Instance Names\SQL"') do ( 
 SET "COREEDITION=%%~S" 
 SET "COREVERSION=%%~T"
 SET "KEY_NAME=HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SQL Server\!COREVERSION!\Setup" 
 SET "VALUE_NAME=Edition"
 rem echo EDITION 
 for /f "skip=2 tokens=3 delims= " %%E in ('reg query "!KEY_NAME!" /v !VALUE_NAME!') do echo edition: %%E 
 SET VALUES_NAME=Version 
 rem echo VERSION
 for /f "skip=2 tokens=3 delims= " %%V in ('reg query "!KEY_NAME!" /v !VALUES_NAME!') do echo edition: %%V 
)

endlocal