Forum Discussion
TJCooper440
Oct 16, 2024Copper Contributor
Powershell 7 - $time is not defined - Script from Microsoft
I get the error "Get-ADComputer: Variable: 'time' found in expression: $time is not defined.". The script is straight from Microsoft and $time is populated with a date/time. It seems very simple, I d...
Vern_Anderson
Oct 21, 2024Copper Contributor
Since it's hard coded in the script anyway why even set the variable to 90? Just set negative 90 right in the parenthesis!
$time = (Get-Date).Adddays(-90)
Get-ADComputer -searchbase "OU=SC,OU=Servers,DC=WSDOT,DC=LOC" -Filter {$_.LastLogonTimeStamp -lt $time} -ResultPageSize 2000 -resultSetSize $null -Properties Name, OperatingSystem, SamAccountName, DistinguishedNameTJCooper440
Oct 21, 2024Copper Contributor
I will try that, but I dont think that is the issue. The $time is correct. It simply wont work when doing the comparison.
- Vern_AndersonOct 21, 2024Copper Contributor
I just now noticed that you have the $_.LastLogonTimeStamp
To my knowledge the $_ indicates an object that was "piped" in I do not see where you piped any objects in.
of course I do not use AD CMDLETs as often as some others but this syntax seems to be the issue here.Get-ADComputer -Filter * | Where-Object {$_.LastLogonTimeStamp -lt [DateTime]$Time}You could try this but when I query my AD computers that property is NULL on every computer, or I may not have rights to see it.
If you do this command does it return any computer objects??Get-ADComputer -Filter * | Where-Object {$_.LastLogonTimeStamp}