I have error IndexOf when i tried to run the following powershell script. Any advise
$unlicensedUsers = Get-MsolUser -UnlicensedUsersOnly
foreach ($aUser in $unlicensedUsers)
{
if ($unlicensedUsers.IndexOf($aUser) % 10 -eq 0) {
Write-Host -ForegroundColor yellow $unlicensedUsers.IndexOf($aUser)
}
}
Error:
IndexOf : Method invocation failed because System.Object[]] does not contain a method name indexOf
IndexOf()
is a method on the List<T>
type, which is usually not used in PowerShell. Most of the time the variables that you use in foreach
are going to be object arrays, as in your example, or some other type of collection. Object arrays don't have an equivalent method, so you'll have to hold your own copy of the array index:
$unlicensedUsers = Get-MsolUser -UnlicensedUsersOnly
for ($i = 0; $i -lt $unlicensedUsers.count; $i++)
{
if ($i % 10 -eq 0) {
Write-Host -ForegroundColor yellow $i
}
}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments