Hi All,
I wonder if someone could give me some critique on my script, regretfully PowerCLI is not my strong point. I'm trying to add some PCi compliance values to a test host and then later to a cluster. Clearly I have gone wrong in a multitude of ways
Script below ...
$ESXihost = 'secretserver.co.uk'
$creds = Get-Credential
Connect-VIServer -Server $ESXihost -Credential $creds
get-vmhost $ESXihost | foreach-object {
write-host 'Amending ESXiShellInteractiveTimeOut value'
$_ | set-AdvancedSetting -Name UserVars.ESXiShellInteractiveTimeOut | Set-AdvancedSetting -Value 600 -confirm:$false
write-host 'Amending ESXiShellTimeOut value'
$_ | set-AdvancedSetting -Name UserVars.ESXiShellTimeOut | Set-AdvancedSetting -Value 600 -confirm:$false
write-host 'Amending UserVars.DcuiTimeout value'
$_ | set-AdvancedSetting -Name UserVars.DcuiTimeout | Set-AdvancedSetting -Value 900 -confirm:$false
write-host 'Amending UserVars.AccountUnlockTime value'
$_ | set-AdvancedSetting -Name Security.AccountUnlockTime | Set-AdvancedSetting -Value 1800 -confirm:$false
write-host 'Amending UserVars.AccountUnlockTime value'
$_ | set-AdvancedSetting -Name Security.AccountLockFailures | Set-AdvancedSetting -Value 3 -confirm:$false
write-host 'Amending UserVars.AccountLockFailures value'
$_ | set-vmhostservice | Where-Object {$_.key -eq "snmpd"} | Stop-VMHostService -confirm:$false
}
Disconnect-VIServer -Server $ESXihost -Confirm:$false
Output when run ....
Amending ESXiShellInteractiveTimeOut value
Uid : /VIServer=root@myserver.co.uk:443/VMHost=HostSystem-ha-host/AdvancedSetting=VMHostSetting-UserVars.ESXiShellInteractiveT
imeOut/
Value : 600
Description :
Type : VMHost
Entity : myserver.co.uk
Id : VMHostSetting-UserVars.ESXiShellInteractiveTimeOut
Name : UserVars.ESXiShellInteractiveTimeOut
Amending ESXiShellTimeOut value
Uid : /VIServer=root@myserver.co.uk:443/VMHost=HostSystem-ha-host/AdvancedSetting=VMHostSetting-UserVars.ESXiShellTimeOut/
Value : 600
Description :
Type : VMHost
Entity : myserver.co.uk.co.uk
Id : VMHostSetting-UserVars.ESXiShellTimeOut
Name : UserVars.ESXiShellTimeOut
Amending UserVars.DcuiTimeout value
Get-AdvancedSetting : 16/03/2020 09:11:23 Get-AdvancedSetting "UserVars.DcuiTimeout" option doesn't exist.
At line:10 char:10
+ $_ | Get-AdvancedSetting -Name UserVars.DcuiTimeout | Set-Advance ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Get-AdvancedSetting], InvalidName
+ FullyQualifiedErrorId : Client20_SystemManagementServiceImpl_ConvertToHashtable_OptionNotFound,VMware.VimAutomation.ViCore.Cmdlets.Commands.GetAdvancedSe
tting
Amending UserVars.AccountUnlockTime value
Uid : /VIServer=root@myserver.co.uk:443/VMHost=HostSystem-ha-host/AdvancedSetting=VMHostSetting-Security.AccountUnlockTime/
Value : 1800
Description :
Type : VMHost
Entity : myserver.co.uk
Id : VMHostSetting-Security.AccountUnlockTime
Name : Security.AccountUnlockTime
Amending UserVars.AccountUnlockTime value
Uid : /VIServer=root@myserver.co.uk:443/VMHost=HostSystem-ha-host/AdvancedSetting=VMHostSetting-Security.AccountLockFailures/
Value : 3
Description :
Type : VMHost
Entity : myserver.co.uk
Id : VMHostSetting-Security.AccountLockFailures
Name : Security.AccountLockFailures
Amending UserVars.AccountLockFailures value
Key : snmpd
Label : SNMP Server
Policy : on
Required : False
PS H:\Handy PowerCLI Scripts> Connect-VIServer -Server $ESXihost -Credential $creds
get-vmhost $ESXihost | foreach-object {
write-host 'Amending ESXiShellInteractiveTimeOut value'
$_ | set-AdvancedSetting -Name UserVars.ESXiShellInteractiveTimeOut | Set-AdvancedSetting -Value 600 -confirm:$false
write-host 'Amending ESXiShellTimeOut value'
$_ | set-AdvancedSetting -Name UserVars.ESXiShellTimeOut | Set-AdvancedSetting -Value 600 -confirm:$false
write-host 'Amending UserVars.DcuiTimeout value'
$_ | set-AdvancedSetting -Name UserVars.DcuiTimeout | Set-AdvancedSetting -Value 900 -confirm:$false
write-host 'Amending UserVars.AccountUnlockTime value'
$_ | set-AdvancedSetting -Name Security.AccountUnlockTime | Set-AdvancedSetting -Value 1800 -confirm:$false
write-host 'Amending UserVars.AccountUnlockTime value'
$_ | set-AdvancedSetting -Name Security.AccountLockFailures | Set-AdvancedSetting -Value 3 -confirm:$false
write-host 'Amending UserVars.AccountLockFailures value'
$_ | set-vmhostservice | Where-Object {$_.key -eq "snmpd"} | Stop-VMHostService -confirm:$false
}
Disconnect-VIServer -Server $ESXihost -Confirm:$false
Name Port User
---- ---- ----
myserver.co.uk... 443 root
Amending ESXiShellInteractiveTimeOut value
Set-AdvancedSetting : A parameter cannot be found that matches parameter name 'Name'.
At line:6 char:30
+ $_ | set-AdvancedSetting -Name UserVars.ESXiShellInteractiveTimeO ...
+ ~~~~~
+ CategoryInfo : InvalidArgument: (:) [Set-AdvancedSetting], ParameterBindingException
+ FullyQualifiedErrorId : NamedParameterNotFound,VMware.VimAutomation.ViCore.Cmdlets.Commands.SetAdvancedSetting
Amending ESXiShellTimeOut value
Set-AdvancedSetting : A parameter cannot be found that matches parameter name 'Name'.
At line:8 char:30
+ $_ | set-AdvancedSetting -Name UserVars.ESXiShellTimeOut | Set-Ad ...
+ ~~~~~
+ CategoryInfo : InvalidArgument: (:) [Set-AdvancedSetting], ParameterBindingException
+ FullyQualifiedErrorId : NamedParameterNotFound,VMware.VimAutomation.ViCore.Cmdlets.Commands.SetAdvancedSetting
Amending UserVars.DcuiTimeout value
Set-AdvancedSetting : A parameter cannot be found that matches parameter name 'Name'.
At line:10 char:30
+ $_ | set-AdvancedSetting -Name UserVars.DcuiTimeout | Set-Advance ...
+ ~~~~~
+ CategoryInfo : InvalidArgument: (:) [Set-AdvancedSetting], ParameterBindingException
+ FullyQualifiedErrorId : NamedParameterNotFound,VMware.VimAutomation.ViCore.Cmdlets.Commands.SetAdvancedSetting
Amending UserVars.AccountUnlockTime value
Set-AdvancedSetting : A parameter cannot be found that matches parameter name 'Name'.
At line:12 char:30
+ $_ | set-AdvancedSetting -Name Security.AccountUnlockTime | Set-A ...
+ ~~~~~
+ CategoryInfo : InvalidArgument: (:) [Set-AdvancedSetting], ParameterBindingException
+ FullyQualifiedErrorId : NamedParameterNotFound,VMware.VimAutomation.ViCore.Cmdlets.Commands.SetAdvancedSetting
Amending UserVars.AccountUnlockTime value
Set-AdvancedSetting : A parameter cannot be found that matches parameter name 'Name'.
At line:14 char:30
+ $_ | set-AdvancedSetting -Name Security.AccountLockFailures | Set ...
+ ~~~~~
+ CategoryInfo : InvalidArgument: (:) [Set-AdvancedSetting], ParameterBindingException
+ FullyQualifiedErrorId : NamedParameterNotFound,VMware.VimAutomation.ViCore.Cmdlets.Commands.SetAdvancedSetting
Amending UserVars.AccountLockFailures value
cmdlet Set-VMHostService at command pipeline position 1
Supply values for the following parameters:
PS H:\Handy PowerCLI Scripts> h:\Handy PowerCLI Scripts\ApplyPCI2Host.ps1
Scope ProxyPolicy DefaultVIServerMode InvalidCertificateAction DisplayDeprecationWarnings WebOperationTimeout
Seconds
----- ----------- ------------------- ------------------------ -------------------------- -------------------
Session UseSystemProxy Multiple Warn False 300
User Multiple Warn False
AllUsers Warn
Loading Automation Tools
disconnectiing: myserver.co.uk
cmdlet Get-Credential at command pipeline position 1
Supply values for the following parameters:
User: root
Password for user root: ********************
IsConnected : True
Id : /VIServer=root@myserver.co.uk:443/
ServiceUri : https://myserver.co.uk/sdk
SessionSecret : "fc5f7468819c51ee56ac808c45533711c0789bc6"
Name : myserver.co.uk
Port : 443
SessionId : "fc5f7468819c51ee56ac808c45533711c0789bc6"
User : root
Uid : /VIServer=root@myserver.co.uk:443/
Version : 6.5.0
Build : 13932383
ProductLine : embeddedEsx
InstanceUuid :
RefCount : 1
ExtensionData : VMware.Vim.ServiceInstance
Amending ESXiShellInteractiveTimeOut value
Set-AdvancedSetting : A parameter cannot be found that matches parameter name 'Name'.
At H:\Handy PowerCLI Scripts\ApplyPCI2Host.ps1:16 char:30
+ $_ | set-AdvancedSetting -Name UserVars.ESXiShellInteractiveTimeO ...
+ ~~~~~
+ CategoryInfo : InvalidArgument: (:) [Set-AdvancedSetting], ParameterBindingException
+ FullyQualifiedErrorId : NamedParameterNotFound,VMware.VimAutomation.ViCore.Cmdlets.Commands.SetAdvancedSetting
Amending ESXiShellTimeOut value
Set-AdvancedSetting : A parameter cannot be found that matches parameter name 'Name'.
At H:\Handy PowerCLI Scripts\ApplyPCI2Host.ps1:18 char:30
+ $_ | set-AdvancedSetting -Name UserVars.ESXiShellTimeOut | Set-Ad ...
+ ~~~~~
+ CategoryInfo : InvalidArgument: (:) [Set-AdvancedSetting], ParameterBindingException
+ FullyQualifiedErrorId : NamedParameterNotFound,VMware.VimAutomation.ViCore.Cmdlets.Commands.SetAdvancedSetting
Amending UserVars.DcuiTimeout value
Set-AdvancedSetting : A parameter cannot be found that matches parameter name 'Name'.
At H:\Handy PowerCLI Scripts\ApplyPCI2Host.ps1:20 char:30
+ $_ | set-AdvancedSetting -Name UserVars.DcuiTimeout | Set-Advance ...
+ ~~~~~
+ CategoryInfo : InvalidArgument: (:) [Set-AdvancedSetting], ParameterBindingException
+ FullyQualifiedErrorId : NamedParameterNotFound,VMware.VimAutomation.ViCore.Cmdlets.Commands.SetAdvancedSetting
Amending UserVars.AccountUnlockTime value
Set-AdvancedSetting : A parameter cannot be found that matches parameter name 'Name'.
At H:\Handy PowerCLI Scripts\ApplyPCI2Host.ps1:22 char:30
+ $_ | set-AdvancedSetting -Name Security.AccountUnlockTime | Set-A ...
+ ~~~~~
+ CategoryInfo : InvalidArgument: (:) [Set-AdvancedSetting], ParameterBindingException
+ FullyQualifiedErrorId : NamedParameterNotFound,VMware.VimAutomation.ViCore.Cmdlets.Commands.SetAdvancedSetting
Amending UserVars.AccountUnlockTime value
Set-AdvancedSetting : A parameter cannot be found that matches parameter name 'Name'.
At H:\Handy PowerCLI Scripts\ApplyPCI2Host.ps1:24 char:30
+ $_ | set-AdvancedSetting -Name Security.AccountLockFailures | Set ...
+ ~~~~~
+ CategoryInfo : InvalidArgument: (:) [Set-AdvancedSetting], ParameterBindingException
+ FullyQualifiedErrorId : NamedParameterNotFound,VMware.VimAutomation.ViCore.Cmdlets.Commands.SetAdvancedSettingancedSetting
Amending UserVars.AccountLockFailures value
cmdlet Set-VMHostService at command pipeline position 1
Supply values for the following parameters:
Policy:
That error comes because the advanced setting doesn't exist.
The best way to tackle this is a try-catch construct.
Something like this for example
$esx = Get-VMHost -Name $esxName
try{
$esx | Get-AdvancedSetting -Name UserVars.DcuiTimeout | Set-AdvancedSetting -Value 900 -Confirm:$false
}
catch{
$esx | New-AdvancedSetting -Name UserVars.DcuiTimeout -Value 900 -Confirm:$false
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference