Neally Bot<\/li>\n<\/ul>","upvoteCount":1,"datePublished":"2021-11-16T21:03:30.000Z","url":"https://community.spiceworks.com/t/powershell-error-i-do-not-understand/817138/3","author":{"@type":"Person","name":"Neally","url":"https://community.spiceworks.com/u/Neally"}},{"@type":"Answer","text":"hahahaha Word strikes again! I’ve been updating the documentation in Word, but using Notepad++ for actually typing up the codes.<\/p>","upvoteCount":1,"datePublished":"2021-11-16T21:05:13.000Z","url":"https://community.spiceworks.com/t/powershell-error-i-do-not-understand/817138/4","author":{"@type":"Person","name":"jsepeta-rush","url":"https://community.spiceworks.com/u/jsepeta-rush"}},{"@type":"Answer","text":"
Proper formatting would possibly have helped you see the issue, as Evan said, the parameter was not expected where it was<\/p>\n
Import-CSV \"C:\\temp\\users-output.csv\" | \nForEach-object {\n Get-Recipient -Identity $_.Name -ResultSize Unlimited | \n Select-object PrimarySmtpAddress \n} | \nExport-CSV \"C:\\temp\\resume.csv\" -Encoding UTF8 -NoTypeInformation\n<\/code><\/pre>","upvoteCount":1,"datePublished":"2021-11-16T21:07:56.000Z","url":"https://community.spiceworks.com/t/powershell-error-i-do-not-understand/817138/5","author":{"@type":"Person","name":"Neally","url":"https://community.spiceworks.com/u/Neally"}},{"@type":"Answer","text":"However, the command has not finished running, and there were only 200 names in the users-output.csv file.<\/p>\n
I just checked the resume.csv file, and it’s got 11718 SMTP addresses. Something is wrong.<\/p>","upvoteCount":0,"datePublished":"2021-11-16T21:30:49.000Z","url":"https://community.spiceworks.com/t/powershell-error-i-do-not-understand/817138/6","author":{"@type":"Person","name":"jsepeta-rush","url":"https://community.spiceworks.com/u/jsepeta-rush"}},{"@type":"Answer","text":"
What does your CSV look like? Does it have a ‘name’ header?<\/p>\n
Keep in mind you are querying Exchange 200 times, that might take a hot minute<\/p>\n
e.g.<\/p>\n
name\nneally\njsepeta\n<\/code><\/pre>\n$csv = import-csv \"C:\\temp\\users-output.csv\"\n\n$report = \nforeach($row in $csv){\n $PSMTP = \n try{\n Get-Recipient -Identity $row.Name -ResultSize Unlimited -ErrorAction Stop\n }\n catch{\n $Null\n }\n\n $PSMTP.PrimarySmtpAddress\n}\n\n$report | \nExport-CSV \"C:\\temp\\resume.csv\" -NoTypeInformation -Encoding UTF8\n<\/code><\/pre>","upvoteCount":0,"datePublished":"2021-11-16T21:59:18.000Z","url":"https://community.spiceworks.com/t/powershell-error-i-do-not-understand/817138/7","author":{"@type":"Person","name":"Neally","url":"https://community.spiceworks.com/u/Neally"}},{"@type":"Answer","text":"\n\n
<\/div>\n
jsepeta-rush:<\/div>\n
\nHowever, the command has not finished running, and there were only 200 names in the users-output.csv file.<\/p>\n
I just checked the resume.csv file, and it’s got 11718 SMTP addresses. Something is wrong.<\/p>\n<\/blockquote>\n<\/aside>\n
If it’s still running, you should not have your C:\\temp\\resume.csv file yet. Something is definitely wrong, as Get-Recipient -Identity $_.Name only gets data for that individual. Which begs the question, since you seem to be looking name-by-name from your .CSV, why -ResultSize Unlimited? Or are there any names in your .CSV that include wildcards?<\/p>\n
If I do “Get-Recipient -Identity Jim6795” it just gets me, but “Get-Recipient -Identity J*” gets every mail-enabled object starting with ‘J’ in my AD.<\/p>\n
For starters, this will dump all your SMTP Addresses:<\/p>\n
Get-Recipient | Select Name, SAMAccountName, PrimarySMTPAddress\n<\/code><\/pre>\nUsing the -Filter parameter will let you cull the ones you don’t want:<\/p>\n
Get-Recipient -Filter \"RecipientType -eq 'UserMailbox'\" | Select Name, SAMAccountName, PrimarySMTPAddress\n<\/code><\/pre>\n(Note the Filter syntax: quotes around the whole Filter, apostrophes around the Value)<\/p>\n
You can add -Filter parameters to narrow your scope:<\/p>\n
Get-Recipient -Filter \"(RecipientType -eq 'UserMailbox') -and (Alias -like '*')\" | Select Name, SAMAccountName, RecipientType\n<\/code><\/pre>\nYou can use Get-Recipient jsepeta-rush | FL * to see all the data at your disposal. \nHow is it going for you? Made progress? More questions?<\/p>","upvoteCount":2,"datePublished":"2021-11-17T16:32:54.000Z","url":"https://community.spiceworks.com/t/powershell-error-i-do-not-understand/817138/8","author":{"@type":"Person","name":"jimlong3","url":"https://community.spiceworks.com/u/jimlong3"}},{"@type":"Answer","text":"
If you are a powershell noob, and this is your first command… um, congratulations! You’ve graduated!<\/p>\n
I definitely wasn’t able to come up with a long pipeline command like this… until after quite a bit of … powershell experimental fun!<\/p>\n
Do you have some other programming in your background?<\/p>\n
Regards,<\/p>\n
Jeff Cummings<\/p>\n
MIS Technician<\/p>","upvoteCount":0,"datePublished":"2021-11-17T21:23:55.000Z","url":"https://community.spiceworks.com/t/powershell-error-i-do-not-understand/817138/9","author":{"@type":"Person","name":"jeffreycummings","url":"https://community.spiceworks.com/u/jeffreycummings"}}]}}
I’m a PowerShell noob, but I’ve used Exchange for over 2 decades. I’m trying to follow some instructions a coworker has left me to migrate mailboxes to the cloud, and the step I’m on requires me to build an SMTP list from the SAM account name.
Import-CSV C:\temp\users-output.csv | ForEach {Get-Recipient -Identity $_.Name | Select PrimarySmtpAddress} -ResultSize Unlimited | Export-CSV C:\temp\resume.csv -Encoding UTF8 -NTI
When I attempt to run this in PowerShell, I get the following error:
ForEach-Object : Cannot bind parameter ‘RemainingScripts’. Cannot convert the “-ResultSize” value of type
“System.String” to type “System.Management.Automation.ScriptBlock”.
At line:1 char:39
… utput.csv | ForEach {Get-Recipient -Identity $_.Name | Select Primary …
CategoryInfo : InvalidArgument: ( [ForEach-Object], ParameterBindingException
FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.ForEachObjectCommand
Do you know what I’m doing wrong? Do you have any suggestions on how to create a list of SMTP addresses from a list of SAM account names?
5 Spice ups
Evan7191
(Evan7191)
November 16, 2021, 9:01pm
2
It looks like the -ResultSize parameter got moved outside of the loop. Try this:
Import-CSV C:\temp\users-output.csv |
ForEach {Get-Recipient -Identity $_.Name -ResultSize Unlimited | Select PrimarySmtpAddress} |
Export-CSV C:\temp\resume.csv -Encoding UTF8 -NTI
4 Spice ups
Neally
(Neally)
November 16, 2021, 9:03pm
3
Welcome
If you post code, please use the ‘Insert Code’ button. Please and thank you!
Hi, and welcome to the PowerShell forum!
Don’t apologize for being a “noob” or “newbie” or “n00b.” There’s just no need – nobody will think you’re stupid, and the forums are all about asking questions. Just ask!
Use a descriptive subject. Don't say "Need help" or "PowerShell Help", actually summarize what the problem is. It helps the rest of us keep track of which problem is which.
Don’t post massive scripts. We’re all volunteers and we don’t have time to read all that, nor will we copy, past…
1 Spice up
hahahaha Word strikes again! I’ve been updating the documentation in Word, but using Notepad++ for actually typing up the codes.
1 Spice up
Neally
(Neally)
November 16, 2021, 9:07pm
5
Proper formatting would possibly have helped you see the issue, as Evan said, the parameter was not expected where it was
Import-CSV "C:\temp\users-output.csv" |
ForEach-object {
Get-Recipient -Identity $_.Name -ResultSize Unlimited |
Select-object PrimarySmtpAddress
} |
Export-CSV "C:\temp\resume.csv" -Encoding UTF8 -NoTypeInformation
1 Spice up
However, the command has not finished running, and there were only 200 names in the users-output.csv file.
I just checked the resume.csv file, and it’s got 11718 SMTP addresses. Something is wrong.
Neally
(Neally)
November 16, 2021, 9:59pm
7
What does your CSV look like? Does it have a ‘name’ header?
Keep in mind you are querying Exchange 200 times, that might take a hot minute
e.g.
name
neally
jsepeta
$csv = import-csv "C:\temp\users-output.csv"
$report =
foreach($row in $csv){
$PSMTP =
try{
Get-Recipient -Identity $row.Name -ResultSize Unlimited -ErrorAction Stop
}
catch{
$Null
}
$PSMTP.PrimarySmtpAddress
}
$report |
Export-CSV "C:\temp\resume.csv" -NoTypeInformation -Encoding UTF8
jimlong3
(Jim6795)
November 17, 2021, 4:32pm
8
jsepeta-rush:
However, the command has not finished running, and there were only 200 names in the users-output.csv file.
I just checked the resume.csv file, and it’s got 11718 SMTP addresses. Something is wrong.
If it’s still running, you should not have your C:\temp\resume.csv file yet. Something is definitely wrong, as Get-Recipient -Identity $_.Name only gets data for that individual. Which begs the question, since you seem to be looking name-by-name from your .CSV, why -ResultSize Unlimited? Or are there any names in your .CSV that include wildcards?
If I do “Get-Recipient -Identity Jim6795” it just gets me, but “Get-Recipient -Identity J*” gets every mail-enabled object starting with ‘J’ in my AD.
For starters, this will dump all your SMTP Addresses:
Get-Recipient | Select Name, SAMAccountName, PrimarySMTPAddress
Using the -Filter parameter will let you cull the ones you don’t want:
Get-Recipient -Filter "RecipientType -eq 'UserMailbox'" | Select Name, SAMAccountName, PrimarySMTPAddress
(Note the Filter syntax: quotes around the whole Filter, apostrophes around the Value)
You can add -Filter parameters to narrow your scope:
Get-Recipient -Filter "(RecipientType -eq 'UserMailbox') -and (Alias -like '*')" | Select Name, SAMAccountName, RecipientType
You can use Get-Recipient jsepeta-rush | FL * to see all the data at your disposal.
How is it going for you? Made progress? More questions?
2 Spice ups
If you are a powershell noob, and this is your first command… um, congratulations! You’ve graduated!
I definitely wasn’t able to come up with a long pipeline command like this… until after quite a bit of … powershell experimental fun!
Do you have some other programming in your background?
Regards,
Jeff Cummings
MIS Technician