<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: LOG succesfull connectivity in VMware PowerCLI Discussions</title>
    <link>https://communities.vmware.com/t5/VMware-PowerCLI-Discussions/LOG-succesfull-connectivity/m-p/2978772#M112761</link>
    <description>&lt;P&gt;&lt;a href="https://communities.vmware.com/t5/user/viewprofilepage/user-id/256147"&gt;@LucD&lt;/a&gt; thank you for your advice&lt;/P&gt;&lt;P&gt;I made the script like this, unfortunately, a successful connection is not logged and a failed one also&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="cpp"&gt;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
 }&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 24 Jul 2023 09:58:16 GMT</pubDate>
    <dc:creator>lElOUCHE_79</dc:creator>
    <dc:date>2023-07-24T09:58:16Z</dc:date>
    <item>
      <title>LOG succesfull connectivity</title>
      <link>https://communities.vmware.com/t5/VMware-PowerCLI-Discussions/LOG-succesfull-connectivity/m-p/2978726#M112759</link>
      <description>&lt;P&gt;Connecting to VCenter using the following script, my plan is to log connectivity to a LOG file for auditing purposes.&lt;BR /&gt;I can log connectivity that failed, but I can't log one that succeeded.&lt;/P&gt;&lt;P&gt;Can someone assist me, please?&lt;/P&gt;&lt;LI-CODE lang="c"&gt;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"
    
}
    }&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 23 Jul 2023 16:36:37 GMT</pubDate>
      <guid>https://communities.vmware.com/t5/VMware-PowerCLI-Discussions/LOG-succesfull-connectivity/m-p/2978726#M112759</guid>
      <dc:creator>lElOUCHE_79</dc:creator>
      <dc:date>2023-07-23T16:36:37Z</dc:date>
    </item>
    <item>
      <title>Re: LOG succesfull connectivity</title>
      <link>https://communities.vmware.com/t5/VMware-PowerCLI-Discussions/LOG-succesfull-connectivity/m-p/2978728#M112760</link>
      <description>&lt;P&gt;Imho a better scenario for this intended behavior would be to use a Try-Catch construct.&lt;BR /&gt;&lt;BR /&gt;Also note that the Write-Information cmdlet needs an InformationAction of Continue, since the default (in $InformationActionPreference is SilentlyContinue)&lt;/P&gt;
&lt;LI-CODE lang="csharp"&gt;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
}
&lt;/LI-CODE&gt;</description>
      <pubDate>Sun, 23 Jul 2023 18:23:13 GMT</pubDate>
      <guid>https://communities.vmware.com/t5/VMware-PowerCLI-Discussions/LOG-succesfull-connectivity/m-p/2978728#M112760</guid>
      <dc:creator>LucD</dc:creator>
      <dc:date>2023-07-23T18:23:13Z</dc:date>
    </item>
    <item>
      <title>Re: LOG succesfull connectivity</title>
      <link>https://communities.vmware.com/t5/VMware-PowerCLI-Discussions/LOG-succesfull-connectivity/m-p/2978772#M112761</link>
      <description>&lt;P&gt;&lt;a href="https://communities.vmware.com/t5/user/viewprofilepage/user-id/256147"&gt;@LucD&lt;/a&gt; thank you for your advice&lt;/P&gt;&lt;P&gt;I made the script like this, unfortunately, a successful connection is not logged and a failed one also&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="cpp"&gt;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
 }&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 24 Jul 2023 09:58:16 GMT</pubDate>
      <guid>https://communities.vmware.com/t5/VMware-PowerCLI-Discussions/LOG-succesfull-connectivity/m-p/2978772#M112761</guid>
      <dc:creator>lElOUCHE_79</dc:creator>
      <dc:date>2023-07-24T09:58:16Z</dc:date>
    </item>
    <item>
      <title>Re: LOG succesfull connectivity</title>
      <link>https://communities.vmware.com/t5/VMware-PowerCLI-Discussions/LOG-succesfull-connectivity/m-p/2978781#M112762</link>
      <description>&lt;P&gt;The Write-Information and Write-Warning go to the console, they are not saved in a file.&lt;BR /&gt;Then you would need to use for example an Out-File, as you did in the last line&lt;/P&gt;</description>
      <pubDate>Mon, 24 Jul 2023 11:42:35 GMT</pubDate>
      <guid>https://communities.vmware.com/t5/VMware-PowerCLI-Discussions/LOG-succesfull-connectivity/m-p/2978781#M112762</guid>
      <dc:creator>LucD</dc:creator>
      <dc:date>2023-07-24T11:42:35Z</dc:date>
    </item>
    <item>
      <title>Re: LOG succesfull connectivity</title>
      <link>https://communities.vmware.com/t5/VMware-PowerCLI-Discussions/LOG-succesfull-connectivity/m-p/2978785#M112763</link>
      <description>&lt;P&gt;Ok I will check for successful connection, but why the failed one doesn't create the file and log the output?&lt;/P&gt;&lt;P&gt;Just for info, the script should create the folder if doesn't exist and the output, CSV-log should go to their respective directory.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 24 Jul 2023 11:50:13 GMT</pubDate>
      <guid>https://communities.vmware.com/t5/VMware-PowerCLI-Discussions/LOG-succesfull-connectivity/m-p/2978785#M112763</guid>
      <dc:creator>lElOUCHE_79</dc:creator>
      <dc:date>2023-07-24T11:50:13Z</dc:date>
    </item>
    <item>
      <title>Re: LOG succesfull connectivity</title>
      <link>https://communities.vmware.com/t5/VMware-PowerCLI-Discussions/LOG-succesfull-connectivity/m-p/2978837#M112765</link>
      <description>&lt;P&gt;This works for me.&lt;/P&gt;
&lt;LI-CODE lang="csharp"&gt;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
}
&lt;/LI-CODE&gt;</description>
      <pubDate>Mon, 24 Jul 2023 14:13:45 GMT</pubDate>
      <guid>https://communities.vmware.com/t5/VMware-PowerCLI-Discussions/LOG-succesfull-connectivity/m-p/2978837#M112765</guid>
      <dc:creator>LucD</dc:creator>
      <dc:date>2023-07-24T14:13:45Z</dc:date>
    </item>
    <item>
      <title>Re: LOG succesfull connectivity</title>
      <link>https://communities.vmware.com/t5/VMware-PowerCLI-Discussions/LOG-succesfull-connectivity/m-p/2979187#M112772</link>
      <description>&lt;P&gt;Thank you &lt;a href="https://communities.vmware.com/t5/user/viewprofilepage/user-id/256147"&gt;@LucD&lt;/a&gt;&lt;/P&gt;&lt;P&gt;YYou point me to elaborate more the solution that you shared with me &lt;img class="lia-deferred-image lia-image-emoji" src="https://communities.vmware.com/html/@69B3F769431606C22F8503450B1B2022/emoticons/1f60a.png" alt=":smiling_face_with_smiling_eyes:" title=":smiling_face_with_smiling_eyes:" /&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 26 Jul 2023 06:27:45 GMT</pubDate>
      <guid>https://communities.vmware.com/t5/VMware-PowerCLI-Discussions/LOG-succesfull-connectivity/m-p/2979187#M112772</guid>
      <dc:creator>lElOUCHE_79</dc:creator>
      <dc:date>2023-07-26T06:27:45Z</dc:date>
    </item>
  </channel>
</rss>

