Mahi
Mahi

Reputation: 11

PowerShell to Batch Script

Can you please help me convert the below PowerShell Script to Batch File? I have Systems they don't have PowerShell. Please Help.

$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
}

Conversion Below

@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

Upvotes: 0

Views: 668

Answers (2)

Guru C
Guru C

Reputation: 1

function ConvertTo-Batch
{ # file names can be passed per parameter or pipeline
param([Parameter(Mandatory,ValueFromPipeline,ValueFromPipelineByPropertyName)][Alias("FullName")]$Path)



BEGIN
{ # initialize counter
$COUNTCONVERTED = 0
}



PROCESS
{ # found more than one object
if ($Path -is [System.Array])
{ # call for each object
foreach ($File in $Path) { ConvertTo-Batch $File }
# and exit function
return
}



if (Test-Path -Path $Path -PathType Leaf)
{ # file found, check extension
if ([IO.Path]::GetExtension($Path) -eq ".ps1")
{ # convert only Powershell scripts
$CONTENT = "function CTB_{"
$CONTENT += Get-Content -Path $Path -Raw -Encoding UTF8
$CONTENT += "`n};_CT_B_ $ENV:1 $ENV:2 $ENV:3 $ENV:4 $ENV:5 $ENV:6 $ENV:7 $ENV:8 $ENV:9"
$ENCODED = [Convert]::ToBase64String([System.Text.Encoding]::Unicode.GetBytes($CONTENT))
$NEWPATH = [IO.Path]::ChangeExtension($Path, ".bat")
"@echo off`nsetlocal`nset 1=%1`nset 2=%2`nset 3=%3`nset 4=%4`nset 5=%5`nset 6=%6`nset 7=%7`nset 8=%8`nset 9=%~9`npowershell.exe -NOP -EP ByPass -Enc $ENCODED" | Set-Content -Path $NEWPATH -Encoding ASCII
$COUNTCONVERTED++
Write-Output "Converted $Path`: $NEWPATH written."
}
else
{ # other file type
Write-Output "$Path is no Powershell script."
}
}
else
{ # it is a directory or the file does not exist
Write-Output "$Path is a directory or does not exist."
}
}



END
{ # report count of converted files
Write-Output "$COUNTCONVERTED files converted."
}
}

Upvotes: 0

npocmaka
npocmaka

Reputation: 57282

in case you have REG.EXE (home editions of windows come without 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
)

update

@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

Upvotes: 1

Related Questions