locked
"IF" Statement that detects decimal input RRS feed

  • Question

  • I need to alert when a decimal is entered in the %Increase:

    $User = Read-Host "Enter an alias"
    $Increase = Read-Host "Enter % Increase"
    $DecException = $Increase
    $colitems = Get-Mailbox -identity $User | Select-object
    IssueWarningQuota,ProhibitSendQuota,ProhibitSendReceiveQuota

    FOREACH ($ColItem in $Colitems)
     {
     $Warning = ([string]$ColItem.IssueWarningQuota).split("K")[0]
     $ProhibitSend = ([string]$ColItem.ProhibitSendQuota).split("K")[0]
     $ProhibitSendReceive = ([string]$ColItem.ProhibitSendReceiveQuota).split("K")[0]
     }
      
    #Write-Host "Prohibit Send: $ProhibitSend"
    #Write-Host "Prohibit Receive: $ProhibitReceive"
    #Write-Host "Issue Warning: $Warning"

    Tuesday, August 28, 2012 5:54 PM

Answers

  • On Wed, 29 Aug 2012 19:46:03 +0000, RJB90 wrote:
     
    >Can you help me with this: DO {[int]$Increase = Read-Host "Enter % Increase"; $p = $increase.split(".")} UNTIL ($p.length -gt 1)
     
    If you're going to cast "$Increase" and an integer you'll never have a
    decimal point in it. If ".5" is entered you'll get a zero as the
    value. If they enter ".7" you'll get a "1" as a value. And becasue
    it's an integer you'll never get "split" to work since that's a
    function that's only available to strings.
     
    Do you WANT a decimal point in the value or not? Your prompt to the
    user just asks for "%". To most people that implies an integer like
    "50" or "75" or "90" or "150". I don't know may people that would
    recognize that you're asking for ".50" or ".75" or ".90" or "1.50".
     
    >This is the logic that i am trying to work with, I am not understanding what you posted. I need to detect when a user puts a decimal and kick back to say only whole numbers are permitted.
     
    That's exactly what my example does. You can replace the "split" with
    this if it make more sense to you:
     
    if ($increase -like "*.*" -or $increase like ".*" or -increase -like
    "*.")
     
    >When i am running my script i am having a "system.double" issue. I am assuming that i am comparing a number with a string.
    >
    >
    >
    >Error: Method invocation failed because [System.Double] doesn't contain a method named 'split'. At line:1 char:19 + $b = [int]$a.split <<<< (".") + CategoryInfo : InvalidOperation: (split:String) [], RuntimeExce ption + FullyQualifiedErrorId : MethodNotFound
     
    That exception doesn't match the "DO..." you posted. There's no "$b"
    variable there, nor is there a "$a" variable.
     
    If you run the code sampe you posted you'd get this exception:
     
    Method invocation failed because [System.Int32] doesn't contain a
    method named 'split'.
     
    $Increase is, after all, an integer not a string.
     
    ---
    Rich Matheisen
    MCSE+I, Exchange MVP
     

    --- Rich Matheisen MCSE+I, Exchange MVP
    Wednesday, August 29, 2012 9:51 PM

