I like to use a wrapper to most of my software distributions to give me a bit more logging.
I've not tried my wrapper with a powershell script but you could give it a whirl:
Use it like this:
Place InstallSoftware.vbs into your package folder
Create a new file alongside InstallSoftware.vbs called: "Powershell-Test.clist"
Place your powershell file in the same directory, lets say its called eddiexuk.ps1
Edit Powershell-Test.clist with your favorite text editor and add the following content:
powershell.exe -ExecutionPolicy Bypass -NoProfile -File eddiexuk.ps1
Create a program in the packaged with the following command syntax:
cscript.exe //Nologo InstallSoftware.vbs Powershell-Test.clist
If run under system context, it should generate a log file in C:\Windows\AppLog
'InstallSoftware.vbs Version 2.9.1
'By Jesse Harris - University of the Sunshine Coast
'Changes from v2.9
' Bugfix, Environment variables are stored and retreived from Process
' context rather than User context. This bug caused variables to be
' saved into a roaming profile if script was run as a user.
'Changes from v2.8
' Added variable to working dir path. This is so that you can explicitly
' call executables which may be registered as an App Path in the registry
' Initially implemented for Outlook deployment on XP
' Path to script can be accessed via %SCRIPTPATH%
'Changes from v2.7
' Added support to run clist files from a UNC path. This allows you double
' click InstallSoftware.vbs or drag and drop a clist file onto
' InstallSoftware.vbs from a UNC path for testing purposes.
'Changes from v2.6
' Added support for Windows XP, which does not have a Username variable when
' running in the system context. Also Logfile support has a different dir
' when run from user context. Updated for all platforms
' Added feature which allows you to just run the script and it will search
' for Clist files in the current directory. If multiple Clist files are
' found all of the commands in all of the clists will be run. Order of
' running is determined alpha-numerically.
' Added more error reporting and handling for log files. Script will now exit
' more gracefully if the log file cannot be opened for append.
'Changes from v2.5
' Added special variables for 64 and 32 bit registry entries. See
' instructions for details
'Changes from v2.4
' Added custom error handling of command execution.
' Previously if a command in the clist was not found
' the execution would end with no feedback in the log.
' Now an error level is generated (Exit code 1) with a message in the Log.
'Changes from v2.3
' Under System context a new log folder is dedicated to log files
' Folder is %Windir%\AppLog
' Folder will be auto-created on first use.
' Recommended filename convention for commandlist is:
' SoftwareTitle-Action.clist (eg, Sophos-PrepareOS.clist, DotNet4-Install.clist)
'Changes from v2.2
' Changed logging to include dates of each line written to text file.
' Increased pause between commands to 5 second
' Fixed issue with detecting user/system context
'Changes from v2.1
' Added builtin dos variables for x86 and x64 independance. See instuctions
' for more details
'Changes from v1.0
' Added logging of Dos command errors as well as Dos command standard output
' Reworded instructions
'Instructions
'Simple InstallSoftware.VBS file
'Call with "cscript //Nologo InstallSoftware.vbs commandlist
'Where commandlist is a plaintext file with a series of commands to be executed
'commands in the commandlist file must be executable files.
'Dos shell commands such as dir, copy, del are not executable files
'To use these types of commands, prefix them with "cmd /c del file.txt"
'What this script does:
'1 Detects whether running under User or System context and writes a logfile
' System logfile is: C:\Windows\Temp
' User logfile is: %temp%
'2 Creates Env variables %DEFAULTSYS% and %DEFAULTPROG%, %REGISTRY%, %SCRIPTPATH%
' %DEFAULTSYS% = SYSWOW64 on x64 and SYSTEMP 32 on x86
' %DEFAULTPROG% = Program Files (x86) on x64 and Program Files on x86
' %REGISTRY% = "HKLM\Software" on x86 and "HKLM\Software\Wow6432Node" on x64
' examle: reg add "%Registry%\Adobe\Reader" /v "Version" /d "11"
' %SCRIPTPATH% = \\wsp-sccm01\smspkgd$\USC00001 or
' %SCRIPTPATH% = C:\Windows\Syswow64\CCM\Cache\USC00001
'3 Executes dos commands without spawning a dos window
'4 Logs Dos output to log file
'Configure Environment
Dim WshShell, strCommand, oExec, Context
Dim CMDFile()
Set FileSystem = WScript.CreateObject("Scripting.FileSystemObject")
Set WshShell = CreateObject("Wscript.Shell")
'Get list of commands to be run from Command List file
CMDList = GetCMDList()
'Get location of logfile depending on login conext and set Context variable
LogFile = GetLogFile()
'Initiate logfile and display context.
LogInfo "Script is running in " & Context & " Context"
'Set Architecture Special Folders ENV Variables and Reg Keys
Call SetSYS64()
'Execute Commands
LogInfo "Executing CMDList from " & CMDFile(0)
For Each CMDListSet in CMDList
For Each CMD in CMDListSet
Call Install(CMD)
Next
Next
'Wrap it up
LogInfo "All Commands Executed"
wscript.quit
'Functions
function Install(strCommand)
'Executes each command and redirects stderror and stdout to the logfile
On Error Resume Next
LogInfo "Executing (" & strCommand & ")"
Set oExec = WshShell.Exec(strCommand)
If Err Then
LogInfo "Error # " & CStr(Err.Number) & " " & Err.Description
LogInfo "Exiting with error code 1"
wscript.quit 1
End If
Do While Not oExec.StdOut.AtEndOfStream
sLine = oExec.StdOut.ReadLine
LogInfo sLine
Loop
Do While Not oExec.StdErr.AtEndOfStream
sLine = oExec.StdErr.ReadLine
LogInfo sLine
Loop
' Pause for 5 seconds to allow the installer to finish properly
Wscript.Sleep 5000
end function
function GetLogFile()
Set Shell = WshShell.Environment("Process")
UserName = wshShell.ExpandEnvironmentStrings( "%USERNAME%" )
ComputerName = wshShell.ExpandEnvironmentStrings( "%COMPUTERNAME%" )
If ComputerName = Left(UserName,Len(Username)-1) or Username = "SYSTEM" or Username = "%USERNAME%" then
LogDir = wshShell.ExpandEnvironmentStrings( "%Windir%" ) & "\AppLog\"
If Not FileSystem.FolderExists(LogDir) Then
Set NewFolder = FileSystem.CreateFolder(LogDir)
End If
Context = "System"
Else
LogDir = wshShell.ExpandEnvironmentStrings( "%TEMP%" ) & "\"
Context = "User"
End If
clistFileName = CMDFile(0)
arrPath = Split(clistFileName, "\")
intIndex = Ubound(arrPath)
LogThing = arrPath(intIndex)
GetLogFile = LogDir & LogThing & ".log"
End Function
function SetSYS64()
Set Shell = WshShell.Environment("Process")
Arch = wshShell.ExpandEnvironmentStrings( "%PROCESSOR_ARCHITECTURE%" )
If Arch = "AMD64" then
Shell( "DEFAULTSYS" ) = "%SystemRoot%\SysWOW64"
Shell( "DEFAULTPROG" ) = "%ProgramFiles(x86)%"
Shell( "REGISTRY" ) = "HKLM\SOFTWARE\Wow6432Node"
else
Shell( "DEFAULTSYS" ) = "%SystemRoot%\System32"
Shell( "DEFAULTPROG" ) = "%ProgramFiles%"
Shell( "REGISTRY" ) = "HKLM\SOFTWARE"
End if
'Shell( "SCRIPTPATH" ) = Replace(WScript.ScriptFullName, WScript.ScriptName, "")
scriptPath = FileSystem.GetFile(Wscript.ScriptFullName).ParentFolder
LogInfo "Working Dir is: " + scriptPath
Shell( "SCRIPTPATH" ) = scriptPath
End function
function GetCMDList()
intCount = 0
ReDim Preserve CMDFile(intCount)
Set args = WScript.Arguments
If args.Count = 0 then
'Find CMD file by yourself
objStartFolder = "."
Set objFolder = FileSystem.GetFolder(objStartFolder)
Set colFiles = objFolder.Files
For Each objFile in colFiles
If FileSystem.GetExtensionName(objFile) = "clist" Then
ReDim Preserve CMDFile(intCount)
CMDFile(intCount) = objFile.Name
intCount = intCount + 1
End If
Next
Else
CMDFile(0) = args.Item(0)
End If
If CMDFile(0) = "" then
wscript.echo "Error, no CMD file: Exiting with error code 1"
'LogInfo "Error, no CMD file"
'LogInfo "Exiting with error code 1"
wscript.quit 1
End If
Const ForReading = 1
intCount = 0
For Each ClistFile in CMDFile
Set objCMDFile = Filesystem.OpenTextFile(ClistFile, ForReading)
ReDim Preserve Clist(intCount)
Clist(intCount) = Split(objCMDFile.ReadAll(), VbCrLf)
intCount = intCount + 1
Next
GetCMDList = Clist
End Function
Sub LogInfo(msg)
On Error Resume Next
Const ForAppending = 8
wscript.echo msg
Set WriteOut = FileSystem.OpenTextFile(LogFile, ForAppending, True)
If Err Then
wscript.echo "Error # " & CStr(Err.Number) & " " & Err.Description
wscript.echo "Error opening log file for appending"
wscript.echo "Logfile path: " & LogFile
End If
WriteOut.WriteLine Date & " " & Time & " " & msg
WriteOut.Close
Set WriteOut = Nothing
End Sub