Me5166
April 1, 2021, 9:52am
1
I tried this in powershell 5 and it works ok:
$days = (get-date).adddays(-90)
Get-aduser -filter {lastlogondate -lt $days} -properties name, lastlogondate
But it doesn’t work in powershell core 7.1. It fails with error:
Get-Aduser: variable: "days" found in expression: $days is not defined
What’s wrong?
Olaf
April 1, 2021, 10:06am
2
Welcome to the forums.
Would you please format your code as code here on the forum?
Here you can read how that works: Guide to Posting Code.
Discourse uses Markdown for text formatting. There is a reference sheet and quick tutorial here: Markdown Reference
You can produce in-line code formatting by wrapping your code in backticks (`). For example, `Get-ChildItem` produces Get-ChildItem.
For larger code blocks, place a line containing three backticks above and below your code. For example
```
# 256-Color Foreground & Background Charts
echo “`n$esc[1;4m256-Color Foreground & Background Charts$esc[0m”
foreach ($fgbg in 38,48) { #…
You can go back and edit your existing post. You don’t have to create a new one.
Thanks in advance.
That’s not a helpful description. What exactly happens? Do you get any errors? If yes - please post them here completely - formatted as code as well.
$days = (get-date).adddays(-90) ; Get-aduser -filter {lastlogondate -lt $days} -properties name, lastlogondate
Works perfect on my workstaion and my DC
Me5166
April 1, 2021, 11:35am
4
What powershell versions are on your workstation and DC?
Sorry:
Both are Powershell 7.1.3
and in WindowsPowershell 5.1.19041.610 it works, too
Olaf
April 1, 2021, 11:42am
6
??? How does it “not work” for you?
Me5166
April 1, 2021, 11:45am
7
It raises an error listed above in my original post in version 7.1.2. Works well in version 5.1
Me5166
April 1, 2021, 11:46am
8
This error
Get-Aduser: variable: "days" found in expression: $days is not defined
Olaf
April 1, 2021, 11:46am
9
Is this the complete code you run?
Me5166
April 1, 2021, 11:48am
10
Yes, it is. Variable definition and cmdlet
Olaf
April 1, 2021, 11:50am
11
Than I don’t have an idea. The code works for me as well with no problems.
I tested it from Windows PowerShell 5.1 and PowerShell 7.1.3 from my local machine.
Me5166
April 1, 2021, 1:32pm
12
Ok, i got the answer here powershell - Get-Aduser -Filter will not accept a variable - Stack Overflow .
So, this one works as supposed to me:
Get-aduser -filter "lastlogondate -lt '$days'" -properties name, lastlogondate
Olaf
April 1, 2021, 1:37pm
13
Very strange. As mentioned above the code works for me just as you posted it in your question.
Anyway thanks for sharing the solution.