Forum Discussion
Powershell 7 - $time is not defined - Script from Microsoft
The only thing I can see with line 3 (which has no bearing on the $time variable) is that you've used an underscore where there shouldn't be one. But this results in a different error to what you're reporting.
It might help if we can see the full script your testing with as "$_" would only make sense when used in a pipeline.
Cheers,
Lain
https://learn.microsoft.com/en-us/archive/technet-wiki/52273.active-directory-how-to-find-inactive-computers-in
- LainRobertsonOct 23, 2024Silver Contributor
Yeah, leave the "$_" out as that is an error.
The native Microsoft code block works fine under PowerShell 7.4.5 for me.
Cheers,
Lain
- Vern_AndersonOct 21, 2024Copper Contributor
To be honest I do not use 7 simply because I am in a Windows environment so I use "Windows PowerShell"
PWSH.exe is PowerShell "Core"
I personally like to write scripts in the same version that my target servers run on. So PowerShell 5.1 is all I use. I mess around with 7 for fun but I do not currently use it in production.
I do not like the way the Active Directory Team wrote their module the filter parameters are always a pain to figure out, and to me is less like PowerShell and more like WQL or SQL. That being said I am curious if any of your AD computers even have a date value stored there. Mine do not (none of them), and the way to find out is that last command I sent.
Get-ADComputer -Filter * | Where-Object {$_.LastLogonTimeStamp}If that returns no output then neither will your script because the date values (if they are like mine) are all NULL.
You could use PowerShell remoting to ask each computer for the last logon date
Get-EventLog -LogName Security | Where-Object { $_.InstanceId -eq 4624 } | Select-Object -First 1 | Format-List TimeGenerated