1

For example if I do:

PS> foreach ($i in $list)
{
    Write-Host $i
}

Produces no error, just a new command prompt.

PS>

Even when calling attributes on objects that don't exist, don't output any error:

PS>(Get-SPWeb "http://homesite").Sites

New command prompt

PS>

Yet it should be:

PS>(Get-SPWeb "http://homesite").Site

Correct results:

Url                                                     CompatibilityLevel  
---                                                     ------------------  
http://homesite                                         15                  
jes516
  • 542
  • 1
  • 13
  • 30
  • 4
    Use `Set-StrictMode -version 2` and you'll get your errors. – wOxxOm Mar 06 '17 at 15:46
  • Answered before: http://stackoverflow.com/questions/32905633/no-error-when-selecting-non-existing-property – Mark Wragg Mar 06 '17 at 15:48
  • 2
    @wOxxOm - I'd actually be inclined to `Set-StrictMode -Latest` and 'force' PS to whine about other sorts of things as well. But then, I like fascist programming languages; I'm less likely to end up with subtle logic bugs. – Jeff Zeitlin Mar 06 '17 at 15:50

1 Answers1

3

Because that's simply the way PowerShell was implemented, however there are many ways to check for keys on Objects.

Least Pedantic:

$s = Get-SPWeb "http://homesite"
if (!$s.Sites) {Write-Error "Invalid Key used!"}

Moderately Pedantic

$s = Get-SPWeb "http://homesite"
if ($a.sites -eq $null) {Write-Error "Invalid Key used!"}

Very Pedantic

$s = Get-SPWeb "http://homesite"
# gm is an alias for Get-Member
if (!($s | gm -MemberType Properties | % Name).contains("sites")) {Write-Error "Invalid Key used!"}
AP.
  • 8,082
  • 2
  • 24
  • 33