Connecting to VCenter using the following script, my plan is to log connectivity to a LOG file for auditing purposes.
I can log connectivity that failed, but I can't log one that succeeded.
Can someone assist me, please?
connect-VIServer $VCServer -ErrorAction SilentlyContinue -ErrorVariable ErrorProcess;
if($ErrorProcess){
Write-Warning "Error connecting to vCenter Server $VCServer error message below"
Write-Warning $Error[0].Exception.Message
$Error[0].Exception.Message | Out-File $LOGOutput\ConnectionError.txt
exit
else {
Write-Information "Connected to vCenter Server $VCServer succesfully"
}
}
This works for me.
function CreateDirectory {
[CmdletBinding()]
param(
[Parameter(Mandatory = $true)]
[String]$Destination
)
if (!(Test-Path -Path $Destination)) {
Write-Host "Folder $Destination not Found...Creating it."
New-Item -Name $DirectoryOutput -ItemType Directory
}
if (!(Test-Path -Path "$Destination\CSV")) {
Write-Host "Folder $CSVOutput Not Found...Creating it."
New-Item -Name 'CSV' -Path $Destination -ItemType Directory
}
if (!(Test-Path -Path "$Destination\LOG")) {
Write-Host "$LOGOutput Not Found...Creating it."
New-Item -Name 'LOG' -Path $Destination -ItemType Directory
}
}
$VCServer = 'MyvCenter'
$folder = 'C:\Folder'
$logFile = "$folder\LOG\$VCServer.log"
$csvFile = "$folder\CSV\$VCServer.csv"
CreateDirectory -Destination $folder | Out-Null
Write-Host "Connecting to vCenter $VCServer"
Write-Output -InputObject "Connecting to vCenter Server $VCServer" | Out-File -FilePath $logFile
try {
Connect-VIServer -Server $VCServer -ErrorAction Stop
Write-Host "Connected"
Write-Output -InputObject "Connected to vCenter Server $VCServer successfully" | Out-File -FilePath $logFile
}
catch {
Write-Host "Not connected"
Write-Output "Error connecting to vCenter Server $VCServer error message below" | Out-File -FilePath $logFile
$Error[0].Exception.Message | Out-File -FilePath $logFile
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Imho a better scenario for this intended behavior would be to use a Try-Catch construct.
Also note that the Write-Information cmdlet needs an InformationAction of Continue, since the default (in $InformationActionPreference is SilentlyContinue)
try{
Connect-VIServer -Server $VCServer -ErrorAction Stop
Write-Information "Connected to vCenter Server $VCServer successfully" -InformationAction Continue
}
catch{
Write-Warning "Error connecting to vCenter Server $VCServer error message below"
Write-Warning $Error[0].Exception.Message
$Error[0].Exception.Message | Out-File $LOGOutput\ConnectionError.txt
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
@LucD thank you for your advice
I made the script like this, unfortunately, a successful connection is not logged and a failed one also
function CreateDirectory {
$DirectoryOutput = $Client
$CSVOutput = "$Client\CSV"
$LOGOutput = "$Client\LOG"
if (!(Test-Path -path $DirectoryOutput)) {
Write-Host $DirectoryOutput "Not Found...Creating it."
New-Item $DirectoryOutput -ItemType Directory
}
if (!(Test-Path -Path $CSVOutput)) {
Write-Host $CSVOutput "Not Found...Creating it."
New-Item $CSVOutput -ItemType Directory
}
if (!(Test-Path -Path $LOGOutput)) {
Write-Host $LOGOutput "Not Found...Creating it."
New-Item $LOGOutput -ItemType Directory
}
}
CreateDirectory | Out-Null
Import-Module -Name VMware.VimAutomation.Core
try{
Connect-VIServer -Server $VCServer -ErrorAction Stop
Write-Information "Connected to vCenter Server $VCServer successfully" -InformationAction Continue
}
catch{
Write-Warning "Error connecting to vCenter Server $VCServer error message below"
Write-Warning $Error[0].Exception.Message
$Error[0].Exception.Message | Out-File $LOGOutput\ConnectionError.txt
}
The Write-Information and Write-Warning go to the console, they are not saved in a file.
Then you would need to use for example an Out-File, as you did in the last line
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Ok I will check for successful connection, but why the failed one doesn't create the file and log the output?
Just for info, the script should create the folder if doesn't exist and the output, CSV-log should go to their respective directory.
This works for me.
function CreateDirectory {
[CmdletBinding()]
param(
[Parameter(Mandatory = $true)]
[String]$Destination
)
if (!(Test-Path -Path $Destination)) {
Write-Host "Folder $Destination not Found...Creating it."
New-Item -Name $DirectoryOutput -ItemType Directory
}
if (!(Test-Path -Path "$Destination\CSV")) {
Write-Host "Folder $CSVOutput Not Found...Creating it."
New-Item -Name 'CSV' -Path $Destination -ItemType Directory
}
if (!(Test-Path -Path "$Destination\LOG")) {
Write-Host "$LOGOutput Not Found...Creating it."
New-Item -Name 'LOG' -Path $Destination -ItemType Directory
}
}
$VCServer = 'MyvCenter'
$folder = 'C:\Folder'
$logFile = "$folder\LOG\$VCServer.log"
$csvFile = "$folder\CSV\$VCServer.csv"
CreateDirectory -Destination $folder | Out-Null
Write-Host "Connecting to vCenter $VCServer"
Write-Output -InputObject "Connecting to vCenter Server $VCServer" | Out-File -FilePath $logFile
try {
Connect-VIServer -Server $VCServer -ErrorAction Stop
Write-Host "Connected"
Write-Output -InputObject "Connected to vCenter Server $VCServer successfully" | Out-File -FilePath $logFile
}
catch {
Write-Host "Not connected"
Write-Output "Error connecting to vCenter Server $VCServer error message below" | Out-File -FilePath $logFile
$Error[0].Exception.Message | Out-File -FilePath $logFile
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thank you @LucD
YYou point me to elaborate more the solution that you shared with me ![]()
