VMware Cloud Community
as900w
Hot Shot
Hot Shot
Jump to solution

How to store user name and password in a script

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
Jump to solution

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
Jump to solution

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
scott28tt
VMware Employee
VMware Employee
Jump to solution

Moderator: Moved to PowerCLI


-------------------------------------------------------------------------------------------------------------------------------------------------------------

Although I am a VMware employee I contribute to VMware Communities voluntarily (ie. not in any official capacity)
VMware Training & Certification blog
Reply
0 Kudos
as900w
Hot Shot
Hot Shot
Jump to solution

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.

Reply
0 Kudos
RoderikdeBlock
Enthusiast
Enthusiast
Jump to solution

You have to provide the full path of the location

Roderik de Block


Blog: https://roderikdeblock.com
Reply
0 Kudos
as900w
Hot Shot
Hot Shot
Jump to solution

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