簡體   English   中英

直接在 CMD 上運行 PowerShell 命令給出索引超出范圍錯誤

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

下面的命令旨在列出過期的用戶帳戶:

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

直接在 PowerShell 上運行似乎運行良好,但是,當我通過 CMD 運行相同的命令時,它會給出以下錯誤:

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

我相信這個問題與 $null 參數有關,但我需要它來從 output 中排除空白匹配項。 有誰知道解決此錯誤的方法? 還是不匹配空白 output 的替代方法?

使用運算符時應該明確:

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

這對我來說很好

它可能不是$null ,請嘗試使用空字符串:

("" -ne $_.AccountExpires)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM