1
votes

I am trying to write a PowerShell script that uses a CSV file as input that will turn off the clutter feature in Office 365. The CSV file has only 1 column and that has the 2 target email addresses that I am using for testing. When I run this script with a read-host line and enter a valid email address it works with no errors. When I use the CSV file errors follow.

Import-Module MSOnline
$LiveCred = Get-Credential 
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/PowerShell  -Credential $LiveCred -Authentication Basic -AllowRedirection 
Import-PSSession -allowclobber $Session

Connect-MsolService -Credential $LiveCred
cd c:\scripts

Write-Host "This tool removes the Clutter feature from O365 "

$Clutter = Import-Csv .\Clutteroff.csv

foreach ($user in $Clutter){
    Set-Clutter -Identity $User -Enable $false
}

When I run this I get the following error :

Cannot process argument transformation on parameter 'Identity'. Cannot convert value "@{[email protected]}" to type "Microsoft.Exchange.Configuration.Tasks.MailboxIdParameter". Error: "Cannot convert hashtable to an object of the following type: Microsoft.Exchange.Configuration.Tasks.MailboxIdParameter. Hashtable-to-Object conversion is not supported in restricted language mode or a Data section."

+ CategoryInfo          : InvalidData: (:) [Set-Clutter], ParameterBindin...mationException
+ FullyQualifiedErrorId : ParameterArgumentTransformationError,Set-Clutter
+ PSComputerName        : ps.outlook.com

Any help would be appreciated and explanations will get extra credit :)

CSV file = User, [email protected], [email protected]

Email addresses are valid.

2
Is your CSV only the one column? - Matt
Set-Clutter -Identity $User.UserID -Enable $false possibly if I can read into your CSV structure. It looks like your -Identity is an object with a userid property. You need to extract the property from the object. You also have an empty -Header which might be a typo. - Matt
-header is a typo from previous attempts to fix the script. So the CSV structure is really simple for this test. The five was built in excel and has 3 entries. These entries are my groups email addresses in cells 1A, 2A, 3A and then saved as a CSV file. $User was just supposed to = the text in 1A, 2A.... - Mike_D
What about my other comments? Did you try my suggestions? - Matt
Is your csv one line or one column? The error in the question shows you have a header of userid which us why I made my suggestion. Interpetting your source data is clearly the issue - Matt

2 Answers

1
votes

Putting all of the items in one line like that is not going to work well with Import-CSV. Import-CSV is suited to a table structure (columns and rows), whereas you are just using a comma-separated list (one row, with an unknown number of columns). If in fact you do have the items on different lines, then please correct the question and I'll change the answer.

To work with the data from a file formatted like that, I would just split it into an ArrayList, and remove the first item because it is "User" and not not an email address:

[System.Collections.ArrayList]$Clutter = (get-content .\Clutteroff.csv).split(",")
$Clutter.RemoveAt(0)

Then you can iterate through the array:

foreach ($user in $Clutter){
    $address = $user.trim()
    {Set-Clutter -Identity $address -Enable $false}
}

For the extra credit, $user in your script was returning a row of key/value pairs to represent columns (keys) and the data in the columns (values). Your error message shows @{[email protected]}, so to return just the email address you would use $user.UserID to return the value for UserID.

-1
votes

i GOT THIS WORKING TO PULL FROM CSV SO ONLY THOSE USERS ARE MODIFIED!! SORRY FOR THE CAPS BUT I AM A TOTAL NOOB AND I COULDNT BELIEVE I GOT THIS TO WORK!!! I am beyond STOKED!! :)

the csv requires no headers, just the email address of the users you want to modify in one column

$Clutter = (Get-Content "pathofyourcsv.csv")
foreach ($User in $Clutter) {
    $address = $User
    Get-Mailbox -Identity $User | Set-Clutter -Enable $false}