PowerShell Code

I use this code to create a log file during a VM build using vRealize Automation

Here’s an example:


$VMname = 'ServerName'

#The next line defines where to save the log file
$LOGFILENAME = "C:\vRA-Install-Logs\vRA-Install-Log-" + $VMname + ".log"

#Check to see if file already exists. If it does delete it.
if (Test-Path $LOGFILENAME) {
Remove-Item -Path $LOGFILENAME
}

#Creates the Log File
New-Item $LOGFILENAME -ItemType File

#Update Log File. I get the Date/Time and include in each step.
$LOGDATETIME = Get-Date -Format G
$LOG = $LOGDATETIME + ' - The vRA Server Install Started...'
Add-Content $LOGFILENAME $LOG

#You can just keep repeating the the next 3 lines whenever you complete new step in
#the VM build process.
$LOGDATETIME = Get-Date -Format G
$LOG = $LOGDATETIME + ' - The Templete Clone process completed...'
Add-Content $LOGFILENAME $LOG

Sample Output:


7/18/2019 7:56:23 AM - The vRA Server Install Started...
7/18/2019 7:56:23 AM - AD Computer Object: VM-Name already exists in: OU=Systems,DC=Domain-Name,DC=org.
7/18/2019 7:56:23 AM - AD Group Object: svr-VM-Name already exists in: OU=Groups,DC=Domain-Name,DC=org.
7/18/2019 7:56:24 AM - Email sent...
7/18/2019 7:56:24 AM - VM install started. The Windows Server Template is now being cloned...
7/18/2019 8:04:51 AM - Template Cloned and Sysprep Completed...
7/18/2019 8:04:55 AM - Connected to vCenter to Create VM Drives...
7/18/2019 8:05:13 AM - Added Tag "TAG-vRA-VM" to VM...
7/18/2019 8:05:18 AM - E: Drive size received - 100GB...