All replies

  • On Tue, 28 Aug 2012 17:54:36 +0000, RJB90 wrote:
     
    >
    >
    >I need to alert when a decimal is entered in the %Increase:
    >
    >$User = Read-Host "Enter an alias" $Increase = Read-Host "Enter % Increase" $DecException = $Increase $colitems = Get-Mailbox -identity $User | Select-object IssueWarningQuota,ProhibitSendQuota,ProhibitSendReceiveQuota
    >
    >FOREACH ($ColItem in $Colitems) { $Warning = ([string]$ColItem.IssueWarningQuota).split("K")[0] $ProhibitSend = ([string]$ColItem.ProhibitSendQuota).split("K")[0] $ProhibitSendReceive = ([string]$ColItem.ProhibitSendReceiveQuota).split("K")[0] } #Write-Host "Prohibit Send: $ProhibitSend" #Write-Host "Prohibit Receive: $ProhibitReceive" #Write-Host "Issue Warning: $Warning"
     
    How about:
     
    $p = $increase.split(".")
    if ($p.length -gt 1)
    {
    "decimal present in data"
    }
     
    I gotta say I've never seen the quotas dealt with in the way you're
    doing it.
     
    How about this instead?
     
    $increase = read-host "Enter % Increase"
    while (($increase.split(".")).length -gt 1)
    {
    "Value cannot cantain a decimal point!"
    $increase = read-host "Enter % Increase"
    }
    $grow = 1.0 + ($increase/100)
    $m = get-mailbox -identity $user -resultsize 1
    $setmbx = $false
    if ($m.issuewarningquota.isunlimited -eq $false)
    {
    $m.issuewarningquota = $m.issuewarningquota.value.tokb() * $grow
    $setmbx = $true
    }
    if ($m.prohibitsendquota.isunlimited -eq $false)
    {
    $m.prohibitsendquota = $m.prohibitsendquota.value.tokb() * $grow
    $setmbx = $true
    }
    if ($m.prohibitsendreceivequota.isunlimited -eq $false)
    {
    $m.prohibitsendreceivequota = $m.prohibitsendreceivequota.value.tokb()
    * $grow
    $setmbx = $true
    }
    if ($setmbx)
    {
    $m | set-mailbox
    }
     
    ---
    Rich Matheisen
    MCSE+I, Exchange MVP
     

    --- Rich Matheisen MCSE+I, Exchange MVP
    Wednesday, August 29, 2012 12:54 AM
  • Can you help me with this:

    DO {[int]$Increase = Read-Host "Enter % Increase"; $p = $increase.split(".")}
    UNTIL ($p.length -gt 1)

    This is the logic that i am trying to work with, I am not understanding what you posted. I need to detect when a user puts a decimal and kick back to say only whole numbers are permitted. When i am running my script i am having a "system.double" issue. I am assuming that i am comparing a number with a string.

    Error: Method invocation failed because [System.Double] doesn't contain a method named
     'split'.
    At line:1 char:19
    + $b = [int]$a.split <<<< (".")
        + CategoryInfo          : InvalidOperation: (split:String) [], RuntimeExce
       ption
        + FullyQualifiedErrorId : MethodNotFound

    Wednesday, August 29, 2012 7:46 PM
  • On Wed, 29 Aug 2012 19:46:03 +0000, RJB90 wrote:
     
    >Can you help me with this: DO {[int]$Increase = Read-Host "Enter % Increase"; $p = $increase.split(".")} UNTIL ($p.length -gt 1)
     
    If you're going to cast "$Increase" and an integer you'll never have a
    decimal point in it. If ".5" is entered you'll get a zero as the
    value. If they enter ".7" you'll get a "1" as a value. And becasue
    it's an integer you'll never get "split" to work since that's a
    function that's only available to strings.
     
    Do you WANT a decimal point in the value or not? Your prompt to the
    user just asks for "%". To most people that implies an integer like
    "50" or "75" or "90" or "150". I don't know may people that would
    recognize that you're asking for ".50" or ".75" or ".90" or "1.50".
     
    >This is the logic that i am trying to work with, I am not understanding what you posted. I need to detect when a user puts a decimal and kick back to say only whole numbers are permitted.
     
    That's exactly what my example does. You can replace the "split" with
    this if it make more sense to you:
     
    if ($increase -like "*.*" -or $increase like ".*" or -increase -like
    "*.")
     
    >When i am running my script i am having a "system.double" issue. I am assuming that i am comparing a number with a string.
    >
    >
    >
    >Error: Method invocation failed because [System.Double] doesn't contain a method named 'split'. At line:1 char:19 + $b = [int]$a.split <<<< (".") + CategoryInfo : InvalidOperation: (split:String) [], RuntimeExce ption + FullyQualifiedErrorId : MethodNotFound
     
    That exception doesn't match the "DO..." you posted. There's no "$b"
    variable there, nor is there a "$a" variable.
     
    If you run the code sampe you posted you'd get this exception:
     
    Method invocation failed because [System.Int32] doesn't contain a
    method named 'split'.
     
    $Increase is, after all, an integer not a string.
     
    ---
    Rich Matheisen
    MCSE+I, Exchange MVP
     

    --- Rich Matheisen MCSE+I, Exchange MVP
    Wednesday, August 29, 2012 9:51 PM
  • I would assume the same. I passed it by some colleagues and they recommended that if a decimal was inputted it generates an "Error" message. More of a sanity checker.

    Thanks for your help.

    Wednesday, August 29, 2012 10:22 PM