PowerShell hashtable question - office 365 - powershell

I need to set OOF message for around 80 users in O365.
I found a cmdlet Set-MailboxAutoReplyConfiguration which I can use to automate the procedure,
and it's looks fine but I'am probably missing something.
Here is the code:
$usersfile = import-csv "C:\Users\Out Of office bulk\Users.csv"
$setmailbox = #{
'Identity' = $usersfile.UserPrincipalName
'AutoReplyState' = 'Scheduled'
'externalaudience' = 'all'
'InternalMessage' = 'I am not here'
'ExternalMessage' = 'I am not here'
'StartTime' = '01/02/2020 01:00:00'
'EndTime' = '02/02/2020 23:00:00'
}
Set-MailboxAutoReplyConfiguration #setmailbox
my issue is with the Identity parameter,
when I run the $setmailbox
I can see that it showing the right UPN from the csv file:
Name Value
---- -----
AutoReplyState Scheduled
externalaudience all
Identity {blah#blah.com, blah2#blah.com}
StartTime 01/02/2020 01:00:00
EndTime 02/02/2020 23:00:00
InternalMessage I am not here
ExternalMessage I am not here
But when I run the script, i'am getting this error:
Cannot process argument transformation on parameter 'Identity'. Cannot convert the "System.Collections.ArrayList" value of type "System.Collections.ArrayList" to type "Microsoft.Exchange.Configuration.Tasks.MailboxLocationIdParameter".
+ CategoryInfo : InvalidData: (:) [Set-MailboxAutoReplyConfiguration], ParameterBindin...mationException
+ FullyQualifiedErrorId : ParameterArgumentTransformationError,Set-MailboxAutoReplyConfiguration
+ PSComputerName : outlook.office365.com
I've tried to change this:
'Identity' = $usersfile.UserPrincipalName
to almost any thing, and it didn't work.
Thanks a lot for your help.
PS:
I know that I can do something like that:
Import-Csv 'C:\Users.csv' | ForEach-Object {
$user = $_."UserPrincipalName"
Set-MailboxAutoReplyConfiguration `
-Identity $user -AutoReplyState Scheduled -StartTime "07/10/2018 01:00:00" `
-EndTime "7/15/2018 23:00:00" -InternalMessage "I am not here" -ExternalMessage "I am not here."
}
but for practice purposes I prefer to do it with the hash table in order to understand it better.
Solution:
thanks to #Lee_Dailey, I didn't noticed that the identity parameter accepts only one user each time, so the solution for multi user is to use the foreach-object.

Related

Configuring bulk Msol users with non-English Values

I've been trying to configure a bulk of Msol users via .csv file with non-English values.
First tried importing the values I want to set from the .csv file and didn't get any error and the user I tried to update didn't get updated.
Then, I tried setting the account by writing the non-English values in Powershell and got an error that mentions my non-English values.
Is there any special syntax for non-English values? something in my code were I tried to import the values from the file is the problem?
Thanks.
The code:
Import-Csv C:\Documents\Changes.csv | Get-MsolUser | Where {$_.UserPrincipalName -eq $_.userprincipalname} | Set-MsolUser -Department $-.class -Office $_.school -Title $_.title
The Error:
Set-MsolUser : A positional parameter cannot be found that accepts argument '×לי××'. At C:\UserChanges1.ps1:5 char:143 + ... ipalname} | Set-MsolUser -Department "מורה" -Office "×לי×× ×¡ ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidArgument: (:) [Set-MsolUser], ParameterBindingException + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.Online.Administration.Automation.SetUse

Use modified property of object as parameter in Powershell for Set-MailPublicFolder

