####################################################################
### make_keystore.txt ###
####################################################################
Param($CertDir, $Password, $KeyStore = "", $LockedProperties = "")
# This script will create a keystore file containing all the
# certificates in $CertDir, and create they keystore with the
# given $Password. It will also generate a locked.properties file
# for use with the VMware View Connection Server.
 
# -CertDir -- The path to the directory containing all the certificates to import
# -Password -- The password to write the keystore with.
# -KeyStore -- The full path and file name of the keystore file to write
# -LockedProperties -- The full path and file name of the locked.properties file
 
function usage($commandname) {
Write-Host "Usage:"
Write-Host "$commandname -CertDir <Directory Path>"
Write-Host " -Password <Password>"
Write-Host " [-KeyStore <Keystore file path>]"
Write-Host " [-LockedProperties <locked.properties file path>]"
}
 
# Initialize Variables
$messages=@()
$errorstatus=$false
$certcount=0
 
# Add the View JRE to the path
$env:Path+=";C:\Program Files\VMware\VMware View\Server\jre\bin"
 
# Make sure the password has at least 6 characters
if ($Password.length -le 6) {
$messages+="Password must be a minimum of 6 characters long"
$errorstatus=$true
}
 
# Check the certificate directory to make sure it exists
if ($CertDir -eq $null) {
$messages+="Please provide a -CertDir and the path to your certificates folder"
$errorstatus=$true
} else {
if (-not (Test-Path -PathType Container -Path $CertDir)) {
$messages+="$CertDir does not exist"
$errorstatus=$true
}
}
 
# Check for errors and exit if found
if ($errorstatus) {
foreach ($message in $messages) {
Write-Host $message -ForegroundColor "red"
}
usage $myinvocation.mycommand.name
return
}
 
# Create a certificate store in the CertDir unless it's specified
# to go into another location
if ($KeyStore -eq "") {
$KeyStore = "$CertDir\keystore"
}
$returnstatus = $KeyStore -match "([^\\]*$)"
$KeyStoreNoPath = $Matches[1]
 
 
# Create a locked.properties file in the CertDir unless it's specified
# to go into another location
if ($LockedProperties -eq "") {
$LockedProperties = "$CertDir\locked.properties"
}
 
# Loop through all *.cer files in the certificate directory. Add each
# one to the keystore file.
foreach ($certificate in Get-ChildItem "$CertDir\*.cer") {
# Create an alias for this key
$returnstatus = $certificate -match "([^\\]*)\..*$"
$alias = $Matches[1]
trap [SystemException] {
Write-Host "$_" -ForegroundColor "red"
return;
}
$OldErrorActionPreference = $ErrorActionPreference
$ErrorActionPreference = 'SilentlyContinue'
keytool -import -alias $alias -file $certificate -keystore $KeyStore -storepass $Password -noprompt -storetype JKS
$ErrorActionPreference = $OldErrorActionPreference
$certcount += 1
}
Write-Host "Wrote $certcount certificates into the keystore file $KeyStore" -ForegroundColor "green"
 
# Create a locked.properties file properly formatted for View.
"trustKeyfile=$KeyStoreNoPath" | Out-File -Encoding "ASCII" $LockedProperties
"trustStoretype=JKS" | Out-File -Encoding "ASCII" -Append $LockedProperties
"useCertAuth=true" | Out-File -Encoding "ASCII" -Append $LockedProperties
Write-Host "Wrote $LockedProperties file" -ForegroundColor "green"
 
Write-Host -ForegroundColor Blue "Please copy the files $KeyStore and $LockedProperties files into the"
Write-Host -ForegroundColor Blue "VMware View sslgateway\conf folder, by default located in your View"
Write-Host -ForegroundColor Blue "installation directory."
####################################################################