问题
I have a powershell script related to SQL Server which requires the user to pass a string only in this format : machineName\instanceName. For eg., MYMACHINE\MYINST. If the argument is not in this format, the script should display the usage syntax. Can someone please provide script. Thanks in advance.
回答1:
You're looking for parameter validation. Put something like this at the beginning of your script:
[CmdletBinding()]
Param(
[Parameter()]
[ValidateScript({
if ($_ -match '.+\\.+') {
$true
} else {
Write-Host 'Parameter must have the form MACHINE\INSTANCE.'
$false
}
})]
[string]$Instance
)
来源:https://stackoverflow.com/questions/24652484/powershell-string-parameter-validation