PS > $Date = Get-Date\nPS > $Date -gt 140\nTrue\nPS > $Date.Ticks\n637401552502484191\nPS > $Date.Ticks.gettype()\n\nIsPublic IsSerial Name BaseType\n-------- -------- ---- --------\nTrue True Int64 System.ValueType\n<\/code><\/pre>","upvoteCount":1,"datePublished":"2020-11-05T11:45:08.000Z","url":"https://community.spiceworks.com/t/help-on-powershell-in-o365/780991/2","author":{"@type":"Person","name":"Evan7191","url":"https://community.spiceworks.com/u/Evan7191"}},{"@type":"Answer","text":"What exactly is the issue?<\/p>\n
As Evan said, i’d double check your types.<\/p>","upvoteCount":0,"datePublished":"2020-11-05T15:52:49.000Z","url":"https://community.spiceworks.com/t/help-on-powershell-in-o365/780991/3","author":{"@type":"Person","name":"Neally","url":"https://community.spiceworks.com/u/Neally"}},{"@type":"Answer","text":"
Take off the “”. varible type of lastlogon should be int you can check to make sure<\/p>\n
$lastlogon.gettype()<\/p>\n
if (($status -eq “Not Active”) -and ($lastlogon -ge 140))<\/p>","upvoteCount":0,"datePublished":"2020-11-06T20:09:02.000Z","url":"https://community.spiceworks.com/t/help-on-powershell-in-o365/780991/4","author":{"@type":"Person","name":"spiceuser-lhb9l","url":"https://community.spiceworks.com/u/spiceuser-lhb9l"}},{"@type":"Answer","text":"
Also you could just<\/p>\n
$AADdevice | sort lastlogindays | where {$_.status -eq ‘Not Active’ -and $lastlogon -ge 140 } | select -First 1<\/p>","upvoteCount":0,"datePublished":"2020-11-06T20:13:49.000Z","url":"https://community.spiceworks.com/t/help-on-powershell-in-o365/780991/5","author":{"@type":"Person","name":"spiceuser-lhb9l","url":"https://community.spiceworks.com/u/spiceuser-lhb9l"}},{"@type":"Answer","text":"
You could clean this up by adding the $date command at the top so it doesn’t have to run for each user. You could also filter out any accounts that don’t have the Sku you are looking for then you don’t need a foreach to check the License. Because $name is already the UPN you can just keep the rest.<\/p>\n
$AADDevice = Get-MsolUser -UserPrincipalName $name | where {$_.Licenses.AccountSkuID -eq $E3sku } | select -ExpandProperty UserPrincipalName | Get-AzureADUserRegisteredDevice | where { $_.deviceOstype -eq 'Windows'} | select deviceostype, displayname, @{n='UPN';E={$name}}, @{N='Status';E={if ($_.ApproximateLastLogonTimeStamp -le $date){'Not Active'} ELSE{\"active\"}}},@{N='LastLogindays';E={ $_.ApproximateLastLogonTimeStamp }} \n<\/code><\/pre>","upvoteCount":0,"datePublished":"2020-11-06T21:02:28.000Z","url":"https://community.spiceworks.com/t/help-on-powershell-in-o365/780991/6","author":{"@type":"Person","name":"spiceuser-lhb9l","url":"https://community.spiceworks.com/u/spiceuser-lhb9l"}}]}}
Hello All,
I am working on a script, but stuck at a step where I am unable to figure out how to perform an action
$E3sku = "domain:ENTERPRISEPACK"
$csv = Import-Csv -Path "C:\Users\Desktop\upn.csv"
foreach ($name in $csv.userprincipalname)
{
$user =Get-MsolUser -UserPrincipalName $name
[string]$apuser = $name
foreach ($Licenses in $user.Licenses)
{
if ($E3sku -eq $Licenses.AccountSkuId)
{$date= (Get-Date).AddDays(-30)
$AADDevice = Get-AzureADUserRegisteredDevice -ObjectId $name | where { $_.deviceOstype -eq 'Windows'} | select deviceostype, displayname, @{n='UPN';E={$apuser}}, @{N='Status';E={if ($_.ApproximateLastLogonTimeStamp -le $date){'Not Active'} ELSE{"active"}}},@{N='LastLogindays';E={(New-TimeSpan -Start $_.ApproximateLastLogonTimeStamp -End (Get-Date)).Days}}
#$AADDevice
}
}
if (!($AADDevice))
{
Write-Host -ForegroundColor Yellow "$name- no Device Assigned"
}
else
{
#$AADdevice | sort lastlogindays | select -First 1
$lld =($AADdevice | sort lastlogindays | select -First 1)
$lastlogon = $lld.lastlogindays
$status = $lld.status
#Write-Host $status + $lastlogon
if (($status -eq "Not Active") -and ($lastlogon -ge "140"))
{ $lld}
}
}
Now, I am unable to act on condition when the $last logon is greater than 140 for any user, set the OOF message using Set-MailboxAutoReplyConfiguration.
Any leads would be very helpful…thank you in advance
@alexw
2 Spice ups
Evan7191
(Evan7191)
November 5, 2020, 11:45am
2
What data type is $lastlogon? You can check by running this:
$lastlogon.gettype()
My guess is that $lastlogon is a DateTime object, and comparing it to an integer with -gt will not produce the expected results. DateTime values may be converted to ticks in order to compare to an integer, and ticks are typically large integers.
PS > $Date = Get-Date
PS > $Date -gt 140
True
PS > $Date.Ticks
637401552502484191
PS > $Date.Ticks.gettype()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True Int64 System.ValueType
1 Spice up
Neally
(Neally)
November 5, 2020, 3:52pm
3
What exactly is the issue?
As Evan said, i’d double check your types.
Take off the “”. varible type of lastlogon should be int you can check to make sure
$lastlogon.gettype()
if (($status -eq “Not Active”) -and ($lastlogon -ge 140))
Also you could just
$AADdevice | sort lastlogindays | where {$_.status -eq ‘Not Active’ -and $lastlogon -ge 140 } | select -First 1
You could clean this up by adding the $date command at the top so it doesn’t have to run for each user. You could also filter out any accounts that don’t have the Sku you are looking for then you don’t need a foreach to check the License. Because $name is already the UPN you can just keep the rest.
$AADDevice = Get-MsolUser -UserPrincipalName $name | where {$_.Licenses.AccountSkuID -eq $E3sku } | select -ExpandProperty UserPrincipalName | Get-AzureADUserRegisteredDevice | where { $_.deviceOstype -eq 'Windows'} | select deviceostype, displayname, @{n='UPN';E={$name}}, @{N='Status';E={if ($_.ApproximateLastLogonTimeStamp -le $date){'Not Active'} ELSE{"active"}}},@{N='LastLogindays';E={ $_.ApproximateLastLogonTimeStamp }}