Showing posts with label registry. Show all posts
Showing posts with label registry. Show all posts

Thursday, October 20, 2011

Query Installed Apps a Different Way

For whatever reason, in some environments, a WMI query of Win32_Product is God-awful slow.  I've seen this on Windows 7 and Windows 7 SP1 clients, as well as on Windows Server 2008 and 2008 R2.  The symptom can be seen from WIM script, WBEM, and using WMIC from a command console with very similar results:  The query hangs for 20-25 seconds and then begins executing in spurts.  Other Win32 classes work fine, from what I've seen, it's just Win32_Product for some reason.  One workaround is to dump a registry output file, and scrub it to make a "clean" output file.  You can port this to PowerShell or KiXtart if you want (or whatever you prefer, I really don't care as long as you're happy and that makes me happy so we're all happy. yay!)

'****************************************************************
' Filename..: installedApps.vbs
' Author....: David M. Stein aka Scriptzilla aka dipshit
' Date......: 10/20/2011
' Purpose...: save query of installed applications to local file
'****************************************************************

Const strInputFile  = "c:\regoutput.txt"
Const strOutputFile = "c:\installedApps.txt"

Const ForReading = 1
Const ForWriting = 2
Const adVarChar = 200

cmd = "reg query hklm\software\microsoft\windows\currentversion\uninstall /s >" & strInputFile

On Error Resume Next

Set objShell = CreateObject("Wscript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")

wscript.echo "info: executing shell command to create temp file..."

objShell.Run "cmd /c " & cmd, 7, True

wscript.echo "info: getting temp file for input..."

If objFSO.FileExists(strInputFile) Then
 wscript.echo "info: reading temp file..."
 Set objFile = objFSO.OpenTextFile(strInputFile, ForReading)
 Set objFile2 = objFSO.CreateTextFile(strOutputFile, True)

 Set rs = CreateObject("ADODB.RecordSet")

 rs.CursorLocation = adUseClient
 rs.Fields.Append "productname", adVarChar, 255
 rs.Open

 Do Until objFile.AtEndOfStream
     strLine = objFile.Readline
     If Left(strLine, 25) = "    DisplayName    REG_SZ" Then
      strOutput = Trim(Mid(strLine, 30))
   rs.AddNew
   rs.Fields("productname").value = strOutput
   rs.Update
     End If
 Loop
 
 rs.Sort = "productname"
 
 Do Until rs.EOF
     objFile2.WriteLine(rs.Fields("productname").value)
  rs.MoveNext
 Loop
 rs.CLose
 Set rs = Nothing
 
 objFile.Close
 objFile2.Close
 wscript.echo "info: finished scrubbing input to new output file"
Else
 wscript.echo "fail: temp file not found"
End If

Set objFSO = Nothing
Set objShell = Nothing
'----------------------------------------------------------------

wscript.echo "info: processing complete!"

Tuesday, February 15, 2011

Template CMD Script for App Installs

This is obviously bare-bones and will need to be modified to suit specific implementations.  There are dozens of ways to execute a software installation, including setup.exe files, extracting ZIP and other archive files, registering DLLs with REGSVR32, and the ugly-ass, vice-grip + screwdriver + duct tape approach where you manually build folder trees, copy files, register DLLs, add and modify registry keys and make or replace shortcuts.  Oh, the torture.

EDIT: Updated 7/30/2011 to include errorlevel 3010 (reboot pending)


