Showing posts with label cmd. Show all posts
Showing posts with label cmd. 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, December 12, 2010

Useful CMD Script Path Expansion Variables


@echo off
rem http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/percent.mspx?mfr=true
echo displaying various path alias values...
echo %~nx0
echo %comspec%
echo %~d0
echo %~dp0
echo %~dps0
echo %~dpp0
echo %~f0
echo --------------------------------
echo %systemdrive%
echo %systemroot%
echo %windir%
echo %programdata%
echo %programfiles%
echo %userprofile%
echo %allusersprofile%
echo %temp%
echo %commonprogramfiles%
echo %commonprogramfiles(x86)%
echo %appdata%
echo %localappdata%
echo %public%

Uninstall: 3DS Max 2010

CMD Script…


@echo off
CLS
SETLOCAL
set APPNAME=3dsMax2010
set LOG=%TMP%\%APPNAME%_uninstall.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 ----------------------------------------------- >>%LOG%
echo Checking if 3ds Max 2010 is installed >>%LOG%
if exist "%programfiles%\Autodesk\3ds Max 2010" GoTo REMOVE
goto NOTFOUND
goto END

:NOTFOUND
echo %DATE% %TIME% 3DS Max 2010 was not detected on this computer
goto END

:REMOVE
echo %DATE% %TIME% removing 3ds Max 2010 32-bit >>%TMP%\3dsmax2010uninst.log
echo command = msiexec.exe /x {317AC0C7-FEBF-0409-87A3-4FC70D0ED900} %MSI% >>%LOG%
msiexec.exe /x {317AC0C7-FEBF-0409-87A3-4FC70D0ED900} %MSI%
rem ------------------------------------------------------
echo %DATE% %TIME% removing 3ds Max 2010 32-bit Components >>%TMP%\3dsmax2010uninst.log
echo command = msiexec.exe /x {60A08432-00DD-0409-AC2C-143C75460878} %MSI% >>%LOG%
msiexec.exe /x {60A08432-00DD-0409-AC2C-143C75460878} %MSI%
rem ------------------------------------------------------
echo %DATE% %TIME% removing Backburner 2008.01 >>%TMP%\3dsmax2010uninst.log
echo command = msiexec.exe /x {3D347E6D-5A03-4342-B5BA-6A771885F379} %MSI% >>%LOG%
msiexec.exe /x {3D347E6D-5A03-4342-B5BA-6A771885F379} %MSI%
rem ------------------------------------------------------
echo %DATE% %TIME% removing Tutorials Files >>%TMP%\3dsmax2010uninst.log
echo command = msiexec.exe /x {E551D82D-4D56-4AF7-A2C9-8897D7A0CB00} %MSI% >>%LOG%
msiexec.exe /x {E551D82D-4D56-4AF7-A2C9-8897D7A0CB00} %MSI%
rem ------------------------------------------------------
echo %DATE% %TIME% removing FBX Plugin 2009.4 - 3ds Max 2010 >>%TMP%\3dsmax2010uninst.log
c:
cd "%programfiles%\Autodesk\"
echo command = "FBX\FBXPlugins\2009.4\3ds Max 2010\Uninstall.exe" /S >>%LOG%
"FBX\FBXPlugins\2009.4\3ds Max 2010\Uninstall.exe" /S
goto CLEANUP
goto END

:CLEANUP
echo %DATE% %TIME% cleaning up leftover folders and files >>%LOG%
c:
if exist "%programfiles%\Autodesk\3ds Max 2010" (
echo Removing 3ds Max 2010 sub-folder >>%LOG%
cd "%programfiles%\Autodesk"
rd "3ds Max 2010" /s /q
)
if exist "%programfiles%\Autodesk\FBX" (
echo Removing FBX sub-folder >>%LOG%
cd "%programfiles%\Autodesk"
rd "FBX" /s /q
)
rem ------------------------------------------------------
echo %DATE% %TIME% exit code %errorlevel% >>%LOG%
echo %DATE% %TIME% completed >>%LOG%
goto END

:END
exit %errorlevel%

Wednesday, September 8, 2010

Script Tip: Determine the Script File Path

With BAT/CMD scripts you can simply prefix any path references with %~dps0 to get to things in the same location (folder/UNC) where the script itself resides.  So if you run..

start /wait %~dps0setup.exe /silent /norestart

It runs setup.exe from the same folder location. (that is not a typo, there should be NO space between the zero and the rest of the file path)

It's just as easy with KiXtart using the @scriptdir macro, but with VBscript it's not that simple.  But it's not difficult either…

