I’ve recently watched the Powershell jumpstart series and I’m getting dangerous. I have a fairly simple script but I have a feeling there’s an easier (more elegant?) way to do this.
My script creates new deployments in a vmware environment and I want a nicely formatted log in a text file for now. The purpose is so the admin running it can see exactly what was done and also for auditing purpose.
My question is putting the out-file $filename -append and the end of just about every line seems a little crazy. Is Start-Transcript a better option? I did see a forum post about Start-Transcript but it didnt to quite apply to my situation.
Param( [Parameter(Mandatory=$True,Position=1)] [string]$ClientName, [Parameter(Mandatory=$True)] [string]$vlanID ) $cluster = 'Cluster' $RootVMFolder = 'Client Folder' $vSwitch = 'dvswitch' $numports = 8 $LogFileRoot= "c:\scripts\logs\" $logfile =($LogFileRoot + $clientname + "-" + (Get-Date -f MMddyyyy-hhmm) +".log") Write-Output ("Script Executed by " + $env:username) | out-file $logfile -append #Loading credentials for logging into vCenter $creds = Get-VICredentialStoreItem -file pshell.creds Write-Output "Logging into vCenter...." | out-file $logfile -append Connect-VIServer -server $creds.host -user $creds.user -password $creds.password | out-file $logfile -append Write-Output "Creating New Folder for VMs" | out-file $logfile -append new-folder -Name $clientName -Location $RootVMFolder | select Name,Parent | out-file $logfile -append Write-Output "Creating New Resource Pool" | out-file $logfile -append new-resourcepool -Location $cluster -name $clientName | select Name | out-file $logfile -append Write-Output ("Creating New PortGroup called " + "VPN-" + $ClientName + " with vlanID " + $vlanid) | out-file $logfile -append Get-VDSwitch -name "dvSwitch" | New-VDPortgroup -Name VPN-$ClientName -numports $numports -vlanid $vlanId | out-file $logfile -append