VMware Cloud Community
orbion
Contributor
Contributor
Jump to solution

script to create a host profile for each Host on Vcenter

How can i do to create an automated script to create a brand new host profile for each host on the vcenter...

1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Try something like this

Get-VMHost |

ForEach-Object -Process {

    New-VMHostProfile -Name "$($_.Name)-Profile" -Description "This is my test profile based on $($_.Name))." -ReferenceHost $_ -Confirm:$false

}


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

View solution in original post

0 Kudos
13 Replies
orbion
Contributor
Contributor
Jump to solution

i did this but not working

$vmhostline = ""

$vmhost = ""

Connect-VIServer XXXXXX -User administrator@vsphere.local -Password XXXXXX

foreach ($vmhost in  $vmhostline) {

$vmhost = Get-VMHost | select name

New-VMHostProfile -Name $vmhost_"test1" -Description "This is my test profile based on $vmhost." -ReferenceHost $vmhost

}

0 Kudos
LucD
Leadership
Leadership
Jump to solution

What is not working?
Do you get an error message?
DO you have the required license to use HostProfiles?


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

0 Kudos
orbion
Contributor
Contributor
Jump to solution

it have error

xxxxxxx                443   VSPHERE.LOCAL\Administrator  

New-VMHostProfile : Cannot convert 'System.Object[]' to the type 'VMware.VimAutomation.ViCore.Types.V1.Inventory.VMHost' required by

parameter 'ReferenceHost'. Specified method is not supported.

At line:9 char:113

+ ... on "This is my test profile based on $vmhost." -ReferenceHost $vmhost

+                                                                   ~~~~~~~

    + CategoryInfo          : InvalidArgument: (:) [New-VMHostProfile], ParameterBindingException

    + FullyQualifiedErrorId : CannotConvertArgument,VMware.VimAutomation.ViCore.Cmdlets.Commands.Host.NewVMHostProfile

 

i`ve the right license----

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Try something like this

Get-VMHost |

ForEach-Object -Process {

    New-VMHostProfile -Name "$($_.Name)-Profile" -Description "This is my test profile based on $($_.Name))." -ReferenceHost $_ -Confirm:$false

}


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

0 Kudos
orbion
Contributor
Contributor
Jump to solution

Great thx sir.. u da men!!!

0 Kudos
orbion
Contributor
Contributor
Jump to solution

Sir

how can i do if i want to read the hostnames directly from a file? appreciate your help----

0 Kudos
LucD
Leadership
Leadership
Jump to solution

If you have the nodenames, one per line, in .txt file, you could do

$esxNames = Get-Content -Path '.\esxnames.txt'

Get-VMHost -Name $esxNames |

ForEach-Object -Process {

    New-VMHostProfile -Name "$($_.Name)-Profile" -Description "This is my test profile based on $($_.Name))." -ReferenceHost $_ -Confirm:$false

}


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

orbion
Contributor
Contributor
Jump to solution

sir....something it`s wrong --i got this error

Name                           Port  User

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

9.45.252.139                   443   VSPHERE.LOCAL\Administrator

Get-VMHost : 11/29/2019 5:31:44 AM      Get-VMHost              VMHost with name 'b02esx003.ciolab.ibm.com ' was not found using the

specified filter(s).

At G:\itss\hostprof2.ps1:13 char:1

+ Get-VMHost -Name $esxNames |

+ ~~~~~~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : ObjectNotFound: (:) [Get-VMHost], VimException

    + FullyQualifiedErrorId : Core_OutputHelper_WriteNotFoundError,VMware.VimAutomation.ViCore.Cmdlets.Commands.GetVMH

   ost

Get-VMHost : 11/29/2019 5:31:44 AM      Get-VMHost              VMHost with name 'b02esx004.ciolab.ibm.com ' was not found using the

specified filter(s).

At G:\itss\hostprof2.ps1:13 char:1

+ Get-VMHost -Name $esxNames |

+ ~~~~~~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : ObjectNotFound: (:) [Get-VMHost], VimException

    + FullyQualifiedErrorId : Core_OutputHelper_WriteNotFoundError,VMware.VimAutomation.ViCore.Cmdlets.Commands.GetVMH

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Are either of those values returned under the Name property when you do a Get-VMHost?


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

0 Kudos
orbion
Contributor
Contributor
Jump to solution

got this error when i run the script-below is the code...thx in avance!!!

Connect-VIServer $vcenter -User $user -Password $password -ErrorAction Stop

$esxNames = Get-Content -Path '.\esxnames.txt'

Get-VMHost -Name $esxNames |

ForEach-Object -Process {

    New-VMHostProfile -Name "$($_.Name)-Profile2" -Description "This is my test profile based on $($_.Name))." -ReferenceHost $_ -Confirm:$false

}

Disconnect-VIServer $vcenter

0 Kudos
LucD
Leadership
Leadership
Jump to solution

When you do a Get-VMHost (no parameters and after a Connect-VIServer), does the Name column show the same names that you have in your .txt file?

Could there be an extra blank at the end of a line?


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

0 Kudos
orbion
Contributor
Contributor
Jump to solution

it seems a blank cause the error...it`s working now...thx a lot sir!!!!

0 Kudos
LucD
Leadership
Leadership
Jump to solution

You're welcome :smileygrin:


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

0 Kudos