none
Want to covert in to powershell

    Question

  • I have some part of my batch script & I want to convert it into powershell

    Batch Script

    :DTVerCheck
      ver | %WINDIR%\SYSTEM32\find "Windows XP" >nul
       If not %errorlevel%==1 goto XPCheck
      Ver | %WINDIR%\SYSTEM32\find "Windows [Version 6" >nul
       if not %errorlevel%==1 goto VistaCheck
      ver | %WINDIR%\SYSTEM32\find "Windows [Version 5.2" >nul
       if not %errorlevel%==1 goto XPCheck
      ver | %WINDIR%\SYSTEM32\find "Windows [Version 5.0" >nul
       if not %errorlevel%==1 goto NTServer   

      call %LOGONSERVER%\NETLOGON\gettype.exe
       if errorlevel=9 goto FILENOTFOUND
       if ERRORLEVEL=8 goto NTServer
       if ERRORLEVEL=7 goto NTServer
       if ERRORLEVEL=6 goto NTServer
       if ERRORLEVEL=5 goto NTServer
       if ERRORLEVEL=4 goto NTServer
       if ERRORLEVEL=3 goto NTServer
       if ERRORLEVEL=2 goto XPCheck
       if ERRORLEVEL=1 goto XPCheck

    In the above Code DTVerCheck, FILENOTFOUND,NTServer,XPCheck are all functions. Also how call we replace call in powershell. I tried little bit but not sure whether it's write or not. Below is the code that I have tried

    Function DTVerCheck

    if ((get-wmiobject Win32_OperatingSystem).Caption -like "*windows xp*")
    {XPCheck}
    if ((get-wmiobject Win32_OperatingSystem).Caption -like "*Windows [Version 6*")
    {VistaCheck}
    if ((get-wmiobject Win32_OperatingSystem).Caption -like "*Windows [Version 5.2*")
    {XPCheck}
    if ((get-wmiobject Win32_OperatingSystem).Caption -like "*Windows [Version 5.0*")
    {NTServer}

    but I have no idea how to script after the call statement.

    Tuesday, October 26, 2010 1:00 PM

Answers

  • One way is to use script blocks:

    $XPCheck = {

    <XP Check routines go here>

    }

     

    if ((get-wmiobject Win32_OperatingSystem).Caption -like "*windows xp*")
    {&$XPCheck}


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "
    Tuesday, October 26, 2010 1:19 PM
    Moderator
  • Hi,

    You don't need to use the gettype.exe in PowerShell since it can use WMI. For example:

    $osName = (get-wmiobject Win32_OperatingSystem).Caption
    if ($osName -like "*windows xp*) {
      ...
    }

    However: A PowerShell script may not be the best choice to use for a logon script unless you have a guarantee somehow that every computer connected to your network has PowerShell installed on it.

    Bill

    Tuesday, October 26, 2010 2:47 PM
    Moderator

All replies

  • One way is to use script blocks:

    $XPCheck = {

    <XP Check routines go here>

    }

     

    if ((get-wmiobject Win32_OperatingSystem).Caption -like "*windows xp*")
    {&$XPCheck}


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "
    Tuesday, October 26, 2010 1:19 PM
    Moderator
  • But even mine will work right ? My main concern is how I will script for

     call %LOGONSERVER%\NETLOGON\gettype.exe
       if errorlevel=9 goto FILENOTFOUND
       if ERRORLEVEL=8 goto NTServer
       if ERRORLEVEL=7 goto NTServer
       if ERRORLEVEL=6 goto NTServer
       if ERRORLEVEL=5 goto NTServer
       if ERRORLEVEL=4 goto NTServer
       if ERRORLEVEL=3 goto NTServer
       if ERRORLEVEL=2 goto XPCheck
       if ERRORLEVEL=1 goto XPCheck

    how I will use call in powershell.

    Tuesday, October 26, 2010 1:27 PM
  • Hi,

    You don't need to use the gettype.exe in PowerShell since it can use WMI. For example:

    $osName = (get-wmiobject Win32_OperatingSystem).Caption
    if ($osName -like "*windows xp*) {
      ...
    }

    However: A PowerShell script may not be the best choice to use for a logon script unless you have a guarantee somehow that every computer connected to your network has PowerShell installed on it.

    Bill

    Tuesday, October 26, 2010 2:47 PM
    Moderator
  • I can take care of having powershell on each machine, but the reason I am trying this is Win 7 & the later versions of Win will not support .bat fully. Hence i have planned to use powershell.

    Also If I ignore the half part of my batch code that is after gettype.exe it will work fine right ? and the OS will be determined.

    Tuesday, October 26, 2010 3:18 PM
  • What does "the later versions of Win will not support .bat fully" mean? Windows 7 has Cmd.exe, which executes shell scripts ("batch files") just fine.

    Bill

    Tuesday, October 26, 2010 3:24 PM
    Moderator
  • Ok shall I continue using batch file.
    Tuesday, October 26, 2010 3:31 PM
  • For logon scripts, I recommend WSH scripts. But it depends on what specifically you want to do. If what you have is working, why change it?

    Bill

    Tuesday, October 26, 2010 3:59 PM
    Moderator