Logo

dev-resources.site

for different kinds of informations.

โ“ Do you allow wrong input to enter your function?

Published at
10/19/2024
Categories
powershell
windows
sysadmin
scripting
Author
kamilpro
Author
8 person written this
kamilpro
open
โ“ Do you allow wrong input to enter your function?

โœ‹ Then stop, PowerShell can help you validate your parameters.

The first picture shows how to manually validate whether the number provided is between 1 and 100. It's not perfect.

The second picture does the same and more with fewer lines of code:

  • 1๏ธโƒฃ Makes the parameter required
  • 2๏ธโƒฃ Ensures the provided number is within 1 and 100
  • 3๏ธโƒฃ The [int] type ensures the $Number parameter is an integer

If any of the checks fails, PowerShell prompts the user for the right data, or errors out.

The wrong input never enters your function - it's like a bouncer at the party!

Follow me for more tips!

Image description

Image description

powershell Article's
30 articles in total
Favicon
Windows ไธŠ VSCode ็š„ C/C++ ๅปถไผธๆจก็ต„่™•็†็ทจ็ขผ็š„ๅ•้กŒ
Favicon
PowerShell ็š„ๆ–‡ๅญ—็ทจ็ขผ
Favicon
็ฌ”่ฎฐ3
Favicon
TryHackMe | Windows PowerShell | RSCyberTech
Favicon
Configuring Hyper-V Global Default Directories
Favicon
How I Set Up an Awesome PowerShell Environment for Script Development
Favicon
Azure Function App (Flex Consumption) PowerShell Modules solution
Favicon
File Comparison Made Easy: Detecting New and Changed Files with PowerShell
Favicon
10 Best Practices of PowerShell Code Signing for Signing Your Script
Favicon
PowerShell Script Collection: Automation and Solutions for Everyday Tasks
Favicon
Video: List All Available Windows Features on Windows 11 using CMD & PowerShell
Favicon
Set up Azure Network Security Perimeter with PowerShell
Favicon
Automating Azure Project Setup with PowerShell and GitHub Actions
Favicon
Azure Function App (Flex Consumption) in private VNET via IaC
Favicon
Using PowerShell to Create Service Principals for RBAC
Favicon
Two ways to use Pester to Mock objects with strongly typed parameters
Favicon
PowerShell Automation: Execute Batch GitHub CLI Commands
Favicon
โ“ Do you allow wrong input to enter your function?
Favicon
Introducing PowerShell Utility Scripts
Favicon
Cisco DHCP Pool conversion to Windows Server DHCP
Favicon
PowerShell | Script output garbled Chinese characters
Favicon
Rename Multiple Files in Sequence with Just One Click Using PowerShell in Windows! ๐Ÿš€
Favicon
Redirect Out-File to TestDrive: in your PowerShell Pester test scripts with this one weird trick
Favicon
Using PowerShell for day to day stuff
Favicon
Automating SharePoint Embedded: Using PowerShell to Call Graph API Endpoints
Favicon
Install Ubuntu on WSL 2
Favicon
How to Install WSL from PowerShell on Windows 10 and 11
Favicon
How to Run PowerShell Script in Jenkins Pipeline
Favicon
Identify Which Files are Large.
Favicon
Quick guide to setting up an shortcut/alias in windows

Featured ones: