Guest User

Untitled

a guest
Feb 18th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.90 KB | None | 0 0
  1. function validatePath {
  2. Param
  3. (
  4. [ValidateScript({
  5. If ($_ -eq "" -or $_ -eq [String]::Empty) {
  6. $_ = "C:Install"
  7. $True
  8. }
  9. ElseIf ($_ -match "^([a-z]:\(?:[-\w\.\d])*)") {
  10. $True
  11. }
  12. Else {
  13. Write-Host "Please enter a valid path,$_ is not a valid path."
  14. Write-debug $_.Exception
  15. }
  16. })]
  17. [string]$filePath = "C:Install"
  18. )
  19. Process
  20. {
  21. Write-Host "The path is "$filePath
  22. }
  23. }
  24.  
  25. validatePath -filePath $args[0]
  26.  
  27. Begin{
  28. If ($filepath -eq "") {
  29. $filepath = "C:Install"
  30. }
  31. ElseIf ($filepath -notmatch "^([a-z]:\(?:[-\w\.\d])*)") {
  32. Write-Error "Please enter a valid path,$filepath is not a valid path."
  33. }
  34. }
  35. Process{
  36.  
  37. function validatePath {
  38. Param
  39. (
  40. [ValidateScript({
  41. if ($_ -match '^[a-z]:\[-.wd\]*$') { return $True }
  42. Throw "'$_' is not a valid local path."
  43. })]
  44. [string] $filePath = "C:Install"
  45. )
  46. Process
  47. {
  48. "The path is: $filePath"
  49. }
  50. }
  51.  
  52. > validatePath # use default value
  53. The path is: C:Install
  54.  
  55. > validatePath -filePath C:MyInstall # valid path
  56. The path is: C:MyInstall
  57.  
  58. > validatePath -filePath NotAFullPath # invalid path -> error with custom message
  59. validatePath : Cannot validate argument on parameter 'filePath'.
  60. 'NotAFullPath' is not a valid local path.
  61. At line:1 char:24
  62. + validatePath -filePath NotAFullPath # invalid path
  63. + ~~~~~~~~~~~~
  64. + CategoryInfo : InvalidData: (:) [validatePath], ParameterBindingValidationException
  65. + FullyQualifiedErrorId : ParameterArgumentValidationError,validatePath
  66.  
  67. if ([string] $args[0]) { # only true if $args[0] is neither $null nor the empty string
  68. validatePath -filePath $args[0]
  69. } else {
  70. validatePath
  71. }
  72.  
  73. validatePath @args
  74.  
  75. # Define a hashtable to hold the parameters, if any, to pass through
  76. # to validatePath() via splatting.
  77. $htPassthruParams = @{}
  78.  
  79. # If the first script argument is neither $null nor the empty string,
  80. # add a hashtable entry for it that will bind to the -filePath parameter.
  81. if ([string] $args[0]) { $htPassthruParams.Add('filePath', $args[0]) }
  82.  
  83. # Pass the hashtable with `@`, the splatting operator, to validatePath()
  84. validatePath @htPassthruParams
  85.  
  86. # Define a hashtable to hold the parameters, if any, to pass through
  87. # to validatePath() via splatting.
  88. $htPassthruParams = @{}
  89.  
  90. # Using a list of parameters, pass their values through only if they are
  91. # *bound*, i.e., only if they received values when the enclosing script/function
  92. # itself was called.
  93. # Assume that the enclosing script declared a -filePath parameter too.
  94. foreach($paramName in , 'filePath') {
  95. if ($PSBoundParameters.ContainsKey($paramName)) {
  96. $htPassthruParams.Add($paramName, $PSBoundParameters[$paramName])
  97. }
  98. }
  99.  
  100. # Pass the hashtable with `@`, the splatting operator, to validatePath()
  101. validatePath @htPassthruParams
Add Comment
Please, Sign In to add comment