Just a list of functions I use regularly in PSADT, will update periodically.
##***Commonly used PSADT env variables
$envCommonDesktop # C:\Users\Public\Desktop
$envCommonPrograms # C:\ProgramData\Microsoft\Windows\Start Menu\Programs
$envCommonStartMenu # C:\ProgramData\Microsoft\Windows\Start Menu
$envProgramFiles # C:\Program Files
$envProgramFilesX86 # C:\Program Files (x86)
$envProgramData # c:\ProgramData \
$envUserDesktop # c:\Users\{user currently logged in}\Desktop
$envUserStartMenuPrograms # c:\Users\{user currently logged in}\AppData\Roaming\Microsoft\Windows\Start Menu\Programs
$envSystemDrive # c:
$envWinDir # c:\windows
## *** Examples of exe install***
Execute-Process -Path “<application>.exe” -Parameters ‘/quiet’ -WindowStyle Hidden
Execute-Process -Path “$dirFiles\DirectX\DXSetup.exe” -Parameters ‘/silent’ -WindowStyle ‘Hidden’
Execute-Process -Path “uninstall_flash_player.exe” -Parameters ‘/uninstall’ -WindowStyle Hidden
##***Example to install an msi***
Execute-MSI -Action Install -Path “<application>.msi”
Execute-MSI -Action ‘Install’ -Path “Discovery 2015.1.msi”‘ -Parameters ‘/QN’
##***Install the base MSI and apply a transform***
Execute-MSI -Action Install -Path “Adobe_Reader_11.0.0_EN.msi” -Transform “Adobe_Reader_11.0.0_EN_01.mst” -Parameters ‘REBOOT=ReallySuppress /QN’
## ***Install the patch***
Execute-MSI -Action Patch -Path “Adobe_Reader_11.0.3_EN.msp”
## ***To uninstall an MSI***
Execute-MSI -Action Uninstall -Path “{5708517C-59A3-45C6-9727-6C06C8595AFD}”
## ***Run a vbscript***
Execute-Process -Path “cscript.exe” -Parameters ‘$dirFiles\whatever.vbs’
##***Remove registry key***
Remove-RegistryKey -Key HKEY_LOCAL_MACHINE\SOFTWARE\Macromedia\FlashPlayer\SafeVersions -Recurse -ContinueOnError:$True
## ***Create a reg key***
Set-RegistryKey -Key ‘HKEY_LOCAL_MACHINE\SOFTWARE\LMKR\Licensing’ -Name ‘LMKR_LICENSE_FILE’ -Value ‘@license’-Type String -ContinueOnError:$True
## ***To sleep***
Start-Sleep -Seconds 120
## ***To copy and overwrite a file***
Copy-File -Path “$dirSupportFiles\mms.cfg” -Destination “C:\Windows\SysWOW64\Macromed\Flash\mms.cfg”
## ***To copy a file***
Copy-File -Path “$dirSupportFiles\mms.cfg” -Destination “C:\Windows\SysWOW64\Macromed\Flash\”
## ***To copy a folder***
Copy-Item -Path “$dirFiles\client_1” -Destination “C:\oracle\product\11.2.0\client_1” -Recurse
## ***To delete a file or shortcut***
Remove-File -Path “C:\Users\Public\Desktop\GeoGraphix Seismic Modeling.lnk”
##***To Create a shortcut***
New-Shortcut -Path “$envProgramData\Microsoft\Windows\Start Menu\My Shortcut.lnk” -TargetPath “$envWinDir\system32\notepad.exe” -IconLocation “$envWinDir\system32\notepad.exe” -Description ‘Notepad’ -WorkingDirectory “$envHomeDrive\$envHomePath”
##***To uninstall previous installs ***
##Java example but can be used for any other apps that appear in Programs and Features
# Uninstall java for every case first
$ProductStrings = “Java 8 Update”, “Java(TM) 8 Update”,”Java(TM) 7 Update”, “Java 7 Update”,”Java(TM) 6 Update”, “Java 6 Update”, “Java(TM) 5 Update”, “Java 5 Update”, “Java 3D”, “JavaFX”, “J2SE Runtime Environment”, “Java 2 Runtime Environment”
# Remove any Java Auto Updater installations
Remove-MSIApplications “Java Auto Updater”
# Use tool-kit method to uninstall java
Write-Log -Message “Use tool-kit method to uninstall old java versions” -Source “Java-Package”
foreach($ProductString in $ProductStrings)
{
Remove-MSIApplications $ProductString
}
##*** Check for an app installed already on the computer and if so remove old version if not an MSI uninstall***
if (test-path -path “C:\Program Files\SAP\FrontEnd\SAPgui\SAPgui.exe”)
{
$version = (Get-Item “C:\Program Files\SAP\FrontEnd\SAPgui\SAPgui.exe”).versioninfo.ProductMajorPart
if ($version -eq 7300)
{
Execute-Process SAP730uninst.EXE
}
}
##***Check version of SW, if SW is version 7.30 run the uninstaller***
$version = (Get-Item “C:\Program Files\SAP\FrontEnd\SAPgui\SAPgui.exe”).versioninfo.ProductMajorPart
if ($version -eq 7300)
{
Exit-Script -ExitCode “0”
}
## ***Change Powershell Execution Policy***
Set-ExecutionPolicy unrestricted
## ***Show Welcome Popup***
Show-InstallationWelcome -CloseApps “iexplore”
Show-InstallationWelcome -AllowDefer -DeferTimes 5 -CloseApps “iexplore”
##***Execute a process (exe)***
Execute-Process “vcredist_x86.exe” /q -ContinueOnError(1)
Execute-Process setup.exe -Arguments ‘/S /v/qn’
##***Text pop-up box***
$a = new-object -comobject wscript.shell
$b = $a.popup(“This is a test message from http://whatever “,0,”Test message from <whoever>”,1)
##***Copy the response file locally as it will not run from the sccm cache***
Copy-File -Path “$dirSupportFiles\response_file.ini” -Destination “C:\temp\response_file.ini”
##***Copy over required Firmware. EXAMPLE OF IF ELSE STATEMENT***
if (test-path -path “C:\Program Files (x86)”)
{
Execute-Process CreateFolderStructure_x86.cmd
Copy-File -Path “$dirSupportFiles\NXR-710K_FIRM_SH01_REL33.HEX” -Destination “C:\Program Files (x86)\Kenwood Firmware\KPG-129D\NXR-710K_FIRM_SH01_REL33.HEX”
}
Else
{
Execute-Process CreateFolderStructure.cmd
Copy-File -Path “$dirSupportFiles\NXR-710K_FIRM_SH01_REL33.HEX” -Destination “C:\Program Files\Kenwood Firmware\KPG-129D\NXR-710K_FIRM_SH01_REL33.HEX”
}
Show-DialogBox -Title “Installation Notice” -Text “Installation will take approximately 30 minutes.” -Buttons “OK” -Icon “Exclamation” -Timeout 600
##***Update a shortcut:
## Registry key for detection method if shortcut not exist
Set-RegistryKey -Key ‘HKEY_CURRENT_USER\SOFTWARE\Microsoft\DetectionMethod\Nexus_Shortcut’ -Name ‘No_Nexus_Shortcut’ -Value ‘1’-Type String -ContinueOnError:$True
## If Else – Continue script if file to be replaced exists, exit if it doesn’t.
$CheckFile = (Test-Path “$envUserStartup\Nexus (acceptance2.0).url”)
If ($CheckFile){Write-Host “File Exists”} else {Exit-Script}
## Remove old shortcut
Remove-File -Path “$envUserStartup\Nexus (acceptance2.0).url”
##*** Replace with HTTPS shortcut
Wait-Event -Timeout 15
$shell = New-object -comobject (“wscript.shell”)
$shortcut = $shell.createshortcut(“$envUserStartup\Nexus (acceptance2.0).url”)
$shortcut.targetpath=”https://nexusvsrva.cbr.ca/Nexusv2.0.net/Nexus.application”
$shortcut.save()
##*** Import a driver (note, >= win7 must be signed, and cert must be in trusted publishers store)
Execute-Process -Path ‘PnPutil.exe’ -Parameters “/a `”$dirFiles\USB Drivers\driver.inf`””
##*** Register a DLL module
Execute-Process -FilePath “regsvr32.exe” -Parameters “/s `”$dirFiles\example\codec.dll`””
##*** Make an install marker reg key for custom detections
#for e.g. below would create something like:
#HKLM:\SOFTWARE\PSAppDeployToolkit\InstallMarkers\Microsoft_KB2921916_1.0_x64_EN_01
Set-RegistryKey -Key “$configToolkitRegPath\$appDeployToolkitName\InstallMarkers\$installName”
## ***While loop pause (incase app installer exits immediately)
#pause until example reg key
While(!(test-path -path “HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\{product-code-hereD}”)) {
sleep 5;
Write-Log -Message “$appVendor – $appName – $appVersion is still not finished installing, sleeping another 5” -Source $deployAppScriptFriendlyName;
}
#pause until example file
While(!(test-path -path “$envCommonDesktop\Example Shortcut.lnk”)) {
sleep 5;
Write-Log -Message “$appVendor – $appName – $appVersion is still not finished installing, sleeping another 5” -Source $deployAppScriptFriendlyName;
## Modify ACL on a file
#first load the ACL
$acl_to_modify = “$envProgramData\Example\File.txt”
$acl = Get-Acl “$acl_to_modify”
#add another entry to the ACL list (in this case, add all users to have full control)
$ar = New-Object System.Security.AccessControl.FileSystemAccessRule(“BUILTIN\Users”, “FullControl”, “None”, “None”, “Allow”)
$acl.SetAccessRule($ar)
#re-write the acl on the target file
Set-Acl “$acl_to_modify” $acl
##*** Modify ACL on a folder
$folder_to_change = “$envSystemDrive\Example_Folder”
$acl = Get-Acl “$folder_to_change”
$ar = New-Object System.Security.AccessControl.FileSystemAccessRule(“BUILTIN\Users”, “FullControl”, “ContainerInherit,ObjectInherit”, “None”, “Allow”)
$acl.SetAccessRule($ar)
Set-Acl “$folder_to_change” $acl
##*** Modify ACL on a regkey (ex. if users need read/write on a registry key)
Execute-Process -Path “$dirSupportFiles\setacl.exe” -Parameters ‘-on “HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\MC2” -ot reg -actn setowner -ownr “n:Users”‘
Execute-Process -Path “$dirSupportFiles\setacl.exe” -Parameters ‘-on “HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\MC2” -ot reg -actn ace -ace “n:Users;p:full”‘
## Add to environment variables
# The first input in the .NET code can have Path subtituted for any other environment variable name (gci env: to see what is presently set)
$path_addition = “C:\bin”
#add $path_addition to permanent system wide path
[Environment]::SetEnvironmentVariable(“Path”, $env:Path + “;” + $path_addition, “Machine”)
#add $path_addition to permanent user specific path
[Environment]::SetEnvironmentVariable(“Path”, $env:Path + “;” + $path_addition, “User”)
#add $path_addition to the process level path only (i.e. when you quit script, it will no longer be applied)
[Environment]::SetEnvironmentVariable(“Path”, $env:Path + “;” + $path_addition, “Process”)
##***exit codes for reboot required
#soft reboot <- will not ‘force’ restart, and sccm will progress past, but will nag to restart afterward
Exit-Script -ExitCode 3010
#hard reboot <- does not ‘force’ restart, but sccm won’t proceed past any pre-reqs without reboot
Exit-Script -ExitCode 1641
##Create Active Setup to Update User Settings
Copy-File -Path “$dirFiles\Example.exe” -Destination “$envProgramData\Example”
Set-ActiveSetup -StubExePath “$envProgramData\Example\Example.exe” `
-Description ‘AutoDesk BIM Glue install’ `
-Key ‘Autodesk_BIM_Glue_Install’ `
-ContinueOnError:$true
## Copy something to all user profiles
$ProfilePaths = Get-UserProfiles | Select-Object -ExpandProperty ‘ProfilePath’
ForEach ($Profile in $ProfilePaths) {
Copy-File -Path “$dirFiles\Example\example.ini” -Destination “$Profile\Example\To\Path\”
}
## ***To set an HKCU key***
[scriptblock]$HKCURegistrySettings = {
Set-RegistryKey -Key ‘HKEY_CURRENT_USER\SOFTWARE\Classes\AppX4hxtad77fbk3jkkeerkrm0ze94wjf3s9’ -Name ‘NoOpenWith’ -Value ‘””‘-Type String -ContinueOnError:$True
}
Invoke-HKCURegistrySettingsForAllUsers -RegistrySettings $HKCURegistrySettings
Leave a Reply