@echo off
rem ****************************************************************
rem  Filename..: setup.cmd
rem  Author....: David M. Stein
rem  Date......: 07/30/2011
rem  Purpose...: install apps in controlled sequence
rem ****************************************************************
rem Additional Notes:
rem
rem ****************************************************************
title Installing Applications
CLS
echo Installing Applications...
SETLOCAL
set APPNAME=MyApplicationSuite2011
set LOG=%TMP%\%APPNAME%_install.log
set MSI=/quiet /norestart
echo %DATE% %TIME% installing... %APPNAME%... >%LOG%
echo %DATE% %TIME% source....... %~dps0 >>%LOG%
echo %DATE% %TIME% target....... %COMPUTERNAME% >>%LOG%
echo %DATE% %TIME% windir....... %WINDIR% >>%LOG%
echo %DATE% %TIME% progfiles.... %PROGRAMFILES% >>%LOG%
echo %DATE% %TIME% temp......... %TMP% >>%LOG%
echo INSTALL LOG: %LOG%
echo ----------------------------------------------- >>%LOG%
echo *** APPLICATION NAME 1 >>%LOG%
echo %DATE% %TIME% info: checking if application is already installed... >>%LOG%
if exist "%ProgramFiles%\FolderName\filename.exe" (
echo %DATE% %TIME% info: ## application is already installed >>%LOG%
) else (
echo %DATE% %TIME% info: ## installing application... >>%LOG%
echo %DATE% %TIME% command = msiexec /i "%~dps0Folder\filename.msi" TRANSFORMS="%~dps0Folder\filename.MST" >>%LOG%
   msiexec /i "%~dps0Folder\filename.msi" TRANSFORMS="%~dps0Folder\filename.MST" %MSI%
   if %errorlevel%==0 (
      echo %DATE% %TIME% info: installation SUCCESSFUL >>%LOG%
   ) else (
      if %errorlevel%==3010 (
         echo %DATE% %TIME% info: installation SUCCESSFUL [reboot pending] >>%LOG%
      ) else (
         echo %DATE% %TIME% file: exit code is %errorlevel% >>%LOG%
         rem Raise error to parent process!!
         exit %errorlevel%
      )

   )
)
rem ------------------------------------------------
rem echo *** APPLICATION NAME 2 >>%LOG%
rem ------------------------------------------------
rem
rem repeat code above with modifications as needed
rem
rem ------------------------------------------------
echo %DATE% %TIME% info: adjusting application folder permissions... >>%LOG%
cacls "%ProgramFiles%\FolderName" /T /E /C /G Users:C
echo ----------------------------------------------- >>%LOG%
echo %DATE% %TIME% info: applying attachmate file association fix... >>%LOG%
REG DEL HKCR\.xxx /f
REG ADD HKCR\.xxx /ve /d "ApplicationClass.ProgName.1" /f
echo ----------------------------------------------- >>%LOG%
echo %DATE% %TIME% info: adjusting registry permissions... >>%LOG%
REGINI.exe %~dps0customregsettings.ini
echo ----------------------------------------------- >>%LOG%
echo %DATE% %TIME% completed! result code: %errorlevel% >>%LOG%
ENDLOCAL
rem Raise error to parent process!!
exit %errorlevel%

Sunday, October 25, 2009

Windows 7 Clean Install using Upgrade Media

Just a slightly different spin to the post by Paul Thurrott about performing a clean install of Windows 7 using “upgrade” media without having to install XP or Vista first.  This just wraps the steps inside a .BAT script.  Make sure you read Paul’s article first or you’ll be lost.  Be sure to run this from a CMD console that was launched via “Run as Administrator” or it won’t work…

@echo off
rem the following line between the divider lines should NOT wrap!
rem --------------------------------
REG ADD "HKLM/Software/Microsoft/Windows/CurrentVersion/Setup/OOBE/" /v MediaBootInstall /d 1 /t REG_DWORD /f
rem --------------------------------
echo Registry key value has been set.
pause
slmgr /rearm
echo Activation skip-rearm has been set.
echo Press ENTER to reboot your computer now...
pause
shutdown -r -f -t 2

Saturday, October 3, 2009

BAT - Enable Remote Desktop on Remote Computer

Tested on XP, Vista, Windows 7, Windows Server 2003 and Windows Server 2008.

@echo off
if %1=="" (
    goto USAGE
) else (
    goto VERIFY
)

:USAGE
echo ***************************************
echo Usage:
echo enable_remote_desktop.bat [computername]
echo .
echo ***************************************
goto END

:VERIFY
if EXIST \\%1\c$\windows\system32 (
    goto ENABLE
) else (
    goto OFFLINE
)