When we create a public folder and mail enable in Exchange Online, the default email address is #domain.onmicrosoft.com
Our folder names are "NNNNN_Folder name" where NNNNN is a 5 digit number.
I would like to set the primary email address of the public folder to NNNNN#domain.com
I have tried many variations of this:
Get-PublicFolder -Recurse -Identity "\X\Y\Z"|
Sort-Object Identity –Descending|
Select-Object -first 4|
Set-MailPublicFolder -PrimarySmtpAddress {$_.name.substring(0,5)+"#domain.com"}
and receive errors about interpreting the resulting email address:
Cannot process argument transformation on parameter 'PrimarySmtpAddress'. Cannot convert value
"$_.name.substring(0,5)+"#domain.com"" to type "Microsoft.Exchange.Data.SmtpAddress". Error: "The email
address "$_.name.substring(0,5)+"#domain.com"" isn't correct. Please use this format: user name, the # sign,
followed by the domain name. For example, tonysmith#contoso.com or tony.smith#contoso.com."
+ CategoryInfo : InvalidData: (:) [Set-MailPublicFolder], ParameterBindin...mationException
+ FullyQualifiedErrorId : ParameterArgumentTransformationError,Set-MailPublicFolder
+ PSComputerName : outlook.office365.com
I also tried setting the EmailAddress of the PublicFolder to NNNNN#domain.com in the same operation.
-EmailAddresses #{$_.name.substring(0,5)+"#domain.com"}
It doesn't seem to be evaluating the argument or I'm missing something else?
If I change Set-MailPublicFolder ... with
% {$_.name.substring(0,5) + "#domain.com"}
I do see the email addresses I am expecting.
Thanks,
Craig.
See this version.
From Microsoft command documentation, the identity parameter is required (see this)
I am also not sure it can take the array and process each individual without specifying a foreach.
See this modified versions.
$PublicFolders = Get-PublicFolder -Recurse -Identity "\X\Y\Z"| Sort-Object Identity –Descending | Select-Object -first 4
$PublicFolders | foreach {
$NewEmail = "$($_.name.substring(0,5))#domain.com"
Write-Host "Settings MailPublicFolder with name $($_.Identity) to $NewEmail" -ForegroundColor Cyan
Set-MailPublicFolder -Identity $_.Identity -PrimarySmtpAddress $NewEmail
}

Powershell connecting and searching multiple domains

