Powershell if formatting

I try to run below script, but when I do this, 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"
 }
}
+3
source share
2 answers

The parser hits this - and interprets it as a Powershell statement. A single quotation mark property to be interpreted as a literal string:

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

As an alternative to mjolinor's answer, you can rename the attribute when you select it.

For example, this should work (unverified):

$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: “Name” indicates the new name for the variable, and “Expression” indicates the variable you want to rename.

+1
source

All Articles