HPlum78 Posted July 3, 2019 Posted July 3, 2019 The following will start PS as another users, so create your service account to run your Tasks/ PowerShell then run the code below to start a PS in the context of that service account. Then use the cred commands posted earlier to store the required creds for logging into your o365 tenant. <# .DESCRIPTION This function loads another powershell session in the context of another user This is to faciliate saving stored creds as that user for scheduled tasks etc .PARAMETER Cred the credential object for the other user .PARAMETER Elevate boolean - do you want the powershell session to be elevated - you should decide this based on whether the scheduled task will be set to run with highest privileges and this should only be done if there is an absolute requirement which needs this #> Function Invoke-UserContext { [CmdletBinding()] Param( [Parameter(Position=0,Mandatory=$true,ValueFromPipeline=$true)][system.Management.Automation.PSCredential]$Cred, [Parameter(Position=1,Mandatory=$false,ValueFromPipeline=$true)] [boolean]$Elevate = $false ) Write-Output $PSBoundParameters ### Check elevate parameter and setup verb as required if($Elevate){$verb="runas"} elseif(!($Elevate)){$verb="open"} try { Write-Output "Loading another PowerShell session in context of user: $($Cred.username). Verb = $verb" &Start-Process powershell.exe -Credential $Cred -NoNewWindow -ArgumentList "Start-Process powershell.exe -verb $verb" } catch { Write-Error -errorrecord $_ } }
TechMonkey Posted July 3, 2019 Posted July 3, 2019 (edited) As usual, no guarantees on this and definitely no guarantee that it is pretty or efficient! Ensure you run the first two lines of code (uncommented) separately first to get an encrypted password file. Then put it somewhere accessible by the script and edit the path in the second line of the Credential block. Do not use an MFA user. I can't see anyway for that to work, although I did spot some talk about an AzureAD bypass auth token so I may investigate that. Create two script files, one with "OoO on" line working and the second with the "OoO off" line enabled. Set up your Scheduled tasks to call either the turn on or turn off script. #Use the below 2 lines before running this script to get an encrypted password file. #$credential = Get-Credential #$credential.Password | ConvertFrom-SecureString | Set-Content "C:\random\ExportedPassword.txt" #Ensure not an MFA account #Credentials $username = "[email protected]" $pwdTxt = Get-Content "C:\random\ExportedPassword.txt" $securePwd = $pwdTxt | ConvertTo-SecureString $credObject = New-Object System.Management.Automation.PSCredential -ArgumentList $username, $securePwd #Import Exchange Online Module Import-Module MSOnline #Connect to Exchange Online Connect-MsolService -Credential $credObject $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $credObject -Authentication Basic -AllowRedirection Import-PSSession $Session -AllowClobber #Set OoO on Set-MailboxAutoReplyConfiguration -Identity [email protected] -AutoReplyState Enabled -InternalMessage "Just testing sorry" -ExternalMessage "Just testing, sorry." #Set OoO off #Set-MailboxAutoReplyConfiguration -Identity [email protected] -AutoReplyState Disabled #Disconnect Remove-PSSession -Session $Session As a separate project I'm going to look at how to loop through and apply against a whole OU or Mailboxes with an attribute so I can set all teaching staff to have their OoO on over the summer holiday. Let me know if there are any issues or if anyone spots a clanger! Edited July 3, 2019 by TechMonkey 2
HPlum78 Posted July 3, 2019 Posted July 3, 2019 (edited) As @TechMonkey has posted will do the trick, I am still going to write the code to look for an event that is in the users calendar and then set the OoO based on that. Then users can be subscribed to the service via a group in AzureAD rather than by OU is my plan. This gives the user more control over the settings and also means that they don't have to contact IT for updates to the service (on that the above code would have to be customised for each user who wanted to consume the service). I will keep posting the code snippets as i am going along with a view to posting the whole script when it is done. The functions posted above will also help wrap a service around your tasks and scripts so outside of this will maybe help. Edited July 3, 2019 by HPlum78
TechMonkey Posted July 3, 2019 Posted July 3, 2019 Something I just found, CodeTwo Out of Office Manager, which may be easier as you can plan ahead. No idea how it works but CodeTwo do good tools. It's one of their free ones too.
enjay Posted July 4, 2019 Author Posted July 4, 2019 Something I just found, CodeTwo Out of Office Manager, which may be easier as you can plan ahead. No idea how it works but CodeTwo do good tools. It's one of their free ones too. At quick glance, that's exactly what I am looking for. If only their web presence or my searching skills were better, we could have saved ourselves some hassle!
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now