:OFFLINE
echo ***************************************
echo %1 is not accessible
echo check the name and try again or ensure
echo the client is online and firewall is
echo not preventing access
echo ***************************************
goto END

:ENABLE
echo Configuring registry setting on %1...
REG ADD \\%1\HKLM\SYSTEM\CurrentControlSet\Control\Terminal Server /v fDenyTSConnections /t REG_DWORD /d 0 /f
rem goto REBOOT
echo %1 has been configured
goto END

:REBOOT
echo Requesting a restart of %1...
shutdown -m \\%1 -r -f -t 5
echo Request submitted.  Please allow a few minutes before
echo attempting to connect via remote desktop.
goto END

:END

Thursday, September 24, 2009

Group Policy: Hide Locked User Display on Windows 7

While I did show how to set this in the registry using various script options (Daniel Petri demonstrated it first by using the registry editor), there is a MUCH easier way: Group Policy.

Computer Configuration
...Policies
   ...Windows Settings
      ...Security Settings
         ...Local Policies
            ...Security Options:

Interactive logon: Display user information when the session is locked
    Enable --> "Do not display user information"

Registry: Hide Locked User Name on Windows 7

Borrowing from Daniel Petri's post on how to hide the display of the logged-on user when a Windows 7 computer is locked, I wanted to try to do this in different languages/scripts for the hell of it.

CMD console using REG.exe...

REG ADD HKLM\Software\Microsoft\Windows\CurrentVersion\Policies\System /v DontDisplayLockedUserId /t REG_DWORD /d 3 /f

VBScript using Registry object...

Const HKEY_LOCAL_MACHINE = &H80000002
' more at http://msdn2.microsoft.com/en-us/library/aa394600.aspx

Sub AddKey(strComputer, strKeyPath)
    Set objReg=GetObject( _
        "winmgmts:{impersonationLevel=impersonate}!\\" & _
        strComputer & "\root\default:StdRegProv")
    objReg.CreateKey HKEY_LOCAL_MACHINE,strKeyPath
End Sub
Post Options
Sub AddDWValue(strComputer, strKeyPath, strValueName, iValue)
    Set objReg=GetObject( _
        "winmgmts:{impersonationLevel=impersonate}!\\" & _
        strComputer & "\root\default:StdRegProv")
 objReg.SetDWordValue HKEY_LOCAL_MACHINE, strKeyPath, strValueName, iValue
End Sub

Const k = "SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System"
AddKey "Computer1", k
AddDWValue "Computer1", k, "DontDisplayLockedUserId", 3

KiXtart...

$k = "HKLM\Software\Microsoft\Windows\CurrentVersion\Policies\System"
$=WriteValue($k, "DontDisplayLockedUserId", 3, "REG_DWORD")

This was thrown together pretty quick so it might need tweaking.

Friday, July 10, 2009

VBScript: Sort Start Menu Items


Const optionType = 1
Const regBase = "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\MenuOrder"

Set objShell = CreateObject("Wscript.Shell")

If optionType = 1 Then
' a sloppy, cheap, but effective way to do this with Wscript
cmd1 = "start /wait reg delete """ & regBase & "\Start Menu"" /f"
cmd2 = "start /wait reg delete """ & regBase & "\Start Menu2"" /f"
cmd3 = "start /wait taskkill /im explorer.exe /F"
cmd4 = "explorer.exe"
objShell.Run "cmd.exe /c " & cmd1, 1, True
objShell.Run "cmd.exe /c " & cmd2, 1, True
objShell.Run "cmd.exe /c " & cmd3, 1, True
objShell.Run "cmd.exe /c " & cmd4, 1, True
Else
' a more elegant, poofy and girly way of doing this with style and ambiance
objShell.RegDelete regBase & "\Start Menu\"
objShell.RegDelete regBase & "\Start Menu2\"
objShell.Run "cmd.exe /c start /wait taskkill /im explorer.exe /F", 1, True
objShell.Run "cmd.exe /c explorer.exe", 1, True
End If

Set objShell = Nothing