VMware Cloud Community
dbutch1976
Hot Shot
Hot Shot
Jump to solution

Apply a host profile to multiple hosts from csv

I'm really close on this one I think.  The contents of my hostprofile.csv are:

"NAME,STATE,STATUS,% CPU,% MEMORY,LAST TIME EXITED STANDBY,ALARM ACTIONS,AUTODEPLOY.MACHINEIDENTITY,CREATED BY,FUNCTION,NOTIFICATION DL,OWNER/REQUESTER"

"esxdc2s01.DOMAIN.local,Maintenance Mode,Normal,0%,2%,Never,Enabled,,,,,"

"esxdc2s02.DOMAIN.local,Maintenance Mode,Normal,0%,2%,Never,Enabled,,,,,"

"esxdc2s03.DOMAIN.local,Maintenance Mode,Alert,0%,2%,Never,Enabled,,,,,"

And my code is:

$HostList = Import-Csv Hostprofiles.csv

Connect-VIServer -Server VIRTUALCENTER.DOMAIN.local -User "qa\a-dubutcher" -Password MYPASSWORD

# Apply host profile to all the VMs in $HostList
foreach($Host in $HostList) {
    Apply-VMHostProfile -Entity $($Host.NAME) -Profile YKFProfile -Confirm:$false
}

And my error message is:

Cannot overwrite variable Host because it is read-only or constant.
At C:\Users\dubutcher\Dropbox\VMware\Scripts\Blackberry_Applyprofile.ps1:6 char
:8
+ foreach <<<< ($Host in $HostList) {
    + CategoryInfo          : WriteError: (Host:String) [], SessionStateUnauth
   orizedAccessException
    + FullyQualifiedErrorId : VariableNotWritable

Any suggestions?

Reply
0 Kudos
1 Solution

Accepted Solutions
Zsoldier
Expert
Expert
Jump to solution

The "$Host" variable is system reserved so you cannot overwrite it.

Your code updated:

$HostList = Import-Csv Hostprofiles.csv

Connect-VIServer -Server VIRTUALCENTER.DOMAIN.local -User "qa\a-dubutcher" -Password MYPASSWORD

# Apply host profile to all the VMs in $HostList
foreach($VMHost in $HostList) {
    Apply-VMHostProfile -Entity $($VMHost.NAME) -Profile YKFProfile -Confirm:$false
}

Chris Nakagaki (中垣浩一)
Blog: https://tech.zsoldier.com
Twitter: @zsoldier

View solution in original post

Reply
0 Kudos
3 Replies
Zsoldier
Expert
Expert
Jump to solution

The "$Host" variable is system reserved so you cannot overwrite it.

Your code updated:

$HostList = Import-Csv Hostprofiles.csv

Connect-VIServer -Server VIRTUALCENTER.DOMAIN.local -User "qa\a-dubutcher" -Password MYPASSWORD

# Apply host profile to all the VMs in $HostList
foreach($VMHost in $HostList) {
    Apply-VMHostProfile -Entity $($VMHost.NAME) -Profile YKFProfile -Confirm:$false
}

Chris Nakagaki (中垣浩一)
Blog: https://tech.zsoldier.com
Twitter: @zsoldier
Reply
0 Kudos
dbutch1976
Hot Shot
Hot Shot
Jump to solution

Thanks for your reponse.  Now I'm getting:

Apply-VMHostProfile : Cannot validate argument on parameter 'Entity'. The argum

ent is null or empty. Supply an argument that is not null or empty and then try

the command again.

At C:\Scripts\Applyprofile.ps1:9 char

:32

+     Apply-VMHostProfile -Entity <<<<  $($VMHost.NAME) -Profile YKFProfile -Co

nfirm:$false

    + CategoryInfo          : InvalidData: (:) [Apply-VMHostProfile], Paramete

   rBindingValidationException

    + FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutom

   ation.ViCore.Cmdlets.Commands.Host.ApplyVMHostProfile

I have run the command successfully manually:

Apply-VMHostProfile -Entity "esxdc2s02.DOMAIN.local" -Profile MYPROFILE -Confirm:$false

Is it something to do with the -Entity switch?

Reply
0 Kudos
dbutch1976
Hot Shot
Hot Shot
Jump to solution

My .csv file had double quotes, once I removed them everything started working.  Thanks for the help!

Reply
0 Kudos