VMware Cloud Community
NikunjB
Contributor
Contributor
Jump to solution

How to make Connect-VIServer commandlet always ask for username password

Hi All,

I am using connect-viserver commandlet to connect from windows host to my vcenter server.

using

Host machine - Powershell-2.0, Windows XP, Powercli installed.

Target Machine - Vcenter server 4.1.0 on windows 2008 server, on different subnet

I want to have a provision that, It should always ask for username and password for making a connection.

I had a look at

http://pubs.vmware.com/vsphere-50/index.jsp?topic=/com.vmware.wssdk.apiref.doc_50/vim.VirtualMachine...

http://pubs.vmware.com/vsphere-50/index.jsp?topic=/com.vmware.powercli.cmdletref.doc_50/Connect-VISe...

But couldnt get the relevant information.

Any ideas?

Thanks

Nikunj

Bangalore-India

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

My mistake, Connect-ViServer doesn't accept a SecureString. You have to convert it first

Try it like this

$userName = "username"
$passwd = Read-Host ("Password for " + $userName) -AsSecureString:$true
$cred = New-Object System.Management.Automation.PSCredential -ArgumentList $userName,$passwd
Connect-VIServer -Server MyServer -Credential $cred


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

0 Kudos
8 Replies
RvdNieuwendijk
Leadership
Leadership
Jump to solution

Hi Nikunj,

to let the Connect-VIServer cmdlet ask for the username and password you can use:

Connect-VIServer YourvCenterServer -Credential (Get-Credential)


Regards, Robert

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
NikunjB
Contributor
Contributor
Jump to solution

Hi,

Thanks for the reply !

I am actuallly using this connect-viserver inside my bigger script. and so I want to keep the console area clean.

I saw that it outputs few statements like this.

{{{

cmdlet Get-Credential at command pipeline position 1
Supply values for the following parameters:
Credential
WARNING: Index was outside the bounds of the array.

Name                           Port  User

----                           ----  ----
xx.xx.xx.xx                   443   administrator

}}}

What if I dnt want to print anything from this command on the console.?

Thanks

Nikunj

0 Kudos
RvdNieuwendijk
Leadership
Leadership
Jump to solution

I'm not exactly sure that I understand what you want. But I will tell you how I work.

The first thing I do in the morning when I start my PC at the office, is to start PowerCLI and connect to all the vCenter servers in our environment. I leave these connections open untill I go home. In my PowerCLI scripts I never have to connect to the vCenter servers, because they are already connected.

If you still want to connect to vcenter servers in your script, you could pass the credentials as parameters to your script. Something like:

param($VIServer,$UserName,$Password)

Connect-VIserver -Server $VIServer -User $UserName -Password $Password

# Rest of the script
Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
0 Kudos
LucD
Leadership
Leadership
Jump to solution

I suspect you only want to see the prompts for user and password.

That can be done as follows

$user = Read-Host "Enter the user"

$pswd = Read-Host "Enter the password" -AsSecureString

Connect-VIServer -Server $myServer -User $user -Password $pswd

That way you will only see the prompt and no other messages


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
NikunjB
Contributor
Contributor
Jump to solution

Hi,

The script ( used to deploy OVFs on ESXs ) which I am currently working on will be used by different users ( admin/nonadmin)  on different systems who may or may not have Admin privileges. the script is made in a way that It can also be run from the same vcenter server.

So we want to prompt the user for login credentials everytime.

And once the whole job is done by the script ( ~10 mins ), we dont want the connection to be active.

Let me be more clear:

10.1.1.1 ( host)      ---->     10.2.2.2 ( VCenter )

Admin/Non Admin             Admin

Before I used only "connect-viserver" cmdlet.  Now when the user is logged in as 'Admin' on the host system and tries to connect to vcenter, the connect-viserver does not asks for any credentials. ?? !!

But when the user is logged in as 'NonAdmin' on the host system and tries to connect  to vcenter, the connect-viserver do asks for credentials, by popping up the dialog box ??  !!

The command I am using is:

{{{

connect-viserver -server $ip -ea silentlycontinue -wa silentlycontinue

}}}

Ideally, it should ask for credentials all the time.? I then checked at the documentation and found that It searches the local cred store for it and if not found any matches then only it asks for user/paswd.

So,

1. We wanted to have ask for credentials everytime the script is run

2. At the same time it should keep the console clean. ( I tried with piping to Out-Null, but it didnt work. )

Thanks

Nikunj

0 Kudos
NikunjB
Contributor
Contributor
Jump to solution

LucD,

On trying your solution, I get this error ( am 100% sure about the IP, username and password correctness)

{{{

Connect-VIServer : 12/23/2011 4:34:34 AM    Connect-VIServer        Cannot complete login due to an incorrect user name or password.
At line:1 char:24
+ $ret = Connect-viServer <<<<  -Server "10.1.1.1"  -User $username -Password $password  -EA silentlycontinue  -WA silentlycontinue -EV Err
    + CategoryInfo          : NotSpecified: (:) [Connect-VIServer], InvalidLogin
    + FullyQualifiedErrorId : Client20_ConnectivityServiceImpl_Reconnect_SoapException,VMware.VimAutomation.ViCore.Cmdlets.Commands.ConnectVIServer

}}}

Any ideas.?

Thanks

Nikunj

0 Kudos
LucD
Leadership
Leadership
Jump to solution

My mistake, Connect-ViServer doesn't accept a SecureString. You have to convert it first

Try it like this

$userName = "username"
$passwd = Read-Host ("Password for " + $userName) -AsSecureString:$true
$cred = New-Object System.Management.Automation.PSCredential -ArgumentList $userName,$passwd
Connect-VIServer -Server MyServer -Credential $cred


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
NikunjB
Contributor
Contributor
Jump to solution

Luc,

Cool !! that does it ! Smiley Happy

Thanks

Nikunj

0 Kudos