VMware Cloud Community
medalklector
Contributor
Contributor
Jump to solution

Using get-vicredentialstoreitem

Hi,

Once again, my knowledge has been shown to be lacking.....

I have created a credential store which contains the local root passwords for a number of hosts. I can retrieve the credentials and connect to each host via CLI with no problems. I would like to do the same thing in a script, but it appears I'm doing something incorrectly..As always, help would greatly appreciated.

Here's the section that deals with connections & credentials

$hostESXlist = Get-VMHost | where {$_.ConnectionState -ne "Disconnected"} | where {($_ | get-view).Config.Product.ProductLineId -eq "embeddedESX"} | sort-object -Property Name

foreach ($row in $hostESXlist){

$Creds = Get-VICredentialStoreItem -File c:\scripts\credentials.xml

    $csvname = $customer + "_" + $row.Name + "_" + $date + ".mef3"

    Remove-Item -Path $csvpath\$csvname -Force -Confirm:$false

    if (!($vi.name -eq $row.name)) {

        Write-Host "Please provide logon credentials for" $row.Name -foregroundcolor "white"

        $login = Connect-VIServer -Server $row.Name -User root -Password $Creds

         if (!$?) {

            Write-Host -ForegroundColor Red "Could not create MEF file for $($row.name)."

            Write-Host -ForegroundColor Red "   $($Error[0])"

            continue

        }

    }

Here's the error:

Cannot convert 'System.Object[]' to the type 'System.String' required by parameter 'Password'. Specified method is not supported.
At :line:53 char:66
+         $login = Connect-VIServer -Server $row.Name -User root -Password <<<<  $Creds

Cheers

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

I suspect you will have to use the Credential parameter instead of the User and Password parameters.

$login = Connect-VIServer -Server $row.Name -Credential $Creds



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

View solution in original post

Reply
0 Kudos
6 Replies
LucD
Leadership
Leadership
Jump to solution

I suspect you will have to use the Credential parameter instead of the User and Password parameters.

$login = Connect-VIServer -Server $row.Name -Credential $Creds



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

Reply
0 Kudos
medalklector
Contributor
Contributor
Jump to solution

Thanks Luc...Couldn't see the forest for the trees!

Cheers

Reply
0 Kudos
medalklector
Contributor
Contributor
Jump to solution

I'm stil having issues with this. I suspect it's my syntax for the get-vicredentialstoreitem but I've tried a number of iterations with the same result.

The changed code and error are shown below. I'd appreciate it if someone could look it over.

$hostESXlist = Get-VMHost | where {$_.ConnectionState -ne "Disconnected"} | where {($_ | get-view).Config.Product.ProductLineId -eq "embeddedESX"} | sort-object -Property Name


foreach ($row in $hostESXlist){

$vi
$Creds = Get-VICredentialStoreItem -user root -File c:\scripts\credentials.xml
    $csvname = $customer + "_" + $row.Name + "_" + $date + ".mef3"
    Remove-Item -Path $csvpath\$csvname -Force -Confirm:$false
    if (!($vi.name -eq $row.name)) {
        Write-Host "Please provide logon credentials for" $row.Name -foregroundcolor "white"
       $login = Connect-VIServer -Server $row.Name -Credential $Creds
        if (!$?) {
            Write-Host -ForegroundColor Red "Could not create MEF file for $($row.name)."
            Write-Host -ForegroundColor Red "   $($Error[0])"
            continue
        }
    }


Error:


Could not create MEF file for server1.
   Cannot convert 'System.Object[]' to the type 'System.Management.Automation.PSCredential' required by parameter 'Credential'. Specified method is not supported.

It appears that the $Creds variable is not being passed?

Thanks once again.

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Try it like this

$hostESXlist = Get-VMHost | where {$_.ConnectionState -ne "Disconnected"} | 
where {($_ | get-view).Config.Product.ProductLineId -eq "embeddedESX"} | 
sort-object -Property Name
foreach ($row in $hostESXlist){
    $Creds = Get-VICredentialStoreItem -user root -File c:\scripts\credentials.xml
    $csvname = $customer + "_" + $row.Name + "_" + $date + ".mef3"
    Remove-Item -Path $csvpath\$csvname -Force -Confirm:$false
   
if (!($vi.name -eq $row.name)) {         Write-Host "Please provide logon credentials for" $row.Name -foregroundcolor "white"
       $login = Connect-VIServer -Server $row.Name -User $Creds.User -Password $Creds.Password         if (!$?) {             Write-Host -ForegroundColor Red "Could not create MEF file for $($row.name)."
           
Write-Host -ForegroundColor Red "   $($Error[0])"
            continue
        }     }

}

I was wrong in my previous answer, you have to use the User and Password properties of the VICredentialStoreItem object.


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

Reply
0 Kudos
medalklector
Contributor
Contributor
Jump to solution

Thanks Luc. As well as your additions I changed the following line to include -host ;

$Creds = Get-ViCredentialStoreItem -host $row.Name - file c:\scripts\credentials.xmls

It works well now.


Thanks againg for your help.

Reply
0 Kudos
Nawals
Expert
Expert
Jump to solution

Hello,

Below are the my script created for migrate hosts from old vcenter to new vcenter. From old vcenter disconnected and removed successfully. however, while connecting to new vcenter to add hosts getting problem. Can you please help me here to fix it.

    <#

.SYNOPSIS

        EA  Host Migration for a Cluster

    .DESCRIPTION

        EA  Host Migration for a Cluster

    .NOTES

        Author: Nawal Singh

    .PARAMETER

        ESX(i) Host Configuration

    .EXAMPLE

     

.EXAMPLE

    #>

#Connection to vCenter

$mycred = Get-Credential

Connect-VIServer "VCS001.local" -Credential $mycred

##ESX host migration

#Switch off HA/DRS

#Get-Cluster "CL-01"  | Set-Cluster -HAEnabled:$false -DrsEnabled:$false -Confirm:$false

#Remove ESX hosts from old vcenter

Get-Cluster "CL-01" | Get-VMHost "vsphere1.local" | Set-VMHost -State "Disconnected" -Confirm:$false

Get-VMHost "vsphere1.local" | Remove-VMHost -Confirm:$false

Get-Cluster "CL-01" | Get-VMHost "vsphere2.local" | Set-VMHost -State "Disconnected" -Confirm:$false

Get-VMHost "vsphere2.local" | Remove-VMHost -Confirm:$false

Disconnect-VIServer -Server *  -Force -Confirm:$false

Connect-VIServer "VCS002.local" -Credential $mycred

#add ESX hosts into new vcenter

Add-VMHost "vsphere1.local"  -Location (Get-Cluster "CL-02" ) -user root -Password pass -Force

Add-VMHost "vsphere2.local"  -Location (Get-Cluster "CL-02" ) -user root -Password pass -Force

#Turn on HA and DRS on

#Set-Cluster  CL-02 -DrsEnabled:$true -HAEnabled:$true -Confirm:$false

Disconnect-VIServer -Server *  -Force -Confirm:$false

=======================================================================================================

Below are the error..

Add-VMHost : Cannot convert 'System.Object[]' to the type 'System.String'

required by parameter 'Password'. Specified method is not supported.

At E:\abc\PowerCLI Scripts\MigrateHosts.ps1:57 char:106

+ ... root -Password pass -Force

+                    ~~~~~~~~

    + CategoryInfo          : InvalidArgument: (:) [Add-VMHost], ParameterBind

   ingException

    + FullyQualifiedErrorId : CannotConvertArgument,VMware.VimAutomation.ViCor

   e.Cmdlets.Commands.AddVMHost

NKS Please Mark Helpful/correct if my answer resolve your query.
Reply
0 Kudos