Jump to content

PowerShell IF statement

Hi,

 

I've written a basic PowerShell script to import a CSV of SendOnBehalf permissions into 365 and whilst the script works to do this, I want to get a simple report of the permissions that are imported vs those that are not. To do this I've copied the basics of another script I created for FullAccess permissions and modified it as required. What I've found is that the Set-Mailbox cmd is not returning TRUE so my IF statement doesn't work as intended and always goes to the else. The Set-Mailbox cmd also ignores '-erroraction' annoyingly and this helps to tidy up the output. I just want to know which lines are imported vs the ones that arent.

 

Anyone know a way round this? I'm just using Start-Transcript as the report. Doesn't need to be flash!

 

$timeStamp = Get-Date -Format hhmmss

$reportPath = "C:\temp\ImportReports\SendOnBehalf_$timeStamp.txt"

Start-Transcript -Path $reportPath

Import-Csv "C:\temp\userMailboxSendOnBehalf.csv" | ForEach-Object {
    $sendOB = $_.SendOnBehalf
    if (Set-Mailbox -Identity $_.UserPrincipalName -GrantSendOnBehalfTo @{Add="$sendOB"} -erroraction 'silentlycontinue') {
        Write-Host "Success:"$_.SendOnBehalf "granted SendOnBehalf permission to user" $_.UserPrincipalName -ForegroundColor Green
    } else {
        Write-Host "Error:" $_.SendOnBehalf "was NOT granted SendOnBehalf permission to user" $_.UserPrincipalName -ForegroundColor Red
    }
}

Stop-Transcript

Thanks in advance!

 

Bill

Link to comment
Share on other sites

Link to post
Share on other sites

Saw when browsing forums... Curious more than anything, but what happens if you change your code to this (basically instead of if...else, try...catch and a erroraction stop or even not have 'silentlycontinue' but just silentlycontinue without the ' ' )

$timeStamp = Get-Date -Format hhmmss

$reportPath = "C:\temp\ImportReports\SendOnBehalf_$timeStamp.txt"

Start-Transcript -Path $reportPath

Import-Csv "C:\temp\userMailboxSendOnBehalf.csv" | ForEach-Object {
    $sendOB = $_.SendOnBehalf
    try
    {
    	Set-Mailbox -Identity $_.UserPrincipalName -GrantSendOnBehalfTo @{Add="$sendOB"} -erroraction stop
        Write-Host "Success:"$_.SendOnBehalf "granted SendOnBehalf permission to user" $_.UserPrincipalName -ForegroundColor Green
    } catch {
        Write-Host "Error:" $_.SendOnBehalf "was NOT granted SendOnBehalf permission to user" $_.UserPrincipalName -ForegroundColor Red
    }
}

Stop-Transcript

 

Link to comment
Share on other sites

Link to post
Share on other sites

  • 1 month later...

@bluestreak

I think it's because you're missing brackets. Try

 

$(Set-Mailbox -Identity $_.UserPrincipalName -GrantSendOnBehalfTo @{Add="$sendOB"})

instead of

Set-Mailbox -Identity $_.UserPrincipalName -GrantSendOnBehalfTo @{Add="$sendOB"}

In the event that doesn't work you can pipe Get-Mailbox in front of it but I think that'd have the same affect. I'd personally use the $() way though.

 

If that doesn't work I'd break your code down into smaller parts to allow for more extensive testing.

 

RE silentlycontinue not working, can you use -force instead?

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×