How to create and use CredentialStoreItem

Version 2

    $VIServer = "myserver"
    $user = "mydomain\myuser"
    $password = "**********"
    $FilePath = "c:\users\myuser\desktop\mycred.xml"

     

    #Creates a new file with your credentials
    New-VICredentialStoreItem -Host $VIServer -User $user -Password $password -File $FilePath

     

    #Get your credentials from file and keeps on a new variable
    $cred = Get-VICredentialStoreItem -Host $VIServer -File $FilePath

     

    #Connects using the VICredentialStoreItem
    Connect-VIServer -User $cred.User -Password $cred.Password -Server $VIServer

     

    #To remove the credential File
    Remove-Item -Path $FilePath -Force