I am building a script to assign calendar permissions to specific people. I do this often enough to want a script. This is what i have so far.
#authenticate into the office 365 account
$LiveCred = Get-Credential
#Create new session
$Session = New-PSSession -ConfigurationName Microsoft.Exchange `
-ConnectionUri https://ps.outlook.com/powershell/ `
-Credential $LiveCred `
-Authentication Basic `
-AllowRedirection
#Import Session
Import-PSSession $Session
#Ask for user information
$UserCal = Read-Host -Prompt "Who's calandar are we sharing?"
$HostCal = Read-Host -Prompt "Who's needs access?"
$AccessCal = Read-Host -Prompt "What kind of access?"
#Set access for user
Add-MailboxFolderPermission -Identity ${$UserCal}:\calendar `
-User $HostCal `
-AccessRights $AccessCal
I believe the issue is with the ${$UserCal}:\calendar How do i go about using a variable in this fashion?
FYI I am pretty new to Powershell and scripting. Thanks for any help!
Found the answer to my question after doing much more research!
${$UserCal}:\calendar
Should be
$UserCal`:\calendar
All fixed and working perfect!
Related
I'm attempting to create the ability to connect to my o365 instance through the use of connect-msolservice. Im running into the issue that no matter what I try Im unable to use -Credential to feed it a username and password and avoid a prompt. This is a massive issue as there is literally no point in attempting to automate a process unless it automates the connection too.
Currently what I'm trying to use:
$AdminName = "admin email account"
$Pass = Get-Content "C:\test.txt" | ConvertTo-SecureString
$Cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $AdminName, $Pass
Import-Module MSOnline
Connect-MsolService -Credential $cred
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $cred -Authentication Basic -AllowRedirection
Import-PSSession $Session
Everywhere I check online tells me that this should be possible and for the life of me I cant find anything obvious that I'm doing incorrectly, albeit, all the posts and info I do find are about a year old. This leads me to the question, is this simply no longer supported? If not and hopefully this is the case, what am I doing incorrectly?
I am using the following Syntax, to Migrate user`s and create O365 mailboxes in our organization:
# Mailbox Migration Script
Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010;
$UserCredential = Get-Credential
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $UserCredential -Authentication Basic -AllowRedirection
Import-PSSession $Session
Enable-RemoteMailbox -Identity user.name -PrimarySmtpAddress user.name#company.com -RemoteRoutingAddress user.name#company365.mail.onmicrosoft.com
sleep 30
# After the initial script has done running, run the following:
Get-RemoteMailbox user.name|Set-RemoteMailbox -EmailAddressPolicyEnabled:$true
Everything works ok , but what i would like to do is to convert this, so the data is being read from a CSV file instead so that the actual wont have to be touched.
Im guessing this should be with the import-csv, unfortunately i don`t know what the rest of the syntax should be.
The exact data i need to acquire via csv is the following:
-Identity user.name
-PrimarySmtpAddress user.name#company.com
-RemoteRoutingAddress user.name#company365.mail.onmicrosoft.com
Each part of the data should be acquired from a column in the CSV.
Please assist with creating this script.
Thanks a bunch , in advance to all.
You're right -- Import-CSV is the ticket here.
$ExchangeData = Import-CSV -path C:\MyExchangeData.csv
# Access the data to IMPORT each user.
# Assumes the CSV has "Email, UserName, RemoteEmail" as headers.
foreach ($user in $ExchangeData) {
Enable-RemoteMailbox -Identity $user.UserName -PrimarySmtpAddress $user.Email -RemoteRoutingAddress $user.RemoteEmail
}
So I'm trying to run a script that hides certain email addresses from a client's outlook addressbook.
$Username = "xxx"
$Password = "xxx" | ConvertTo-SecureString -AsPlainText -Force
$UserCredential = New-Object System.Management.Automation.PSCredential -ArgumentList $Username,$Password
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $UserCredential -Authentication Basic -AllowRedirection
Import-PSSession $Session
Get-UnifiedGroup -ResultSize Unlimited | Where-Object {$_.SharePointSiteUrl} | select PrimarySmtpAddress, SharePointSiteUrl | Set-UnifiedGroup -HiddenFromAddressListsEnabled $true
Remove-PSSession $Session
This will hide all the email addresses that have a sharepoint URL connected to them from creating team sites. Anyway, Get-UnifiedGroup works, but when I do the following:
Set-UnifiedGroup -HiddenFromAddressListsEnabled $true
it says this:
Set-UnifiedGroup: The term 'Set-UnifiedGroup' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
I've searched google but can't seem to find a proper solution to my problem... I also did the following commands, to no avail:
Set-ExecutionPolicy RemoteSigned
and
Install-Module MSOnline
I don't know where to look anymore, I'm by not means used to writing Powershell...
Looking at the Microsoft documentation "Set-UnifiedGroup -HiddenFromAddressListsEnabled" does not take pipeline input.
(https://learn.microsoft.com/en-us/powershell/module/exchange/users-and-groups/set-unifiedgroup?view=exchange-ps)
I would suggest trying:
$Username = "xxx"
$Password = "xxx" | ConvertTo-SecureString -AsPlainText -Force
$UserCredential = New-Object System.Management.Automation.PSCredential -ArgumentList $Username,$Password
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $UserCredential -Authentication Basic -AllowRedirection
Import-PSSession $Session
$groups = Get-UnifiedGroup -ResultSize Unlimited | Where-Object {$_.SharePointSiteUrl} | select PrimarySmtpAddress, SharePointSiteUrl
foreach ($group in $groups){
set-unifiedgroup -identity $group.PrimarySmtpAddress -HiddenFromAddressListsEnabled $true
}
Remove-PSSession $Session
With regards to "Set-UnifiedGroup: The term 'Set-UnifiedGroup' is not recognized as the name of a cmdlet"
You will need to add the appropriate role in the exchange admin centre to be able to access the cmdlet.
You will need to be an administrator to make these changes;
https://outlook.office365.com/ecp/?rfr=Admin_o365
Login and select "Permissions" on the left.
I think "Organization Management" should give you the cmdlet you are looking for.
Edit that role and add the user that you are using for the above powershell script to the members list.
This will usually take 30-60 minutes to become effective.
So I'm checking for an approach on a problem I have.
I have an e-mail from my school (Office 365) and I wanted to print the email subject of each email that's located in my inbox with PowerShell.
I already have found the method to lay a connection
$UserCredential = Get-Credential
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://smtp.office365.com/powershell-liveid/ -Credential $UserCredential -Authentication Basic -AllowRedirection
function Connect-O365 {
$session365 = New-PSSession `
-ConfigurationName Microsoft.Exchange `
-ConnectionUri "https://smtp.office365.com/powershell-liveid/" `
-Credential $UserCredential
-Authentication Basic `
-AllowRedirection
Import-Module (Import-PSSession $session365 -AllowClobber) -Global
}
And have found the Get-Mailbox cmdlet.
The problem now however is that I haven't found any real examples or methods that continue to help me printing the email subjects.
I have done quite some research and didn't manage to find something like:
Get-Mailbox -Identity "user" |Select-MailBox * |Where-Object $_.MailBoxName = "Inbox"
Is this not possible or do I have to use another method?
If you have an Office365 subscription you could use the Office 365 api via their graph api endpoint
Since this is basically a REST endpoint you can use the Invoke-Webrequest or Invoke-RestMethod cmdlets.
Or more specificaly the Outlook api.
Both give you json back with you messages content like subject, to, from, and whatever.
I am trying to perform some operations on Exchange online(Office 365) through powershell.
First I created a new powershell session and exported the modules to local as "o365", so that on any later operation I no need to use Import-PsSession to download the required modules
$cred = Get-Credential
$s = New-PSSession -ConfigurationName "Microsoft.Exchange" -ConnectionUri "https://ps.outlook.com/powershell/" -Credential $cred -Authentication Basic -AllowRedirection
Export-PsSession -session $s -outputModule o365
Now, I am creating new session and importing the existing module "o365".
$cred = Get-Credential
$s = New-PSSession -ConfigurationName "Microsoft.Exchange" -ConnectionUri "https://ps.outlook.com/powershell/" -Credential $cred -Authentication Basic -AllowRedirection
Import-Module o365
Get-DistributionGroup
While running the command "Get-DistributionGroup", powershell prompts me to enter the office 365 credentials once again. Is it possible to avoid entering the credentials once again? I don't want to use Import-PsSession, since it takes more time.
It's prompting because you're asking it to each time. I would set up $cred by creating a new object.
#Initiate Get-Credential cmdlet and store inputs
$getcred = Get-Credential
$username = $getcred.UserName
$password = $getcred.Password
$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $username, $password
If you place the above in its own function, you wont have the issue of re-defining the $getcred variable. (That to most is obvious, but thought I'd cover that base)