powershell - Different time formats in same variable -


i writing script filtering computer accounts in ad. want disable/delete accounts @ least inactive more 90 days . if inactive @ least 90 days should disabled , if inactive more 180 days should deleted

so, dates disabling , deleting

$disbale= (get-date).adddays(-90) $delete = (get-date).adddays(-180) 

now loop through given ou , accounts @ least 90 days inactive, therefore inactive more 180 days, too.

$acc = get-adcomputer -filter {lastlogontimestamp -lt $disable)} -properties lastlogontimestamp,description -searchbase "ou=computer,dc=dom,dc=de" -server dom 

then put them foreach disbale or delete them

foreach ($pc in $acc) {if($pc.lastlogontimestamp -lt $delete){write-host 'delete'} else {write-host 'disable'}} 

but here ran error $pc.lastlogontimestamp of type [int64] , number of 18 digits , $delete in format time. why did same comparison above lastlogontimestamp -lt $disable work , doenst? how can work arround easy?

you should use search-adaccount cmdlet, it's in same active directory module get-adcomputer.

search-adaccount -computersonly -accountinactive -timespan 90.00:00:00 | {$_.enabled} | disable-adaccount  search-adaccount -computersonly -accountinactive -timespan 180.00:00:00 | {$_.enabled} | remove-adcomputer 

as exact script should change from:

 if($pc.lastlogontimestamp -lt $delete) 

to:

if([datetime]::fromfiletime($pc.lastlogontimestamp) -lt $delete) 

Comments

Popular posts from this blog

magento2 - Magento 2 admin grid add filter to collection -

Android volley - avoid multiple requests of the same kind to the server? -

Combining PHP Registration and Login into one class with multiple functions in one PHP file -