I am trying to locate the group memberships for a specified user account. One domain's user account is often a member of a group in the other domains (some domains require different admin account). Using get-QAQgroup, I can successfully search each domain individually, but when I try to loop through the domains, I can only find results in the domain that I am logged into.
#Script to change domains and look for group memberships for a specified user account.
$domains = "dom1.ad.state.company.com","dom2.ad.state.company.com","dom3.ad.state.company.com","dom4.ad.state.company.com","corporate.state.company.com","OddNamedDom.com"
$CRED=GET-CREDENTIAL
$userAcc = read-host "Enter domain\username for Group Membership Search"
foreach ($domain in $domains)
{
write-host "In the domain $domain "," $userAcc is a direct member of..."
Get-QADGroup -service $domain -Credential $cred -Containsmember $userAcc | select name
} #foreach domain
Connect-QADService -Service 'dom1.ad.state.company.com'
When I run the script I get results for dom1 (domain I am logged into) and the rest throw the following errors. I am not sure why the "Ref 1:.." lines are pointing to 'dom1'. I thought that might be the source of issue. I have copied the Powershell output below showing the error messages.
In the domain dom1.ad.state.company.com dom1\brownd2.admin.dom1 is a direct member of...
Name
----
DOM1-G-ITS-DS-Company Services
DOM1PGUELFP00003-Exmerge-R
DOMPGUELFP00003-Exmerge-C
ITSPPTBOSHFS003-FSSHARE-C
Domain Users
In the domain dom2.ad.state.company.com dom1\brownd2.admin.dom1 is a direct member of...
Get-QADGroup : 0000202B: RefErr: DSID-03100742, data 0, 1 access points
ref 1: 'dom1.ad.state.company.com'
At C:\TestScripts\tGet-UserAllMemberships.ps1:24 char:6
+ Get-QADGroup -service $domain -Credential $cred -Containsmember $userAcc | ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Get-QADGroup], DirectoryAccessException
+ FullyQualifiedErrorId : Quest.ActiveRoles.ArsPowerShellSnapIn.DirectoryAccess.DirectoryAccessException,Quest.ActiveRoles.ArsPowerShel
lSnapIn.Powershell.Cmdlets.GetGroupCmdlet
In the domain dom3.ad.state.company.com dom1\brownd2.admin.dom1 is a direct member of...
Get-QADGroup : 0000202B: RefErr: DSID-03100742, data 0, 1 access points
ref 1: 'dom1.ad.state.company.com'
At C:\TestScripts\tGet-UserAllMemberships.ps1:24 char:6
+ Get-QADGroup -service $domain -Credential $cred -Containsmember $userAcc | ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Get-QADGroup], DirectoryAccessException
+ FullyQualifiedErrorId : Quest.ActiveRoles.ArsPowerShellSnapIn.DirectoryAccess.DirectoryAccessException,Quest.ActiveRoles.ArsPowerShel
lSnapIn.Powershell.Cmdlets.GetGroupCmdlet
There is a similar set of errors for each domain that I am checking. I have not posted the full list of error messages.
If I change the order of the domains in the array, the errors and the results of the one successful domain just change order to match the array. I thought that it might just be successful for the first iteration of the loop. Not the case though.
I know that the account is a member of groups in Dom2 and not in any groups in Dom3. If I take the commands out of the foreach loop and run individual for each domain in the console I do get the expected results. Based on the individual results, I had thought that this would be straight forward example to do in a loop, but I am not connecting correctly to the domains.
What can I change?
Here is a solution using System.DirectoryServices.AccountManagement Namespace, adapted to PowerShell from C# code. It's a kind of recursive solution. In Find Recursive Group Membership (Active Directory) using C#, I give a recursive solution (using basic ADSI avaible from PowerShell 1.0) that also works with distribution groups.
# Retreiving a principal context for the administrator on the Global Catalog
Add-Type -AssemblyName System.DirectoryServices.AccountManagement
$domainContext = New-Object DirectoryServices.AccountManagement.PrincipalContext([DirectoryServices.AccountManagement.ContextType]::Domain, "VMESS01:3268" , "administrator", "adminPasswd")
# Retreive the groups
try {
$userPrincipal = [DirectoryServices.AccountManagement.UserPrincipal]::FindByIdentity($domainContext, "jpb")
$groups = $userPrincipal.GetAuthorizationGroups()
foreach($group in $groups)
{
$group.name;
}
}
finally {
$pc.domainContext()
}

Powershell: How to pass variable correctly to command

I apologize if I butchered the terminology for this, and understand I'm very new to PowerShell. I have read over some of the guides and this concept is clearly not getting through to me.
Concept:
I want to remove a mobile device from a user in Exchange 2010
Identify user from input
Create variable from input of the PhoneID
Remove the Phone using phoneID variable
I believe my problem is in how I'm passing this data to the next command. I know the appended "#[Identity " that get's added should be removed and I remember reading something about how when you pass data like this Powershell has no context? Here is my very simple script.
Script
Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010
. $env:ExchangeInstallPath\bin\RemoteExchange.ps1
Connect-ExchangeServer -auto
$PU = Read-Host "Enter Username"
$did = get-activesyncdevice -mailbox $PU | Select-Object identity
Remove-ActiveSyncDevice -Identity $did
Error
My error is as follows, and I've tried to research what I'm doing wrong but I'm just not getting it :-( , I replaced the actual output for the account with XX.
Remove-ActiveSyncDevice : Cannot bind parameter 'Identity'. Cannot convert value "#{Identity=XX" to type
"Microsoft.Exchange.Configuration.Tasks.ActiveSyncDeviceIdParameter". Error: "Cannot convert the "#{Identity=XX}" value of type
"Selected.Microsoft.Exchange.Data.Directory.SystemConfiguration.ActiveSyncDevice" to type "Microsoft.Exchange.Configuration.Tasks.ActiveSyncDeviceIdParameter"."
At line:1 char:35
+ Remove-ActiveSyncDevice -Identity $did
+ ~~~~
+ CategoryInfo : InvalidArgument: (:) [Remove-ActiveSyncDevice], ParameterBindingException
+ FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.Exchange.Management.Tasks.RemoveMobileDevice
Any help or advice on this would be amazing!
When you use Select-Object and give it just one property name, you get and object with just one property. But even though it only has one property, you still have to reference that one property by name:
Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010
. $env:ExchangeInstallPath\bin\RemoteExchange.ps1
Connect-ExchangeServer -auto
$PU = Read-Host "Enter Username"
$did = get-activesyncdevice -mailbox $PU | Select-Object identity
Remove-ActiveSyncDevice -Identity $did.identity

