- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Why if condition does not work correctly in my script
I have written follow script with if condition but when I choose 3 it shows many error although I want it just show "Insert correct digit"
"Please choose an Option to apply configurations:"
"1- Select Host"
"2- Apply on All ESXi Host"
$options= Read-Host "Enter your Number 1/2"
if ( 1 -eq $options )
{
$hostname= read-host "Please enter your hostname"
Write-Output "esxi-7.account-auto-unlock-time" | Green
Get-VMHost $hostname | Get-AdvancedSetting Security.AccountUnlockTime | Set-AdvancedSetting -Value 900
}
if ( 2 -eq $options )
{
Write-Output "6- esxi-7.account-auto-unlock-time" | Green
Get-VMHost | Get-AdvancedSetting Security.AccountUnlockTime | Set-AdvancedSetting -Value 900
}
else {
Write-Output "Insert correct number"
}
When insert 3 it shows follow error
Get-VMHost : Cannot validate argument on parameter 'Name'. The argument is null or empty. Provide an argument that is
not null or empty, and then try the command again.
At C:\HP server\security.ps1:92 char:14
+ Get-VMHost $hostname | New-AdvancedSetting -Entity $_ -Name log.kee ...
+ ~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Get-VMHost], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutomation.ViCore.Cmdlets.Commands.GetVMHost
Although it just shows Insert correct number
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Your code works perfectly for me.
Judging from the line number in the error message (line 92) there is more code you are not showing.
The error might be happening there
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Yes . The problem relates to this part that I resided it between 2 if
Get-Cluster -Name $clusterName | Get-VM |
ForEach-Object -Process {
Get-VMHost $hostname | New-AdvancedSetting -Entity $_ -Name log.keepOld -Value '10' -Confirm:$false -Force
Get-VMHost $hostname | New-AdvancedSetting -Entity $_ -Name RemoteDisplay.maxConnections -Value '1' -Confirm:$false -Force
Get-VMHost $hostname | New-AdvancedSetting -Entity $_ -Name log.rotateSize -Value '2048000' -Confirm:$false -Force
"Please choose an Option to apply configurations:"
"1- Select Host"
"2- Apply on All ESXi Host"
$options= Read-Host "Enter your Number 1/2"
if ( 1 -eq $options )
{
$hostname= read-host "Please enter your hostname"
Write-Output "esxi-7.account-auto-unlock-time"
Get-VMHost $hostname | Get-AdvancedSetting Security.AccountUnlockTime | Set-AdvancedSetting -Value 900
}
Get-Cluster -Name $clusterName | Get-VM |
ForEach-Object -Process {
Get-VMHost $hostname | New-AdvancedSetting -Entity $_ -Name log.keepOld -Value '10' -Confirm:$false -Force
Get-VMHost $hostname | New-AdvancedSetting -Entity $_ -Name RemoteDisplay.maxConnections -Value '1' -Confirm:$false -Force
Get-VMHost $hostname | New-AdvancedSetting -Entity $_ -Name log.rotateSize -Value '2048000' -Confirm:$false -Force
}
if
( 2 -eq $options )
{
Write-Output "6- esxi-7.account-auto-unlock-time" | Green
Get-VMHost | Get-AdvancedSetting Security.AccountUnlockTime | Set-AdvancedSetting -Value 900
}
else {
Write-Output "Insert correct number" | Red
}
How can correct it ?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Not exactly sure what you are trying to do, but the block of code in the red box is always executed, independent of the value in $options.
Should that code only be executed when $options is equal to 1?
Also not sure why do the following at the start, before even asking for option 1 or 2?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
It would help if you can tell us what you are actually trying to do with that code.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Actually I want to apply some configuration on hosts and some configurations on VMS
when we choose 1 and enter host name follow parameters should apply on specific host :
$clusterName= Read-Host "Please enter your cluster_name"
"There are follow Hosts in your cluster:"
Get-VMHost | Format-Table Name
"Please choose an Option to apply security configurations:"
"1- Select Host"
"2- Apply on All ESXi Host"
$options= Read-Host "Enter your Number 1/2"
if ( 1 -eq $options )
{
$hostname= read-host "Please enter your hostname"
Write-Output "esxi-7.account-auto-unlock-time"
Get-VMHost $hostname | Get-AdvancedSetting Security.AccountUnlockTime | Set-AdvancedSetting -Value 900
}
After apply above changes on esxi host need to apply some configuration parameters on VMs on that host so I used this
Get-Cluster -Name $clusterName | Get-VM |
ForEach-Object -Process {
Get-VMHost $hostname | New-AdvancedSetting -Entity $_ -Name log.keepOld -Value '10' -Confirm:$false -Force
Get-VMHost $hostname | New-AdvancedSetting -Entity $_ -Name RemoteDisplay.maxConnections -Value '1' -Confirm:$false -Force
Get-VMHost $hostname | New-AdvancedSetting -Entity $_ -Name log.rotateSize -Value '2048000' -Confirm:$false -Force
}
Now the other option is 2 when the user choose it means want to apply configuration on all hosts and VMS so use this one to apply changes on all hosts :
if
( 2 -eq $options )
{
Write-Output "6- esxi-7.account-auto-unlock-time" | Green
Get-VMHost | Get-AdvancedSetting Security.AccountUnlockTime | Set-AdvancedSetting -Value 900
and apply follow changes on all vms
Get-Cluster -Name $clusterName | Get-VM |
ForEach-Object -Process {
New-AdvancedSetting -Entity $_ -Name log.keepOld -Value '10' -Confirm:$false -Force
New-AdvancedSetting -Entity $_ -Name RemoteDisplay.maxConnections -Value '1' -Confirm:$false -Force
New-AdvancedSetting -Entity $_ -Name log.rotateSize -Value '2048000' -Confirm:$false -Force
so finally wrote this one :
$clusterName= Read-Host "Please enter your cluster_name"
"There are follow Hosts in your cluster:"
Get-VMHost | Format-Table Name
"Please choose an Option to apply security configurations:"
"1- Select Host"
"2- Apply on All ESXi Host"
$options= Read-Host "Enter your Number 1/2"
if ( 1 -eq $options )
{
$hostname= read-host "Please enter your hostname"
Write-Output "esxi-7.account-auto-unlock-time"
Get-VMHost $hostname | Get-AdvancedSetting Security.AccountUnlockTime | Set-AdvancedSetting -Value 900
}
Get-Cluster -Name $clusterName | Get-VM |
ForEach-Object -Process {
Get-VMHost $hostname | New-AdvancedSetting -Entity $_ -Name log.keepOld -Value '10' -Confirm:$false -Force
Get-VMHost $hostname | New-AdvancedSetting -Entity $_ -Name RemoteDisplay.maxConnections -Value '1' -Confirm:$false -Force
Get-VMHost $hostname | New-AdvancedSetting -Entity $_ -Name log.rotateSize -Value '2048000' -Confirm:$false -Force
}
if
( 2 -eq $options )
{
Write-Output "6- esxi-7.account-auto-unlock-time" | Green
Get-VMHost | Get-AdvancedSetting Security.AccountUnlockTime | Set-AdvancedSetting -Value 900
Get-Cluster -Name $clusterName | Get-VM |
ForEach-Object -Process {
New-AdvancedSetting -Entity $_ -Name log.keepOld -Value '10' -Confirm:$false -Force
New-AdvancedSetting -Entity $_ -Name RemoteDisplay.maxConnections -Value '1' -Confirm:$false -Force
New-AdvancedSetting -Entity $_ -Name log.rotateSize -Value '2048000' -Confirm:$false -Force
}
}
else {
Write-Output "Insert correct number"
}
What is the issue ?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Then you should place the following lines inside the block for $options -eq 1
Now it is always executed, whatever the value in $options
Get-Cluster -Name $clusterName | Get-VM |
ForEach-Object -Process {
Get-VMHost $hostname | New-AdvancedSetting -Entity $_ -Name log.keepOld -Value '10' -Confirm:$false -Force
Get-VMHost $hostname | New-AdvancedSetting -Entity $_ -Name RemoteDisplay.maxConnections -Value '1' -Confirm:$false -Force
Get-VMHost $hostname | New-AdvancedSetting -Entity $_ -Name log.rotateSize -Value '2048000' -Confirm:$false -Force
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Thanks. But as I said when we select 2 not define specific hosts and it should apply on all hosts and VMS so this part should be change such as this when select 2 :
Get-Cluster -Name $clusterName | Get-VM |
ForEach-Object -Process {
New-AdvancedSetting -Entity $_ -Name log.keepOld -Value '10' -Confirm:$false -Force
New-AdvancedSetting -Entity $_ -Name RemoteDisplay.maxConnections -Value '1' -Confirm:$false -Force
New-AdvancedSetting -Entity $_ -Name log.rotateSize -Value '2048000' -Confirm:$false -Force
Also I have resided that script here as I got error Would you please check it in your env ?
"Please choose an Option to apply security configurations:"
"1- Select Host"
"2- Apply on All ESXi Host"
$options= Read-Host "Enter your Number 1/2"
if ( 1 -eq $options )
{
$hostname= read-host "Please enter your hostname"
Write-Output "esxi-7.account-auto-unlock-time"
Get-VMHost $hostname | Get-AdvancedSetting Security.AccountUnlockTime | Set-AdvancedSetting -Value 900 -Confirm:$false -Force
Get-Cluster -Name $clusterName | Get-VM |
ForEach-Object -Process {
Get-VMHost $hostname | New-AdvancedSetting -Entity $_ -Name log.keepOld -Value '10' -Confirm:$false -Force
Get-VMHost $hostname | New-AdvancedSetting -Entity $_ -Name RemoteDisplay.maxConnections -Value '1' -Confirm:$false -Force
Get-VMHost $hostname | New-AdvancedSetting -Entity $_ -Name log.rotateSize -Value '2048000' -Confirm:$false -Force
}}
if
( 2 -eq $options )
{
Write-Output "6- esxi-7.account-auto-unlock-time" | Green
Get-VMHost | Get-AdvancedSetting Security.AccountUnlockTime | Set-AdvancedSetting -Value 900
Get-Cluster -Name $clusterName | Get-VM |
ForEach-Object -Process {
New-AdvancedSetting -Entity $_ -Name log.keepOld -Value '10' -Confirm:$false -Force
New-AdvancedSetting -Entity $_ -Name RemoteDisplay.maxConnections -Value '1' -Confirm:$false -Force
New-AdvancedSetting -Entity $_ -Name log.rotateSize -Value '2048000' -Confirm:$false -Force
}}
else {
Write-Output "Insert correct number"
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
That looks ok.
I would make a few small changes to optimise the code
"Please choose an Option to apply security configurations:"
"1- Select Host"
"2- Apply on All ESXi Host"
$options = Read-Host "Enter your Number 1/2"
if ( 1 -eq $options ) {
$hostname = Read-Host "Please enter your hostname"
Write-Output "esxi-7.account-auto-unlock-time"
$esx = Get-VMHost -Name $hostname
Get-AdvancedSetting -Entity $esx -Name 'Security.AccountUnlockTime' | Set-AdvancedSetting -Value 900 -Confirm:$false -Force
Get-VM -Location $esx | ForEach-Object -Process {
Get-VMHost $hostname | New-AdvancedSetting -Entity $_ -Name 'log.keepOld' -Value '10' -Confirm:$false -Force
Get-VMHost $hostname | New-AdvancedSetting -Entity $_ -Name 'RemoteDisplay.maxConnections' -Value '1' -Confirm:$false -Force
Get-VMHost $hostname | New-AdvancedSetting -Entity $_ -Name 'log.rotateSize' -Value '2048000' -Confirm:$false -Force
}
} elseif ( 2 -eq $options ) {
Write-Output "6- esxi-7.account-auto-unlock-time" | Green
$esx = Get-VMHost
Get-AdvancedSetting -Entity $esx -Name 'Security.AccountUnlockTime' | Set-AdvancedSetting -Value 900
Get-VM -Location $esx |
ForEach-Object -Process {
New-AdvancedSetting -Entity $_ -Name 'log.keepOld' -Value '10' -Confirm:$false -Force
New-AdvancedSetting -Entity $_ -Name 'RemoteDisplay.maxConnections' -Value '1' -Confirm:$false -Force
New-AdvancedSetting -Entity $_ -Name 'log.rotateSize' -Value '2048000' -Confirm:$false -Force
}
} else {
Write-Output "Insert correct number"
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
1- When we choose option 2 what does this mean here :
$esx = Get-VMHost
because we want to apply on all hosts and VMs
$esx = Get-VMHost
Get-AdvancedSetting -Entity $esx -Name 'Security.AccountUnlockTime' | Set-AdvancedSetting -Value 900
Get-VM -Location $esx |
ForEach-Object -Process {
New-AdvancedSetting -Entity $_ -Name 'log.keepOld' -Value '10' -Confirm:$false -Force
New-AdvancedSetting -Entity $_ -Name 'RemoteDisplay.maxConnections' -Value '1' -Confirm:$false -Force
New-AdvancedSetting -Entity $_ -Name 'log.rotateSize' -Value '2048000' -Confirm:$false -Force
2- How can change this such as your sample when we want to apply on all hosts and specific host?
Get-VMHost $hostname | Get-VMHostService | where {$_.Key -eq 'TSM-SSH'} | Set-VMHostService -Policy Off
Get-VMHost $hostname | Get-VMHostService | where {$_.Key -eq 'TSM-SSH'} | Stop-VMHostService -Confirm:$false
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
1. The Get-VMHost cmdlet gets all ESXi nodes
2. With a single value in $hostname you get 1 ESXi node.
With just Get-VMHost, no value for the -Name parameter, you get all ESXi nodes (see 1.)
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Would you please correct this one for when using specific host and also for all hosts in cluster
Get-VMHost $hostname | Get-VMHostService | where {$_.Key -eq 'TSM-SSH'} | Set-VMHostService -Policy Off
Get-VMHost $hostname | Get-VMHostService | where {$_.Key -eq 'TSM-SSH'} | Stop-VMHostService -Confirm:$false
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
In the last code, I posted those 2 options are already there
Option 1
$esx = Get-VMHost -Name $hostname
Option 2
$esx = Get-VMHost
Not sure what you don't understand.
With option1 there is 1 ESXi node in the $esx variable.
With option 2 all the ESXi nodes are in the $esx variable
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
This is my question :
1- when I run " Get-VMHostService | where {$_.Key -eq 'snmpd'} | Set-VMHostService -Policy Off" this will apply on all hosts how can define to apply on specific host ?
2- When I want to run below script on specific host is this correct ?
Get-VMHost $esx | Get-VMHostService | where {$_.Key -eq 'snmpd'} | Stop-VMHostService -Confirm:$false
3- Is this script correct if I want to run it on specific host ? "Get-VMHost $esx | Get-VMHostService | where {$_.Key -eq 'snmpd'} | Stop-VMHostService -Confirm:$false"
4- Is this script correct if I want to run it on all hosts ?
Get-VMHost $esx | Get-VMHostService | where {$_.Key -eq 'snmpd'} | Stop-VMHostService -Confirm:$false
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Did you even try the script I provided earlier?
You can't just lift out a single line without considering the context.
The trick is that the $esx variable is populated with 1 ESXi node (option 1) or all ESXi nodes (option 2).
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I will try and inform
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
What is the following line supposed to do:
Write-Output "6- esxi-7.account-auto-unlock-time" | Green
Is this supposed to do something like:
Write-Host -ForegroundColor Green "6- esxi-7.account-auto-unlock-time"
Or is a different purpose intended?