Thursday, September 18, 2014

Silently Deploy FMAPlus 3 via SCCM

* Script updated 8/19 to fix an issue.

FMAPlus 3 or Fuels Management Analyst is a software we use in our Forestry computing labs here at the university. While tackling the install and user setup, I decided to try working with PowerShell since I was learning it. If you don't use PowerShell, you can modify the commands for your scripting language of choice.

We are going to script the following:
1) Install FMAPlus for all users silently.
2) Copy a folder full of photos to the FMAPlus 3 directory in Program Files.
3) Use active setup to register the software and point to the photo folder.
4) Copy a settings file to Program Files.
5) Change the permissions on the FMAPlus 3 directory in Program Files to allow Users to Modify.
6) Remove the shortcut from the desktop.
7) Rename the shortcut in the Start Menu from "Gateway.lnk" to "FMAPlus 3.lnk"

I will take you through each step, explain what I did and then combine everything at the end into the PowerShell script and SCCM application deployment. These techniques may be useful when you are trying to create your own installer scripts.

1) Install FMAPlus for all users silently.

$currentDirectory = split-path -parent $MyInvocation.MyCommand.Definition
Start-Process $currentDirectory\fma300.exe -ArgumentList '/S /v"/qn ALLUSERS=2"' -Wait

The /v parameter passes parameters to the MSI that gets extracted during the install to C:\Windows\Installer. I wanted to pass the /qn (silent) and the ALLUSERS=2 property, otherwise the shortcuts in the Start Menu would only exist for the user that ran the install, in my case, the SYSTEM account. Which means there would be no shortcuts in the Start Menu for the lab users. The fact that the .EXE extracts the MSI to the C:\Windows\Installer folder and leaves it there after completing setup is nice, I was able to find it after running a hardware inventory on a test computer after installing the software by hand. This helped me get the GUID from the MSI for the detection method later.

2) Copy a folder full of photos to the FMAPlus 3 directory in Program Files.

Copy-Item -Path PhotoImages -Destination 'C:\Program Files (x86)\FMAPlus 3\PhotoImages' -Recurse -Force


Self Explanatory. Copy-Item does exactly what it says! I am not sure but I think this folder contains special photo's our faculty use in classes. Regardless, the lab team tells me it needs to be there so I put it there.

3) Use active setup to register the software and point to the photo folder.

We need to use active setup for this install as the program registers its license per user in the HKCU  
registry. So what I did was install the application on a VM, put in the license code during the first run, run Photo Manager and set the Photo Image Location. Export the keys located at: 
HKEY_CURRENT_USER\Software\VB and VBA Program Settings\Fuels Management Analyst 3\General] .  Here I have the reg file I exported, substitute the "X"s with your own license code. The reg file should look like: 

Windows Registry Editor Version 5.00

[HKEY_CURRENT_USER\Software\VB and VBA Program Settings\Fuels Management Analyst 3\General]
"RegNumber1"="xxxx"
"RegNumber2"="xxxxx"
"RegNumber3"="xxxx"
"RegNumber4"="xxxxx"
"Registered"="True"
"CMIExample"="True"
"PhotoDir"="c:\\Program Files (x86)\\FMAPlus 3\\


I cant remember what "CMIExample" is for, but it may not need to be set to remove first run prompts. Save this reg file to your source directory with the install file. I call mine "hkcu_fmaplus.reg" 
The following PowerShell command copies the reg file to the Program Files: 


Copy-Item -Path hkcu_fmaplus3.reg -Destination 'C:\Program Files (x86)\FMAPlus 3\hkcu_fmaplus3.reg'
And these three commands setup the active setup:

New-Item -Path Registry::'HKLM\Software\Microsoft\Active Setup\Installed Components\HSU-FMAPlus3' -Force
New-ItemProperty -Path Registry::'HKLM\Software\Microsoft\Active Setup\Installed Components\HSU-FMAPlus3' -Name StubPath -PropertyType String -Value 'CMD /C REG IMPORT "%ProgramFiles(x86)%\FMAPlus 3\hkcu_fmaplus3.reg"' -Force
New-ItemProperty -Path Registry::'HKLM\Software\Microsoft\Active Setup\Installed Components\HSU-FMAPlus3' -Name Version -PropertyType String -Value '2,0,0,0' -Force

4) Copy a settings file to Program Files.

When you run Fuel Model Manager the first time, it prompts you if you have existing custom fuel models from version 2 and do you want to load them, it then creates a file called "MyFuelModels.FFM" in the program directory. We don't have any custom models from our labs, so I click no and save the "MyFuelModels.FFM" file to my source directory. During installation, I copy this file back into the Program Directory with this command: 


Copy-Item -Path MyFuelModels.FFM -Destination 'C:\Program Files (x86)\FMAPlus 3\MyFuelModels.FFM'

5) Change the permissions on the FMAPlus 3 directory in Program Files to allow Users to Modify.

When the program is run by the user, it creates shortcuts in the Program Files\FMAPlus 3 folder for some ungodly reason, and if it can't, the program errors out. Users typically don't have permissions to write to Program Files, so we must modify the folder to allow them to write.

$Acl = Get-Acl 'C:\Program Files (x86)\FMAPlus 3'
$Arguments = "BUILTIN\Users","FullControl","ContainerInherit, ObjectInherit", "None", "Allow"
$accessRule = New-Object System.Security.AccessControl.FileSystemAccessRule $arguments
$Acl.SetAccessRule($accessRule)
$acl | Set-Acl 'C:\Program Files (x86)\FMAPlus 3'

6) Remove the shortcut from the desktop.

if (Test-Path 'C:\Users\Public\Desktop\FMAPlus 3.lnk')
{
  Remove-Item -Path 'C:\Users\Public\Desktop\FMAPlus 3.lnk' -Force
}

7) Rename the shortcut in the Start Menu from "Gateway.lnk" to "FMAPlus 3.lnk"

Rename-Item 'C:\ProgramData\Microsoft\Windows\Start Menu\Programs\FMAPlus 3\Gateway.lnk' 'C:\ProgramData\Microsoft\Windows\Start Menu\Programs\FMAPlus 3\FMAPlus 3.lnk'

Putting It All Together

Combine all your PowerShell commands into a script, I called mine "install_fma.ps1"

$currentDirectory = split-path -parent $MyInvocation.MyCommand.Definition
# Install FMAPlus 3
Start-Process $currentDirectory\fma300.exe -ArgumentList '/S /v"/qn ALLUSERS=2"' -Wait

# Copy PhotoImages Repository
Copy-Item -Path PhotoImages -Destination 'C:\Program Files (x86)\FMAPlus 3\PhotoImages' -Recurse -Force

# Copy Active Setup Reg File
Copy-Item -Path hkcu_fmaplus3.reg -Destination 'C:\Program Files (x86)\FMAPlus 3\hkcu_fmaplus3.reg'

# Active Setup Registry Settings for License and First Run
New-Item -Path Registry::'HKLM\Software\Microsoft\Active Setup\Installed Components\HSU-FMAPlus3' -Force
New-ItemProperty -Path Registry::'HKLM\Software\Microsoft\Active Setup\Installed Components\HSU-FMAPlus3' -Name StubPath -PropertyType String -Value 'CMD /C REG IMPORT "%ProgramFiles(x86)%\FMAPlus 3\hkcu_fmaplus3.reg"' -Force
New-ItemProperty -Path Registry::'HKLM\Software\Microsoft\Active Setup\Installed Components\HSU-FMAPlus3' -Name Version -PropertyType String -Value '2,0,0,0' -Force

# Copy Fuel Model File
Copy-Item -Path MyFuelModels.FFM -Destination 'C:\Program Files (x86)\FMAPlus 3\MyFuelModels.FFM'

# Modify "C:\Program Files (x86)\FMAPlus 3" permissions
$Acl = Get-Acl 'C:\Program Files (x86)\FMAPlus 3'
$Arguments = "BUILTIN\Users","FullControl","ContainerInherit, ObjectInherit", "None", "Allow"
$accessRule = New-Object System.Security.AccessControl.FileSystemAccessRule $arguments
$Acl.SetAccessRule($accessRule)
$acl | Set-Acl 'C:\Program Files (x86)\FMAPlus 3'

# Remove Desktop Shortcut
if (Test-Path 'C:\Users\Public\Desktop\FMAPlus 3.lnk')
{
  Remove-Item -Path 'C:\Users\Public\Desktop\FMAPlus 3.lnk' -Force
}

# Rename Shortcut
Rename-Item 'C:\ProgramData\Microsoft\Windows\Start Menu\Programs\FMAPlus 3\Gateway.lnk' 'C:\ProgramData\Microsoft\Windows\Start Menu\Programs\FMAPlus 3\FMAPlus 3.lnk'

Gather up the fma300.exe installer, the hkcu_fmaplus3.reg registry file, your install script install_fma.ps1, the MyFuelModels.FFM file, and the PhotoImages folder and put them in your SCCM source location.




Create your Application and Deployment Type, the installation command is: 
powershell.exe -ExecutionPolicy Unrestricted -File install_fma.ps1



The GUID of the msi that extracted to C:\Windows\Installer when the EXE was ran is: 
{44D618AE-62C3-4564-B2EF-E00E226DE515}, so i use that for my detection method and uninstall program. YMMV if you have a different version so be sure to install it on a test computer first and see if it puts an msi in C:\Windows\Installer to get the GUID from.


Distribute and Deploy to your computers! 

No comments:

Post a Comment