Powershell won't use parameter value from a variable

The following command works perfectly:
Set-Mailbox $mailbox -EmailAddresses SMTP:am#foo.com,asmo#bar.org,toke#foobar.net
Trying to run the same command with the email addresses in a variable crashes.
$SMTPAddresses = "SMTP:am#foo.com,asmo#bar.org,toke#foobar.net”
Set-Mailbox $mailbox -EmailAddresses $SMTPAddress
Error:
Set-Mailbox : Cannot convert 'SMTP:am#foo.com,asmo#bar.org,toke#foobar.net' to the type 'Microsoft.Exchange.Data.ProxyAddressCollection' required by parameter 'EmailAddresses'. The address 'SMTP:am#foo.com,asmo#bar.org,toke#foobar.net' is invalid: The address 'am#klestrup.dk,asmo#bdk.dk,toke#bdk.dk' is not a valid SMTP address.
At line:1 char:39
+ Set-Mailbox $mailbox -EmailAddresses $SMTPAddresses
+ ~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Set-Mailbox], ParameterBindingException
+ FullyQualifiedErrorId : CannotConvertArgument,Microsoft.Exchange.Management.RecipientTasks.SetMailbox
The variable is a string btw.
$SMTPAddresses.GetType()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True String System.Object
Any ideas what is causing this?
The error message is rather self-explanatory. am#foo.com,asmo#bar.org,toke#foobar.net is not an e-mail address. Try it like this:
$SMTPAddresses = 'SMTP:am#foo.com','SMTP:asmo#bar.org','SMTP:toke#foobar.net'
Set-Mailbox $mailbox -EmailAddresses $SMTPAddresses
This is an array of two email address strings:
$SMTPAddresses = "SMTP:am#foo.com","asmo#bar.org,toke#foobar.net”
This is one string of two email addresses joined with a comma:
$SMTPAddresses = "SMTP:am#foo.com,asmo#bar.org,toke#foobar.net”
A peculiarity on Office 365 powershell using the xxx-UnifiedGroups command
If you're constructing the command like this:
$emails = #()
$emails += "SMTP:primary.address#domain.com"
$emails += "smtp:email2#domain.com"
# I'm splatting here but you can use whatever approach you prefer
$params = #{}
$params += #{"EmailAddresses" = $emails}
And you're getting this error, try making sure you include a tenant address like so:
$emails = #()
$emails += "SMTP:primary.address#domain.com"
$emails += "smtp:email2#domain.com"
$emails += "smtp:email#YourTenant.onmicrosoft.com"
$params = #{}
$params += #{"EmailAddresses" = $emails}
Note: Originally I got past the error using escaping, but that was a red herring. After getting full end to end working example it turns out you need a "MOERA" address (one that matches your 365 tenant) but the command gives a false error message about not being able to convert to the type 'Microsoft.Exchange.Data.ProxyAddressCollection'.