JoeRod
JoeRod

Reputation: 921

Powershell if formatting

I'm trying run the below script but when I do I get

Unexpected token '-PrimaryUserAddress' in expression or statement

How can I fix this?

$users = Get-ADUser "joerod" -Properties *|Select-Object SamAccountName, msRTCSIP-PrimaryUserAddress

ForEach ($user in $users) {
if($user.msRTCSIP-PrimaryUserAddress -ne 0){
Write-Host "Removing $($user.samaccountname) from OCS"
 }
}

Upvotes: 3

Views: 415

Answers (2)

Dave Clarke
Dave Clarke

Reputation: 2696

Alternatively to mjolinor's answer, you could rename the attribute when you select it.

For example, this should work (untested):

$users = Get-ADUser "joerod" -Properties *|Select-Object SamAccountName, @{Name="PrimaryUserAddress";Expression={$_."msRTCSIP-PrimaryUserAddress"}}    

ForEach ($user in $users) {
     if($user.PrimaryUserAddress -ne 0){
         Write-Host "Removing $($user.samaccountname) from OCS"
     }
}

NB: The 'Name' specifies the new name for the variable, while 'Expression' specifies the variable you wish to rename.

Upvotes: 1

mjolinor
mjolinor

Reputation: 68301

The parser is hitting that - and interpreting it as a Powershell operator. Single quote the property so that it gets interpreted as a literal string:

if($user.'msRTCSIP-PrimaryUserAddress' -ne 0)

Upvotes: 6

Related Questions