简体   繁体   中英

Running PowerShell Command Directly On CMD Gives Index Was Out Of Range Error

The command below is designed to list expired user accounts:

powershell -c "Get-LocalUser | Where-Object { $_.AccountExpires -le (Get-Date) -and $null -ne $_.AccountExpires } | Select-Object Name, AccountExpires"

Running directly on PowerShell seems to run fine, however, when I run the same command via CMD, it gives the error below:

Index was out of range. Must be non-negative and less than the size of the collection. (Parameter 'index').

I believe the issue is related to the $null parameter but I need this to exclude blank matches from the output. Does anyone know a way of fixing this error? Or an alternative method to no match blank output?

You should make things clear when using operators:

powershell -c "Get-LocalUser | Where-Object { ($_.AccountExpires -le (Get-Date)) -and ($null -ne $_.AccountExpires) } | Select-Object Name, AccountExpires"

This works fine for me

It might not be $null , try an empty string instead:

("" -ne $_.AccountExpires)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM