I am trying to get password from user.
If he simply enter nothing [just press the enter key alone], then warning should be thrown.
$NewUserPassword = Read-Host -assecurestring "Please enter New Run time user password"
$NewUserPassword=$NewUserPassword.Trim()
If ( ($NewUserPassword -eq $null) -or ($NewUserPassword -eq "") ){
Write-Warning "Please enter valid password. Script execution is stopped"
Exit
}
But script throws error as follows
Method invocation failed because [System.Security.SecureString]
doesn’t contain a method named ‘Trim’.
But if i remove trim() method ,the application still doesn’t throw warning.
What could be the reason? How to get valid password?
You cannot inspect the value of a SecureString instance directly and comparing with a
Stringinstance is futile anyway. However, it has aLengthproperty which you can use for checking whether it is empty: