Problem with If condition

Hi there, it’s me again wiht my FTP script. It’s all working great besides one minor bug that I have noticed in the section of code below:

If ($FTPGroup -notin $FTPGroupCheck) { Write-Host "FTP group '$FTPGroup' not found, do you want to create a new group with the name $FTPGroup? [Y/N]" $CreateGroup = "" while ([string]::IsNullOrEmpty($CreateGroup)) { $CreateGroup = Read-Host If (($CreateGroup -ne "y") -or ($CreateGroup -ne "n")) { Write-Warning "Please enter Y or N" Write-Host "" } } #If user wants to create the group, create it. If ($CreateGroup -eq 'y') { #Script taken from Scripting Guys blog https://blogs.technet.microsoft.com/heyscriptingguy/2014/10/02/use-powershell-to-create-local-groups/ #Use ADSI to create local group $Computer = $env:COMPUTERNAME $cn = [ADSI]"WinNT://$Computer" $Group = $cn.Create('Group',"$FTPGroup") $Group.SetInfo() | Out-Null $Group.Description = "FTP Users group" $Group.SetInfo() | Out-Null Write-Host "A local group has been created with the name '$FtpGroup'" } #Else select a group from FTP groups on the server Else { If ($FTPGroupCheck -ne $null) { Write-Host "$FtpGroup has not been created. Please select a FTP group from the list below." Write-Host "" $x = 1 $FTPGroupList = @{} forEach ($Group in $FTPGroupCheck) { $FTPGroupList.add($x, $Group) $x++ } Write-Host "Please select the FTP group you wish to add the user/users to." $FTPGroupSelection = "" While ([string]::IsNullOrEmpty($FTPGroupSelection)) { $FTPGroupList.GetEnumerator() | Sort-Object -Property name | forEach { Write-Host ("{0}. {1}" -f $_.Key, $_.Value) Write-Host "" } $FTPGroupSelection = Read-Host If ([int]$FTPGroupSelection -notin $FTPGroupList.Keys) { Write-Warning 'Please make a valid selection.' $FTPGroupSelection = "" Pause Write-Host "" } $FTPGroup = $FTPGroupList[[int]$FTPGroupSelection] Write-Host "" Write-Host ("[{0}. {1}] Has been selected." -f $FTPGroupSelection, $FTPGroup) } } #If no FTP groups on the server create default FTP group Else { Write-Host 'No FTP group found on server, creating default group...' $Computer = $env:COMPUTERNAME $cn = [ADSI]"WinNT://$Computer" $Group = $cn.Create('Group','FTPUsers') $Group.SetInfo() | Out-Null $Group.Description = "FTP Users group" $Group.SetInfo() | Out-Null $FtpGroup = 'FTPUsers' Write-Host "A local group has been created with the name '$FtpGroup'" } } 

The bug is ocurring in this If statement:

If (($CreateGroup -ne "y") -or ($CreateGroup -ne "n")) { Write-Warning "Please enter Y or N" Write-Host "" } 

When I enter Y or N it still enters the ‘Write-Warning’ section. I have tried with brackets around each condition as you see above, I have tried without the extra brackets and I have also tried using ‘-ine’ but still every time. However it picks up on the value and performs the appropriate function later in the subsequent if statements as you can see from the output below:

FTP group 'FTPUsers' not found, do you want to create a new group with the name [Y/N] n WARNING: Please enter Y or N No FTP group found on server, creating default group... A local group has been created with the name 'FTPUsers' 

The variable is clearly holding the value because after it outputs the Write-Warning it immediately skips to the appropriate If/else. However it seems to ignore the value for the validation and always enters it. Its probably something really obvious however I’ve been staring at this for a while and I need some sanity checking.

Any help is much appreciated.

submitted by /u/riiskyy
[link] [comments]

Leave a Reply