script enable litigationhold

Iron Contributor

Hello

 

I have the below script. The script brings back the correct users, but when i go to enable litigationhold, its enabling it for all users, and i want to enable it for only the users that currently are set to $false for lithold

 

Any help is greatly appreciated

$pimserviceaccount = "admin.shofmann@mycompany.onmicrosoft.com"
$adminpassword = "01000000d08c9ddf0115d1118c7a00c04fc297eb0100000084b78e199e1c3b478efd799c7aab6a910000000002000000000003660000c0000000100000000d19998b9fdab9ec1f44820c65de76fd0000000004800000a000000010000000dce05d1d250d5882967e46b97ecbd0c218000000ab0ceed45aa0a431a0137017b3479b6a6ea8e9694b41327714000000d1b19a01c628ceffedd753a5e2b53d0cd90fb930"
$secureString = ConvertTo-SecureString -String $adminpassword
$cred = New-Object -TypeName PSCredential -ArgumentList $pimserviceaccount, $secureString
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $cred -Authentication Basic -AllowRedirection
Import-PSSession $Session -DisableNameChecking
#Import-Module msonline
#connect-msolservice -credential $cred
$Lithold = Get-MsolUser -all:$true | Where-Object {($_.licenses).AccountSkuId -match "SPE_E5" -or ($_.licenses).AccountSkuId -match "EXCHANGEARCHIVE_ADDON" -or ($_.licenses).AccountSkuId -match "SPE_F1" -or ($_.licenses).AccountSkuId -match "SPE_F5_SECCOMP"} |select UserPrincipalname
$lithold |ForEach-Object {Get-Mailbox -Identity $_.userprincipalname} | Where-Object {$_.litigationholdenabled -eq $false}
$Lithold |ForEach-Object {set-mailbox -identity $_.userprincipalname -litigationholdenabled:$true}
2 Replies

Hi, @Skipster311-1 

 

How about using IF sentecne? 

 

foreach ($user in $Lithold)
{
    If((Get-Mailbox -Identity $user.userPrincipalName).LitigationHoldEnabled -eq $false)
    {
    Set-Mailbox -Identity $user.userPrincipalName -LitigationHoldEnabled $true
    }
}

@Skipster311-1 

 

I think the results from below command line are not being stored in any variable to take further actions. This does not change the contents of variable "$lithold" and hence the Set command in next line takes action on all the users.   

 

$lithold |ForEach-Object {Get-Mailbox -Identity $_.userprincipalname} | Where-Object {$_.litigationholdenabled -eq $false}

 

You can surely go with the "if" statement or save the results of above command in another separate variable to action.