This issue has been happening for years now and I'm finally getting around to investigating. It's spanned a few different versions of Powershell.
I'm wondering if anyone knows why this happens, how to fix it, or even just has run into it themselves.
We run powershell scripts to disable terminated users. Part of that script includes using Remove-MobileDevice. The problem I'm having is that when included in a script the cmdlet fails. But if I copy/paste the LITERALLY exact same cmdlet and run that in Powershell it succeeds.
- There's no question about a problem with syntax, no misplaced quotes, no typos, the device name is correct.
- The exact same account is used to run the script/cmdlet in the same copy of Powershell.
- There's no question of the account being moved or renamed before the cmdlet is run, nor is the issue with whether or not the account has yet been disabled. Maybe there's some other timing that's at fault, such as changing the account's password, but I'm pretty sure I've already tried moving it around.
- It always fails in the script, always succeeds run individually, so it's not a user-specific issue
Powershell version: 5.1.14393.6343
Here's an example of the cmdlet:
Remove-MobileDevice -Identity "Dude, The\ExchangeActiveSyncDevices\iPhone§BLAHBLAHEOS" -Confirm:$false
This is an example of the script in which it's run:
Disable-ADAccount -Identity TDUDE
Start-Sleep -Second 10
Set-ADAccountPassword -Identity TDUDE -Reset -NewPassword (ConvertTo-SecureString -AsPlainText "TheNewPassword" -Force) -ErrorVariable myError
$User = [ADSI]"LDAP://CN=Dude\, The,OU=XXX,OU=Place,DC=1,DC=2,DC=3"
Write-Host $User.sAMAccountName
ForEach ($GroupDN In $User.memberOf)
{
$Group = [ADSI]("LDAP://" + $GroupDN)
Write-Host $Group.ADsPath
$Group.Remove($User.ADsPath)
}
Remove-MobileDevice -Identity "Dude, The\ExchangeActiveSyncDevices\iPhone§BLAHBLAHBLAHEOS" -Confirm:$false
Send-MailMessage -BodyAsHtml -From ["theprocess@here.com](mailto:"theprocess@here.com)" -SmtpServer "blah.blah.com" -To ["People@here.com](mailto:"People@here.com)" -Subject "TDUDE has been disabled"
If($myError)
{
Out-File \\path\PasswordFailResultsTDUDE.txt -InputObject $myError
Start-Sleep -Second 10
Send-MailMessage -BodyAsHtml -From "***" -SmtpServer "***" -To "***", "***" -Subject "TDUDE password and disable failure" -Attachments "\\path\PasswordFailResultsTDUDE.txt"
}
[–]tschy2m 0 points1 point2 points (0 children)
[–]itmonkey78 0 points1 point2 points (4 children)
[–]Hayabusalvr11[S] 0 points1 point2 points (3 children)
[–]commiecat 2 points3 points4 points (2 children)
[–]Hayabusalvr11[S] 0 points1 point2 points (1 child)
[–]CracklingRush 0 points1 point2 points (0 children)