# cmdletbinding issues

**URL:** <https://forums.powershell.org/t/cmdletbinding-issues/11750>\
**Category:** PowerShell Help\
**Created:** [December 4, 2018, 4:03pm UTC](https://forums.powershell.org/t/cmdletbinding-issues/11750 "2018-12-04T16:03:55Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![nlong12](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/nlong12/32/123_2.png) [@nlong12](https://forums.powershell.org/u/nlong12)\
**Post date:** [December 4, 2018, 4:03pm UTC](https://forums.powershell.org/t/cmdletbinding-issues/11750/1 "2018-12-04T16:03:55Z")

</div>

Good afternoon;

I’m attempting figure out the best way to handle a null or empty entry and validate accuracy of $servername. ValidateNotNullorEmpty displays an error message would like validateNotNullorEmpty to inform the user their entry must contain a value, validatescript does not like multiple entries, ideally get-vm would allow multiple entries.

Any input would be greatly appreciated.

Norm

&nbsp;

&nbsp;

```
[CmdletBinding()]
Param ([parameter(mandatory,
HelpMessage="Enter one or more VM names separated by commas. Value should not be null or empty")]
[ValidateNotNullOrEmpty()]
[ValidateScript ({ get-Vm $servername })]
[String[]]$servername)
```

---

<div class="post-metadata">

**Author:** ![pdearmen](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/pdearmen/32/263_2.png) [@pdearmen](https://forums.powershell.org/u/pdearmen)\
**Post date:** [December 4, 2018, 4:15pm UTC](https://forums.powershell.org/t/cmdletbinding-issues/11750/2 "2018-12-04T16:15:27Z")

</div>

You can use more than just simple scripts in validation scripts - so if we treat the parameter as an array we can do a foreach in there.

```
[CmdletBinding()]
Param ([parameter(mandatory,
				  HelpMessage = "Enter one or more VM names separated by commas. Value should not be null or empty")]
	[ValidateNotNullOrEmpty()]
	[ValidateScript ({
			foreach ($item in $_)
			{
				try
				{
					get-Vm $item
				}
				catch
				{
					throw "The server name $($item) is invalid"
				}
			}
		})]
	[String[]]$servername)
```

---

<div class="post-metadata">

**Author:** ![ta11ow](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/ta11ow/32/131_2.png) [@ta11ow](https://forums.powershell.org/u/ta11ow)\
**Post date:** [December 4, 2018, 4:18pm UTC](https://forums.powershell.org/t/cmdletbinding-issues/11750/3 "2018-12-04T16:18:58Z")

</div>

It’s not that `ValidateScript` dislikes multiple entries, it’s just that passing an array directly to `Get-VM` like that may or may not be properly supported, and you need to be more careful in how you validate.

Here’s how I’d construct that:

> <https://gist.github.com/vexx32/a55d5f778cd4e115ed18d0e0a0958efd>

For more details on this type of completion and validation, see [my blog post here](https://vexx32.github.io/2018/11/29/Dynamic-ValidateSet/). Note that since you’re validating the parameter with the `Get-VM` command it’s essentially redundant to use `[ValidateNotNullOrEmpty()]` although if you wish to be extra sure, you can still include that just in case.

And, if you happen to be working with a recent version of PS Core, as I mention towards the end of the blog post, you can also use a lesser-known feature of `[ValidateSet()]` to make this happen cleanly. 🙂

---

<div class="post-metadata">

**Author:** ![nlong12](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/nlong12/32/123_2.png) [@nlong12](https://forums.powershell.org/u/nlong12)\
**Post date:** [December 4, 2018, 6:09pm UTC](https://forums.powershell.org/t/cmdletbinding-issues/11750/4 "2018-12-04T18:09:43Z")

</div>

Hello Joel;  
When a I run your response with a valid vm I get the following:  
[3:33 PM]ndl@FWTS012:Powercli $ .\parm\_help\_prompt.ps1  
cmdlet parm\_help\_prompt.ps1 at command pipeline position 1  
Supply values for the following parameters:  
(Type !? for Help.)  
servername[0]: fwapp020  
servername[1]:  
D:\PowerShell\_Test\Powercli\parm\_help\_prompt.ps1 : Cannot validate argument on parameter ‘servername’. The "  
foreach ($Name in $\_) {  
if (-not (Get-VM $Name -ErrorAction SilentlyContinue)) {  
throw “Please enter a valid VM Name.”  
}  
}  
" validation script for the argument with value “fwapp020” did not return a result of True. Determine why the validation script failed, and then try the command again.  
At line:1 char:1

- .\parm\_help\_prompt.ps1
- 

```auto
  + CategoryInfo : InvalidData: (:) [parm_help_prompt.ps1], ParameterBindingValidationException
  + FullyQualifiedErrorId : ParameterArgumentValidationError,parm_help_prompt.ps1

```

Pardon my ignorance, I don’t what your doing with the following section:

```
param($Command, $Parameter, $WordToComplete, $CommandAst, $FakeBoundParams)
            if ($WordToComplete) {
                @((Get-VM).Name) -match "^$WordToComplete"
```

What is the purpose of $command…etc, they look like variables that should be set,

Thank you for your help  
Norm

---

<div class="post-metadata">

**Author:** ![nlong12](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/nlong12/32/123_2.png) [@nlong12](https://forums.powershell.org/u/nlong12)\
**Post date:** [December 4, 2018, 6:21pm UTC](https://forums.powershell.org/t/cmdletbinding-issues/11750/5 "2018-12-04T18:21:00Z")

</div>

Hello Paul;  
Wanted to let you know your script worked, did follow Joel’s advice and remove ValidateNotNullOrEmpty() since it wasn’t accomplishing anything. Wondering if you could let me know how to assign points.

Thank you for your input  
Norm

---

<div class="post-metadata">

**Author:** ![kvprasoon](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/kvprasoon/32/4449_2.png) [@kvprasoon](https://forums.powershell.org/u/kvprasoon)\
**Post date:** [December 4, 2018, 9:43pm UTC](https://forums.powershell.org/t/cmdletbinding-issues/11750/6 "2018-12-04T21:43:15Z")

</div>

@Norm , for you current problem, you would not require the [ArgumentCompleter()] attribute, it’ll behave like a dynamic [ValidateSet()]

you could try changing it like below.

```
[ValidateScript(
        {
            foreach ($Name in $_) {
                if (-not (Get-VM $Name -ErrorAction SilentlyContinue)) {
                    throw "Please enter a valid VM Name."
                }
                $true
            }
        }
    )]
```

---

<div class="post-metadata">

**Author:** ![nlong12](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/nlong12/32/123_2.png) [@nlong12](https://forums.powershell.org/u/nlong12)\
**Post date:** [December 5, 2018, 4:13pm UTC](https://forums.powershell.org/t/cmdletbinding-issues/11750/7 "2018-12-05T16:13:21Z")

</div>

Hello kvprasoon

Your suggestion worked perfectly, thank you

Norm

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:33pm UTC](https://forums.powershell.org/t/cmdletbinding-issues/11750/8 "2024-05-16T20:33:38Z")

</div>

Closed on May 16, 2024, 8:33pm UTC


