Advertisement
LSvingen

Office365 Run Scheduled Task With Stored Credentials

Feb 14th, 2019
559
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # Variable declaration
  2. $FilePath = "C:\Scripts\Credentials\O365TenantName.txt"
  3. $Username = "YOURADMIN@YOURTENANT.COM"
  4.  
  5. # Here you should check if modules are loaded and load them
  6. # This is just a sample, adapt/change as needed.
  7. If (!(get-module MSOnline))
  8. {
  9.   Import-Module MSOnline
  10. }
  11.  
  12. # Import the password to use, decrypt it, then build the credential object.
  13. $PasswordText = Get-Content -Path $FilePath
  14. $SecurePassword = $PasswordText | ConvertTo-SecureString
  15. $CredentialObject = New-Object System.Management.Automation.PSCredential -ArgumentList $Username, $SecurePassword
  16.  
  17. # Connect to the Office 365 services you will be using.
  18. Connect-MsolService -Credential $CredentialObject
  19.  
  20. # Run your commands
  21. Get-MsolUser -All
  22.  
  23. #If you need to write to log or write the output you should do this here.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement