Link to home
Create AccountLog in
Avatar of osiexchange
osiexchange

asked on

Need help with a PS script

Here is the script I am running to extract info about activesync devices:

$Date = Get-Date -uformat "%Y%m%d"
$Logfile = "C:\logs\activesync-all-$date.txt

$Lst = Get-CASMailbox -ResultSize Unlimited | Where
{$_.HasActiveSyncDevicePartnership - eq $True}

ForEach ($CASMbx in $lst) {
$Devices=$Null
$Devices= @Get-ActiveSyncDeviceStatistics -Mailbox $CASMbx.name)
ForEach ($device in $devices) {
$DeviceModel = $Device.DeviceModel
$DeviceType = $Device.DeviceType
$LastSyncTime = $Device.LastSuccessSync
$PhoneNumber = $Device.DevicePhonenumber
$UserAgent = $Device.DeviceUserAgent
Add-Content -path $Logfile "$casmbx.name
|$DeviceModel | $DeviceType | $UserAgent | $LastSyncTime | $PhoneNumber|"

    }
}


Getting this error:

Unexpected token 'casmbx' in expression or statement.
At C:\Program Files\Microsoft\Exchange Server\v14\scripts\get-addevices.ps1:16 char:36
+ Add-Content -path $Logfile "$casmbx <<<< .name
    + CategoryInfo          : ParserError: (casmbx:String) [], ParseException
    + FullyQualifiedErrorId : UnexpectedToken

I am sure its a minor change but I can't seem to narrow it down.
Avatar of Rajitha Chimmani
Rajitha Chimmani
Flag of United States of America image

You may want to get the value of casmbx.name to a variable to use it with Add-content

$Date = Get-Date -uformat "%Y%m%d"
$Logfile = "C:\logs\activesync-all-$date.txt

$Lst = Get-CASMailbox -ResultSize Unlimited | Where
{$_.HasActiveSyncDevicePartnership - eq $True}

ForEach ($CASMbx in $lst) {
$Devices=$Null
$CASMbxName = $CASMbx.Name
$Devices= Get-ActiveSyncDeviceStatistics -Mailbox $CASMbxName
ForEach ($device in $devices) {
$DeviceModel = $Device.DeviceModel
$DeviceType = $Device.DeviceType
$LastSyncTime = $Device.LastSuccessSync
$PhoneNumber = $Device.DevicePhonenumber
$UserAgent = $Device.DeviceUserAgent
Add-Content -path $Logfile "$casmbxname
|$DeviceModel | $DeviceType | $UserAgent | $LastSyncTime | $PhoneNumber|"

    }
}
Avatar of osiexchange
osiexchange

ASKER

I tried your script and I am now getting the error below:

Unexpected token 'casmbxname' in expression or statement.
At C:\Program Files\Microsoft\Exchange Server\v14\scripts\get-addevices.ps1:17 char:40
+ Add-Content -path $Logfile "$casmbxname <<<<
    + CategoryInfo          : ParserError: (casmbxname:String) [], ParseException
    + FullyQualifiedErrorId : UnexpectedToken
ASKER CERTIFIED SOLUTION
Avatar of Rajitha Chimmani
Rajitha Chimmani
Flag of United States of America image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Hello,

Try this:

$Date = Get-Date -uformat "%Y%m%d"
$Logfile = "C:\logs\activesync-all-$date.txt

$Lst = Get-CASMailbox -ResultSize Unlimited | Where
{$_.HasActiveSyncDevicePartnership - eq $True}

ForEach ($CASMbx in $lst) {
$Devices=$Null
$CASMbxName = $CASMbx.Name
$Devices= Get-ActiveSyncDeviceStatistics -Mailbox $CASMbxName
ForEach ($device in $devices) {
$DeviceModel = $Device.DeviceModel
$DeviceType = $Device.DeviceType
$LastSyncTime = $Device.LastSuccessSync
$PhoneNumber = $Device.DevicePhonenumber
$UserAgent = $Device.DeviceUserAgent
Add-Content -path $Logfile "$($casmbxname) |$($DeviceModel) | $($DeviceType) | $($UserAgent) | $($LastSyncTime) | $($PhoneNumber) |"

    }
} 

Open in new window



JJ