Hello. I have got the following to find iems in Azure Ad that are over 90 days old from Microsoft. I then wanted to add exceptions as there will be some devices that I want to keep so came up with the following which works.<\/p>\n
$dt = (Get-Date).AddDays(-90)\nGet-AzureADDevice -All:$true | Where {($_.ApproximateLastLogonTimeStamp -le $dt) -and ($_.DisplayName -notcontains \"TestPC01\")} | select-object -Property AccountEnabled, DeviceId, DeviceOSType, DeviceOSVersion, DisplayName, DeviceTrustType, ApproximateLastLogonTimestamp | export-csv C:\\90days-summary.csv -NoTypeInformation\n\n<\/code><\/pre>\n
Advertisement
All good. Then I tried to import a .csv file to make list for exceptions as that should make things easier nbut the following doesn’t work and all I can think of is that it should do
<\/p>\n
$dt = (Get-Date).AddDays(-90)\n$exceptions = Import-Csv -Delimiter \",\" -LiteralPath \"C:\\90daysexceptions.csv\"\n$exceptions\nGet-AzureADDevice -All:$true | Where {($_.ApproximateLastLogonTimeStamp -le $dt) -and ($_.DisplayName -notcontains $exceptions)} | select-object -Property AccountEnabled, DeviceId, DeviceOSType, DeviceOSVersion, DisplayName, DeviceTrustType, ApproximateLastLogonTimestamp | export-csv C:\\90days-summary.csv -NoTypeInformation\n\n<\/code><\/pre>\n
Advertisement
I printed the $exception variable tomake sure that it is reading and it does. I called the column name in the csv DisplayName as well so that it matched the value I am looking for in the -notcontains.<\/p>\n
I suppose I can add lots of ANDS for each device but can anyone see why the csv list is not working please?<\/p>","upvoteCount":3,"answerCount":3,"datePublished":"2021-09-16T13:51:42.000Z","author":{"@type":"Person","name":"kenobi421","url":"https://community.spiceworks.com/u/kenobi421"},"acceptedAnswer":{"@type":"Answer","text":"
I’d try -notmatch and use regex syntax<\/p>\n
You have to make the CSV look like this:<\/p>\n
$_.DisplayName -notmatch \"exception1|exception2|exception3\"\n<\/code><\/pre>\nWithout knowing what your CSV looks like, it’s tough to help.<\/p>\n
in general contains / notcontains checks arrays, so you’d swap it.<\/p>\n
$exceptions -notcontains $_.DisplayName\n<\/code><\/pre>","upvoteCount":2,"datePublished":"2021-09-16T13:57:52.000Z","url":"https://community.spiceworks.com/t/powershell-where-and/811363/2","author":{"@type":"Person","name":"Neally","url":"https://community.spiceworks.com/u/Neally"}},"suggestedAnswer":[{"@type":"Answer","text":"Hello. I have got the following to find iems in Azure Ad that are over 90 days old from Microsoft. I then wanted to add exceptions as there will be some devices that I want to keep so came up with the following which works.<\/p>\n
$dt = (Get-Date).AddDays(-90)\nGet-AzureADDevice -All:$true | Where {($_.ApproximateLastLogonTimeStamp -le $dt) -and ($_.DisplayName -notcontains \"TestPC01\")} | select-object -Property AccountEnabled, DeviceId, DeviceOSType, DeviceOSVersion, DisplayName, DeviceTrustType, ApproximateLastLogonTimestamp | export-csv C:\\90days-summary.csv -NoTypeInformation\n\n<\/code><\/pre>\nAll good. Then I tried to import a .csv file to make list for exceptions as that should make things easier nbut the following doesn’t work and all I can think of is that it should do
<\/p>\n
$dt = (Get-Date).AddDays(-90)\n$exceptions = Import-Csv -Delimiter \",\" -LiteralPath \"C:\\90daysexceptions.csv\"\n$exceptions\nGet-AzureADDevice -All:$true | Where {($_.ApproximateLastLogonTimeStamp -le $dt) -and ($_.DisplayName -notcontains $exceptions)} | select-object -Property AccountEnabled, DeviceId, DeviceOSType, DeviceOSVersion, DisplayName, DeviceTrustType, ApproximateLastLogonTimestamp | export-csv C:\\90days-summary.csv -NoTypeInformation\n\n<\/code><\/pre>\nI printed the $exception variable tomake sure that it is reading and it does. I called the column name in the csv DisplayName as well so that it matched the value I am looking for in the -notcontains.<\/p>\n
I suppose I can add lots of ANDS for each device but can anyone see why the csv list is not working please?<\/p>","upvoteCount":3,"datePublished":"2021-09-16T13:51:42.000Z","url":"https://community.spiceworks.com/t/powershell-where-and/811363/1","author":{"@type":"Person","name":"kenobi421","url":"https://community.spiceworks.com/u/kenobi421"}},{"@type":"Answer","text":"
Thanks Neally. Your post put me on the right path. I think it is the case that is causing the issues so I did the is instead and avoided it altogether:<\/p>\n
$exceptions = “testpc01|testpc03”<\/p>\n
That worked using -not match<\/p>\n
Many thanks.
<\/p>","upvoteCount":0,"datePublished":"2021-09-17T07:02:23.000Z","url":"https://community.spiceworks.com/t/powershell-where-and/811363/3","author":{"@type":"Person","name":"kenobi421","url":"https://community.spiceworks.com/u/kenobi421"}}]}}