as900w
Hot Shot
Hot Shot

How to store user name and password in a script

Jump to solution

I will create a script to connection to vSphere, and the script will sent to user to use.

I don't want to show password in the script.

What should I do?

1 Solution

Accepted Solutions
RoderikdeBlock
Enthusiast
Enthusiast

You can use the VICredentialStore:

New-VICredentialStoreItem -User <user> -Password <user> -Host <server> -File C:\<your location.xml>

$user =Get-VICredentialStoreItem -File C:\<your location.xml> -host <server>

Connect-VIServer -Server <server> -user $user.User -password $user.Password

OR:

$Cred = Get-Credential

Connect-VIServer <your_server> -Credential $Cred

Roderik de Block Blog: https://roderikdeblock.com

View solution in original post

5 Replies
RoderikdeBlock
Enthusiast
Enthusiast

You can use the VICredentialStore:

New-VICredentialStoreItem -User <user> -Password <user> -Host <server> -File C:\<your location.xml>

$user =Get-VICredentialStoreItem -File C:\<your location.xml> -host <server>

Connect-VIServer -Server <server> -user $user.User -password $user.Password

OR:

$Cred = Get-Credential

Connect-VIServer <your_server> -Credential $Cred

Roderik de Block Blog: https://roderikdeblock.com

View solution in original post

scott28tt
VMware Employee
VMware Employee

Moderator: Moved to PowerCLI

0 Kudos
as900w
Hot Shot
Hot Shot

I want to will the user and password file store with the script.

so, i changed the script:

$user =Get-VICredentialStoreItem -File user_and_password.xml -host <server>

Connect-VIServer -Server <server> -user $user.User -password $user.Password

but, it's failed.

Get-VICredentialStoreItem : Cannot bind parameter 'File' to the target. Exception setting "File": "Credentials file doesn't exist."

if i use "$user =Get-VICredentialStoreItem -File C:\<your location.xml> -host <server>", that is OK.

0 Kudos
RoderikdeBlock
Enthusiast
Enthusiast

You have to provide the full path of the location

Roderik de Block Blog: https://roderikdeblock.com
0 Kudos
as900w
Hot Shot
Hot Shot

Thanks!

I have solved this problem.

I have use $PSScriptRoot arguments

$user =Get-VICredentialStoreItem -File $PSScriptRoot\vCenterPassword.xml -host corevcsvr01.sh.dto

Connect-VIServer -Server corevcsvr01.sh.dto -user $user.User -password $user.Password