We just recently added a domain to our accepted domains and e-mail address policy. While many of our users follow that policy, we have a third domain that some users have as their primary address that does not follow an address policy. I need to take those users and add an SMTP Address in the format of firstname.lastname#domain2.com.
E.g., john.doe#domain3.com (primary) has john.doe#domain1.com as an alias and needs to have john.doe#domain2.com added as an SMTP Address.
I have the following code but am receiving errors:
$Users = Get-Mailbox -ResultSize Unlimited | Where-Object {($_.PrimarySMTPAddress -like "*domain3.com*)}
foreach ($a in $Users) {
$b = Get-User $a.Primary.SMTPAddress
$a.EmailAddresses.Add("$($b.Firstname + "." + $b.Lastname)#domain2.com")
}
$Users |%{Set-Mailbox $_.PrimarySMTPAddress -EmailAddresses $_.EmailAddresses
Errors are as follows:
Cannot process argument transformation on parameter 'Identity'. Cannot convert the "john.doe#domain3.com" value of type "Microsoft.Exchange.Data.SmtpAddress" to type "Microsoft.Exchange.Configuration.Tasks.UserIdParameter".
+ CategoryInfo : InvalidData: (:) [Get-User], ParameterBindin...mationException
+ FullyQualifiedErrorId : ParameterArgumentTransformationError,Get-User
Exception calling "Add" with "1" argument(s): "The address '.#domain2.com' is invalid: ".#domain2.com" isn't a valid SMTP address. The domain name can't contain spaces and it has to have a prefix and a suffix, such as example.com."
At C:\_scripts\SmtpAdd.ps1:4 char:23
+ $a.emailaddresses.Add <<<< ("$($b.Firstname + "." + $b.LastName)#domain2.com")
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException
Cannot process argument transformation on parameter 'Identity'. Cannot convert the "john.due#domain3.com" value of type "Microsoft.Exchange.Data.SmtpAddress" to type "Microsoft.Exchange.Configuration.Tasks.MailboxIdParameter".
+ CategoryInfo : InvalidData: (:) [Set-Mailbox], ParameterBindin...mationException
+ FullyQualifiedErrorId : ParameterArgumentTransformationError,Set-Mailbox
Thanks
I think the problem starts with this line:
$b = Get-User $a.Primary.SMTPAddress
Get-User fails to return a valid user as $a.Primary.SMTPAddress doesn't return a type that Get-User can use. That then causes EmailAddresses.Add to fail as $b is null. Set-Mailbox fails for the same reason as Get-User
Try this instead (you'll also need to do this on the Set-Mailbox line):
$b = Get-User $a.Primary.SMTPAddress.ToString()
Related
command is ((Get-WindowsFeature | Format-List|out-string).Trim().replace(':', '=').replace('', '\') -split '(\r?\n){2,}') and
Is there a way to put all multi-lines into a single for selected Name's like only to "Description"
Name = AD-Certificate
DisplayName = Active Directory Certificate Services
Description = Active Directory Certificate Services (AD CS) is used to create certification authorities and related role services that allow you
to issue and manage certificates used in a variety of applications.
Installed = False
Error:
PS C:\Users\Administrator> ((Get-WindowsFeature | Format-List|out-string).Trim().replace(':', '=').replace('\', '\\') -split '(\r?\n){2,}') |where-object {![string]::IsNullOrWhiteSpace($_)}|convertfrom-stringdata|convertto-json
convertfrom-stringdata : Data line ' to issue and manage certificates used in a variety of applications.' is not in **'name=value'**
format.
At line:1 char:165
+ ... ct {![string]::IsNullOrWhiteSpace($_)}|convertfrom-stringdata|convert ...
+ ~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [ConvertFrom-StringData], PSInvalidOperationException
+ FullyQualifiedErrorId : InvalidOperation,Microsoft.PowerShell.Commands.ConvertFromStringDataCommand
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
I wrote this a few months ago to loop through a few domains and a userlist and give me a readout of how many emails from the listed domain each user got. Now I'm getting some error that is SUPER weird and I just don't know what it's saying.
Here's the code and the error
[long]$IntSent=0
[long]$IntRec=0
[long]$IntTotal=0
$startdate="06/09/2018 00:00:01"
$enddate="06/12/2018 23:59:59"
$domains=#(REDACTED) #etc
$users=#(REDACTED)
ForEach ($user in $users) {
foreach ($domain in $domains) {
get-messagetrackinglog -start $startdate -End $enddate -Recipients $users -resultsize unlimited -EventID Receive | where {[string]$_.sender -like "*#$domain"} |ForEach{$IntRec++}
#|Where {[String]$_.recipients -notlike "*#contoso4.com*"}
}
}
And the error:
The server software doesn't support the type of search requested.
+ CategoryInfo : InvalidOperation: (:) [Get-MessageTrackingLog], LocalizedException
+ FullyQualifiedErrorId : 6220E2A4,Microsoft.Exchange.Management.TransportLogSearchTasks.GetMessageTrackingLog
+ PSComputerName : MUHSERVER
The server software doesn't support the type of search requested.
+ CategoryInfo : InvalidOperation: (:) [Get-MessageTrackingLog], LocalizedException
+ FullyQualifiedErrorId : 6220E2A4,Microsoft.Exchange.Management.TransportLogSearchTasks.GetMessageTrackingLog
+ PSComputerName : MUHSERVER
The server software doesn't support the type of search requested.
+ CategoryInfo : InvalidOperation: (:) [Get-MessageTrackingLog], LocalizedException
+ FullyQualifiedErrorId : 6220E2A4,Microsoft.Exchange.Management.TransportLogSearchTasks.GetMessageTrackingLog
+ PSComputerName : MUHSERVER
Thoughts? I have no idea where to look here.
This issue is most likely related with too many proxy addresses. As you can see here (not in the answer but below):
if the mailbox has more than 49 proxy addresses entered within the Email addresses tab the search command will fail
This would explain why it was working before and isn't working now. While in general the solution would be to check if any user has more than 49 proxy addresses, this might not be the case here.
In your ForEach you use
-Recipients $users
so basically for each user you do the search for messages sent to all users from your list. What you probably wanted to use is (notice lack of s)
-Recipients $user
Also, if I might suggest something (as a good habit) - I usually use one character variables while iterating using ForEach like this:
ForEach ($u in $users)
ForEach ($d in $domains)
With that habit you minimize the likelihood of using incorrect variable in ForEach as in that script.
I am new-ish to PowerShell and am having a problem. I am trying to get a users password last set date and time from a domain and then add 90 days to it. So far I have:
$PLS_date = (get-aduser randomuser -Properties PasswordLastSet | Select passwordlastset) | out-string
"Password will expire on $((get-date).adddays(90)($PLS_date))"
However this spits out an error:
+ "Password will expire on $((get-date).adddays(90)($PLS_date))"
+ ~
Unexpected token '(' in expression or statement.
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : UnexpectedToken
I have search around to try and find what i am doing wrong but cannot figure it out.
Help!
Try it this way:
$pwdLastSet = Get-ADUser username -Properties PasswordLastSet | Select-Object -ExpandProperty PasswordLastSet
if ( $pwdLastSet ) {
"Password will expire on: {0}" -f $pwdLastSet.AddDays(90)
}
I wrote a small script to grab event log entries from a remote machine and write it to a .csv file. The script works when targeting a single machine, but when I try to implement a for loop and loop it over all machines in Active Directory, I get this error:
Method invocation failed because [Microsoft.ActiveDirectory.Management.ADComputer]
does not contain a method named 'op_Addition'.
At Y:\srp.ps1:7 char:143
+ ... | Export-Csv $($computer + ".csv")
+ ~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (op_Addition:String) [], RuntimeException
+ FullyQualifiedErrorId : MethodNotFound
Export-Csv : Cannot validate argument on parameter 'Path'. The argument is null or empty.
Provide an argument that is not null or empty, and then try the command again.
At Y:\srp.ps1:7 char:141
+ ... 0 | Export-Csv $($computer + ".csv")
+ ~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Export-Csv],
ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,
Microsoft.PowerShell.Commands.ExportCsvCommand
The error indicates there's a problem with the Export-Csv command, but running the command by itself creates the log files needed. Here is the full script, for reference:
# Gets SRP event log entries from remote machine and writes them to a .csv file
# of the same name.
Write-Output "Running..."
$computers = Get-ADComputer -filter {(Name -like "PC*") -or (Name -like "LT*")}
foreach ($computer in $computers) {
Get-EventLog -LogName Application -Source Microsoft-Windows-SoftwareRestrictionPolicies
-ComputerName $computer -Newest 10 | Export-Csv $($computer + ".csv")
} #end foreach
Write-Host "Done."
Any ideas as to why this error appears when I try to loop over computers in AD?
It looks like Get-ADComputer returns ADComputer objects, but you're passing it to Get-EventLog's ComputerName parameter, which takes a string, as-is. I'm assuming you'll need to grab the name property from the Microsoft.ActiveDirectory.Management.ADComputer object.