Link to home
Start Free TrialLog in
Avatar of bbimis
bbimis

asked on

convert code to powershell

can someone help me convert this code over to powershell please?
@echo off
setlocal enabledelayedexpansion

for /f "tokens=3 delims=." %%a in ('reg query "HKEY_CLASSES_ROOT\Word.Application\CurVer"') do set reg=%%a

set /a i=0
for %%b in (9 11 12 14 15) do (
  if %%b == %reg% goto setver
  set /a i+=1
)

:setver
set /a n=0
for %%c in (2000 2003 2007 2010 2013) do (
  if !n! == !i! set ver=%%c && goto endloop
  set /a n+=1
)

:endloop
echo Microsoft Office gVersion: %ver%
echo.
endlocal

:end
pause

Open in new window


i want to be able to call the function while connected to the computer (i have the connection part done)

i just want to be able to type like 22 and call a function that will produce the results like this bat file.

What i really would like to do is query that registry key for the version numbers and return the result. I'm not sure how to query the registry in powershell.

Thanks!
SOLUTION
Avatar of NinjaStyle82
NinjaStyle82
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of bbimis
bbimis

ASKER

works but only problem is it is not getting it from the remote system it is only showing it locally.
i need to pass that to a remote system so i can query the registry on the remote. sorry and thanks!
$remotecomputer = "<remotecomputername>"
$Reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('ClassesRoot', $remotecomputer)
$RegKey = $Reg.OpenSubKey("Word.Application\\CurVer")
$ver = $RegKey.GetValue($null)
$ver
$ver = $ver.Split(".")
$v = switch ($ver[2]) 
    { 
        "9" {"2000"} 
        "11" {"2003"} 
        "12" {"2007"} 
        "14" {"2010"} 
        "15" {"2013"} 
    }
Write-Output "Microsoft Office Version: $v"

Open in new window

Avatar of bbimis

ASKER

if it requires authentication do i use the -credentials -creds in this as well?
sorry new at powershell
I would just launch powershell as whatever user has permissions (i also think you need remote registry turned on). You can do this by shift-right clicking the powershell shortcut and running as different user.
just realized i have that extra $ver in there on its own line, was using it for diagnostics, but you can remove it.
Avatar of bbimis

ASKER

well i can't get it to work remotely. I guess i will just use the bat file :(
i'm having issues passing it the creds
i tried your method above but it will not work for me.
i need to be able to somehow pass the $creds = get-credential
then -Computername $compname -Credientals $creds

but i can't figure out how to use it with the openremotebasekey

thanks!
you opened powershell as something like a domain admin and ran and it didn't work? what error are you getting?
Avatar of bbimis

ASKER

my computer is on a domain yes. i connect to locations via our wan vpn (asa connections)
they ARE domained.  They so i would entered generally username administrator password "whatever" and then it wold let me in
and i pass that to the program using the get-credential function
User generated image
I suppose the only way it can be done is via wmi instead of .net remote registry. I will see if I can make something really quick in about an hour.
Avatar of bbimis

ASKER

thanks for your time. i enjoy learning this but i'm clueless :(
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of bbimis

ASKER

one question where can i look to understand what the reference 2147483648 is for?
not sure why you are storing that value? is it a default that must be used? Just trying to understand thanks!
Avatar of bbimis

ASKER

i get the following error
User generated image
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of bbimis

ASKER

thanks got it !
sweet, have a good one.