(614) 259-7038 info@thecrmsense.com
Select Page

Template PowerShell Script:

I recommend using the Windows PowerShell ISE to tune and test your script.

# salesforce login

$u = “emailAddress@whatever.com”

 

# salesforce password

$p = “Salesforce password”

 

# salesforce security token

$t = “Salesforce security token”

 

# folder where backups should be stored

$f = “C:\Backups\Backup Folder Name”

 

 

# following configure sending status emails.

# the script assumes outside email – may be different for local mail server

 

# server name ex: smtp.gmail.com

$SMTPServer = “smtp.gmail.com”

 

# login id for smtp server

$SMTPlogin=”emailAddress@whatever.com”

 

# password for smtp server

$SMTPpass=”email password”

 

# following are where the two software tools are installed on your system

 

# path to FuseIT software

$appPath = “C:\Program Files (x86)\FuseIT SFDC Explorer\FuseIT.SFDC.DataExportConsole.exe”

 

# path to Force.com CLI tool

$forcePath = “C:\Program Files\Force CLI\force.exe”

 

 

# ——————————————————

# Configuration is above; execution below.

# ——————————————————

 

$dir = $f + “\Backup ” + (get-date).ToString(“yyyy-MM-dd”)

$metaDir = $dir + “\temp”

$zipFile = $dir + “\Metadata”

$errMsg = $null

 

New-Item “$dir” -ItemType Directory -Force

 

## — Following downloads the backup from Salesforce.

& $appPath  /u:$u /p:$p /t:$t /e:Production $dir

if ($lastExitCode -eq 6) { $errMsg += “Downloader:  Error Logging In`n” }

if ($lastExitCode -eq 8) { $errMsg += “Downloader:  Backup Not Available`n” }

 

## — Following downloads the metadata and zips it.

& $forcePath login -u=”$u” -p=”$($p)$($t)”

 

if ($lastExitCode -ne $null -and $lastExitCode -ne 0) { $errMsg += “Force.com CLI:  Error Logging In” }

else {

# Following creates a zip from the downloaded metadata files

& $forcePath export “$metaDir”

& $forcePath fetch -t Aura -d “$metaDir”

& $forcePath logout -u=”$u”

Compress-Archive -Path $metaDir -DestinationPath $zipFile -Force

Remove-Item -LiteralPath $metaDir -Force -Recurse

}

 

if ($errMsg -eq $null -or $errMsg -eq “”) { $errMsg = “Successful backup”}

 

#– Send an email notifying of the job”s final status

if ($errMsg -ne $null -and $errMsg -ne “”) {

$EmailTo = $u

$EmailFrom = $u

$Subject = “SFDC Backup Report: “+ $f

 

$Body = “`n`n” + $u

$Body += “`n ” + $f

$Body += “`n ” + $errMsg

 

$SMTPMessage = New-Object     System.Net.Mail.MailMessage($EmailFrom,$EmailTo,$Subject,$Body)

$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587)

$SMTPClient.EnableSsl = $true

$SMTPClient.Credentials = New-Object System.Net.NetworkCredential($SMTPlogin,     $SMTPpass);

$SMTPClient.Send($SMTPMessage)

}

Social Share Buttons and Icons powered by Ultimatelysocial