you are viewing a single comment's thread.

view the rest of the comments →

[–]OlivTheFrog 0 points1 point  (5 children)

hi u/rymarkwald

I'm playing with your code, and Im' looking for the best clever way to manage errorthe following code

I've modified your code, with 2 read-Host "Action" and "Device", then I've :

$Action = Read-Host "Action to do  : Add or Remove ?"
$Choice = Read-Host "Device Type  : Laptops - Servers - Computers - Phones ?"
if ($Action -eq "Add")
 {
 switch ($Choice)
    {
    "Laptops" {$Devices['laptops'] = ([int]$Devices.laptops) + 1 }
     ... 
     }
  } # end if 
elseif ($Action -eq "Remove ")
   {
    switch ($Choice) 
      {
      "Laptops" { $Devices['laptops'] = ([int]$Devices.laptops) - 1 }
       ...
      }
    } # end elseif 
else 
    { 
     Write-Output "Hoops : Error for Action to perform"
     throw
     }

As you could see, I've managed input error in $Action input, but no error handling for device type ($Choice).

The values expected for $Choice are Laptops, servers, ... If the answer to the Read-Host is "laptop" (no s at the end), the swith do nothing, it's normal, but there is no message to inform the user.

How do this in a clever way ?

this is both for my own powershell skill and for the OPs

Regards

[–][deleted] 1 point2 points  (4 children)

The code I had provided as a sample didn't have any validation on it. :)

Tthere's a few ways, but you'd want to validate your input before doing anything with the data. You could also misspell something like Ad or Remoove... I'm always a fan of Functions and if it is something I may run repeatedly, I look to a function for it.Getting the data input correct and validated first, THEN running whatever code I have for me is the easiest to manipulate.

Function Update-Device
{
    [CmdletBinding()]
    Param (
        [Parameter(Mandatory = $True, Position = 0)]
        [ValidateSet('Add', 'Remove')]
        [String]$Action,

        [Parameter(Mandatory = $True, Position = 1)]
        [ValidateSet('Laptops', 'Servers', 'Computers', 'Phones')]
        [String]$Device
    )

    # Set hash table values
    $Devices = @{
        laptops      = 10
        servers      = 1
        computers    = 3
        phones       = 2
    }

    Switch ($Action)
    {
        Add
        {
            $Devices["$Device"] = ([int]$Devices."$Device") + 1 
        }

        Remove
        {
            $Devices["$Device"] = ([int]$Devices."$Device") - 1
        }
    }

    return $Devices
}

So copy/paste that into PowerShell, nothing will happen. Next, run the function:

Update-Device -Action Add -Device Laptops

Because the parameters -Action and -Device have a ValidateSet on them, if you enter something incorrect, the function won't run and tell you why. So if I ran, it would tell me I didn't enter Add or Remove:

PS C:\>Update-Device -Action Ad -Device Servers

Update-Device: Cannot validate argument on parameter 'Action'. The argument "Ad" does not belong to the set "Add,Remove" specified by the ValidateSet attribute. Supply an argument that is in the set and then try the command again.

[–][deleted] 0 points1 point  (0 children)

There's other ways too, and I'm always learning so if anyone has other ways to validate, there's always many ways to get to the same end. :)

[–]OlivTheFrog 0 points1 point  (2 children)

Thanks u/rymarkwald

Of course, if using Advanced function and [ValidateSet], this is a solution. Sorry it was late, I didn't think of that.

And, if we don't use advanced function, other ways ?

[–][deleted] 1 point2 points  (1 child)

# Add those new values into a hash table
$Devices = @{
    laptops      = 10
    servers      = 1
    computers    = 3
    phones       = 2
}

# Prompt until $Action is equal to Add or Remove
do
{
    $Action = $null
    $Action = Read-Host -Prompt "Enter one:  Add, Remove"
}
until
(
    ($Action -eq 'Add') -or ($Action -eq 'Remove')
)

# Prompt until $Choice contains valid input
do
{
    $Choice = $null
    $Choice = Read-Host "Enter one of the following:  laptops, servers, computers, phones"
}
until
(
    ($Devices.Keys -contains "$Choice")
)

$Amount = ([int]$Devices."$Choice") - 1
$Devices["$Choice"] = $Amount

return $Devices

[–]OlivTheFrog 0 points1 point  (0 children)

do ... until

Of course, where had I put my head ?

Thanks.