scriptPath = Replace(Wscript.ScriptFullName, "\" & Wscript.ScriptName, "")

An example of running setup.exe in the same folder with VBscript might be…

filePath = scriptPath & "\setup.exe"
result = objShell.Run(filePath & " /silent /norestart", 7, True)

However, be careful to wrap paths in double-quotes if they contain spaces…

filePath = Chr(34) & scriptPath & "\setup.exe" & Chr(34)
result = objShell.Run(filePath & " /silent /norestart", 7, True)

Tuesday, July 20, 2010

MS Security Essentials vNext Silent Install

The new Microsoft Security Essentials is out in beta, and here's how to install it silently using a VBscript.  You still need to tweak it for UAC unless you adapt the command string for use within something else like ConfigMgr.  Enjoy…

const x86 = "mseinstall_en_us_x86.exe"
const x64 = "mseinstall_en_us_amd64.exe"

Function ScriptPath()
ScriptPath = Replace(wscript.ScriptFullName, "\" & wscript.ScriptName, "")
End Function

Set fso = createobject("Scripting.FileSystemObject")
If fso.FolderExists("c:\Program Files (x86)") Then
cmd = x64
Else
cmd = x86
End If

cmdpath = ScriptPath() & "\" & cmd

If fso.FileExists(cmdpath) Then
Set objShell = CreateObject("Wscript.Shell")
objShell.Run cmdpath & " /s /runwgacheck /o", 1, True
Set objShell = Nothing
Else
wscript.echo cmdpath & " not found"
End If

Set fso = Nothing

Thursday, March 18, 2010

Query Scheduled Tasks The Hard Way

Because the CIM/WMI model is still an unfinished work in progress, I’ve once again had to resort to making some ugly crap code to work around a giant hole in the model. In this case it’s the lack of a model counterpart to the Scheduled Tasks collection.  This is NOT the same as the Win32_ScheduledJobs or the AT command output.  I’m talking about SCHTASKS.exe and the Windows Scheduled Tasks utility.  Anyhow, the klunky workaround here is to ride the SCHTASKS command like a whipping boy and make it dump a text file, then turn around and do a crude (but effective) string parsing and crank it out in XML form.  Enjoy.
'****************************************************************
' Filename..: xml_scheduled_tasks.vbs
' Author....: David M. Stein
' Date......: 03/18/2010
' Purpose...: query Scheduled Tasks (not WMI/Jobs/AT) --> XML out
'****************************************************************

Const tempfile = "c:\temp\schtasks.txt"
Const computer = ""

Const ForReading = 1
Const ForWriting = 2

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

If computer <> "" Then
cmdstr = "cmd /c schtasks /query /s \\" & computer & " /v /fo list >" & tempfile
Else
cmdstr = "cmd /c schtasks /query /v /fo list >" & tempfile
End If

retval = objShell.Run(cmdstr, 1, True)

If objFSO.FileExists(tempfile) Then
Dim linecount, objFSO, objFile, ln, strLine

linecount = 0
wscript.echo ""

Set objFile = objFSO.OpenTextFile(tempfile, ForReading)
Do Until objFile.AtEndOfStream
strLine = Trim(objFile.Readline)

If Left(strLine, 9) = "HostName:" Then
hostname = Mid(strLine, 39)
wscript.echo vbTab & ""

ElseIf Left(strLine, 9) = "TaskName:" Then
wscript.echo vbTab & vbTab & "" & Mid(strLine, 39) & ""
wscript.echo vbTab & vbTab & "" & hostname & ""

ElseIf Left(strLine, 15) = "Scheduled Type:" Then
wscript.echo vbTab & vbTab & "" & Mid(strLine, 39) & ""

ElseIf Left(strLine, 9) = "Schedule:" Then
wscript.echo vbTab & vbTab & "" & Mid(strLine, 39) & ""

ElseIf Left(strLine, 12) = "Task To Run:" Then
wscript.echo vbTab & vbTab & "" & Mid(strLine, 39) & ""

ElseIf Left(strLine, 8) = "Comment:" Then
wscript.echo vbTab & vbTab & "" & Mid(strLine, 39) & ""

ElseIf Left(strLine, 12) = "Run As User:" Then
wscript.echo vbTab & vbTab & "" & Mid(strLine, 39) & ""

ElseIf Left(strLine, 11) = "Start Time:" Then
wscript.echo vbTab & vbTab & "" & Mid(strLine, 39) & ""

ElseIf Left(strLine, 11) = "Start Date:" Then
wscript.echo vbTab & vbTab & "" & Mid(strLine, 39) & ""

ElseIf Left(strLine, 17) = "Power Management:" Then
wscript.echo vbTab & "
"

End If
linecount = linecount + 1

Loop
objFile.Close
Set objFSO = Nothing

wscript.echo "
"

Else
wscript.echo "fail: file not found"
End If

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

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.