I'm working on a script that will perform some ESX host configurations based on a CSV file to speed up implementation on blade systems and running into a slight roadblock. I'm getting an error stating:
Cannot bind parameter 'Server'. Cannot convert value "GPIVM08" to type "VMware.VimAutomation.Types.VIServer". Error: "Invalid cast from 'System.String' to 'VMware.VimAutomation.Types.VIServer'."
I know WHAT the issue is but not sure how to import the string for hostname as a VIServer object. Here is the code segment I'm trying to run.
$hostserver = Import-Csv "
server\install\Josh\Deploy\hostadd.csv"
$GroupAccount = "VIAdmin"
$hostserver | % {
Connect-VIServer -Server $_.Name -User $_.loginname -Password $_.loginpass
New-VMHostAccount -Server $_.Name -Id $GroupAccount -GroupAccount
}
Consider that i have a .csv with columns for Name, loginname, and loginpass. I have no issue connecting to the VIserver and get an error when trying to create the new host account.
Any help is greatly appreciated.
The VMware.VimAutomation.Types.VIServer object is returned by a Get-VmHost cmdlet.
You can pipe the result to your New-VMHostAccount cmdlet
$hostserver = Import-Csv "\\server\install\Josh\Deploy\hostadd.csv" $GroupAccount = "VIAdmin" $hostserver | % { Connect-VIServer -Server $_.Name -User $_.loginname -Password $_.loginpass Get-VmHost -Name $_.Name | New-VMHostAccount -Id $GroupAccount -GroupAccount }
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
The VMware.VimAutomation.Types.VIServer object is returned by a Get-VmHost cmdlet.
You can pipe the result to your New-VMHostAccount cmdlet
$hostserver = Import-Csv "\\server\install\Josh\Deploy\hostadd.csv" $GroupAccount = "VIAdmin" $hostserver | % { Connect-VIServer -Server $_.Name -User $_.loginname -Password $_.loginpass Get-VmHost -Name $_.Name | New-VMHostAccount -Id $GroupAccount -GroupAccount }
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thank you LucD. I knew I was close. My immediate thought was to perform the following:
$hostserver | $ {
$currentserver = Get-VMhost -Server $_.name
$ New-VMHostAccount -Server $currentserver -Id $GroupAccount -GroupAccount
}
It appears that my problem came down to order of operations where it was necessary to first establish the object and then perform an action on the object. It amazes me how the obvious things are sometimes hard to find. Once this script is complete I will certainly be posting for the community as I think they may find it helpful.
Thank you again for your quick (and correct!) response.
Your version would have worked as well (with a few minor adaptions).
$hostserver | % { $currentserver = Get-VMhost -Name $_.name New-VMHostAccount -Server $currentserver -Id $GroupAccount -GroupAccount }
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Not sure why I didn't think to use -Name instead of -Server (the $ was a typo where I actually used %)
Thanks again.
Unfortunately now I'm getting a different error when using:
Get-VmHost -Name $_.Name | New-VMHostAccount -Id $GroupAccount -GroupAccount
+The input object cannot be bound to any parameters for the command either because the command does not take pipeline input or the input and its properties do not match any of the parameters that take pipeline input. + Get-VmHost -Name $_.Name | New-VMHostAccount <<<< -Id VIAdmin -GroupAccount+
It appears that the Server can be piped in but i'm not sure what the (ByValue) is or what other parameter it may referring to.
My mistake, should have seen that.
For the New-VMHostAccount cmdlet you should be connected to an ESX server, not the vCenter.
The -Server parameter asks for a ViServerImpl object.
That's the object that is returned by a Connect-ViServer cmdlet not the Get-VmHost cmdlet (which returns a VmHostImpl object).
The script should be
$ESXname = <ESX-hostname> $server = Connect-VIServer -Server $ESXname -User <account> -Password <password> New-VMHostAccount -Server $server -Id $GroupAccount -GroupAccount
And you can even leave out the -Server parameter completely, since the cmdlet will take the content of the $DefaultVIServer variable if the parameter is not present.
And the $DefaultVIServer variable is populated by the Connect-ViServer cmdlet.
Sorry for the confusion.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thanks again for the assist. Here is my completed script. Nothing fancy but will prove to be invaluable for me as I reconfigure a large number of servers. I had considered adding a check to see if the current connected server matched the value listed on the next object but the connections processed so quickly I didn't see a point. I can add if others find it useful. I did add a check to see if the group existed since this is intended for new installs and I may want to create and assign multiple users to the same group. Attached example .csv and .ps1
#This script allows you to add users with SSH access to a specified group through based
#on data from a populated CSV file. the only components that need to be filled out prior
#to running is the location of the .CSV file. A final line is available to remove SSH
#access for 'root' but this has not worked thus far in my testing.#Thanks to LucD for his assistance
$hostserver = Import-Csv "
server\share\hostadd.csv"
$hostserver | % {
$server = Connect-VIServer -Server $_.Name -User $_.loginname -Password $_.loginpass
If (!(Get-VMHostAccount -Id $_.Group -Group)){
New-VMHostAccount -Id $_.Group -GroupAccount
}
New-VMhostAccount -Id $_.newuser -Password $_.newpass -Description $_.description -AssignGroups $_.Group -GrantShellAccess
#Set-VMHostAccount -UserAccount "root" -GrantShellAccess $False
}