访问PowerShell Import-CSV重新调整的数据

时间:2016-05-12 17:36:53

标签: csv powershell office365

我正在尝试编写一个PowerShell脚本,该脚本使用CSV文件作为输入,将关闭Office 365中的混乱功能.CSV文件只有1列,并且具有我用于测试的2个目标电子邮件地址。当我使用读取主机行运行此脚本并输入有效的电子邮件地址时,它可以正常工作。当我使用CSV文件时出现错误。

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
}

当我运行时,我收到以下错误:

  

无法处理参数'Identity'的参数转换。无法将值“@ {UserID=xxxxx@myCompany.com}”转换为“Microsoft.Exchange.Configuration.Tasks.MailboxIdParameter”。错误:“不能   将哈希表转换为以下类型的对象:Microsoft.Exchange.Configuration.Tasks.MailboxIdParameter。受限语言模式或数据不支持Hashtable-to-Object转换   节“。

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

任何帮助将不胜感激,解释将获得额外的信誉:)

CSV文件=用户,XXXX @ MyCompany.com,YYYY @ MyCompany.com

电子邮件地址有效。

2 个答案:

答案 0 :(得分:1)

将所有项目放在一行中就不能与Import-CSV一起使用。 Import-CSV适用于表结构(列和行),而您只使用逗号分隔列表(一行,列数未知)。如果实际上你有不同行的项目,那么请更正问题,我会改变答案。

要使用格式化的文件中的数据,我只需将其拆分为ArrayList,然后删除第一项,因为它是" User"而不是电子邮件地址:

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

然后你可以遍历数组:

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

对于额外的功劳,脚本中的$user返回一行键/值对来表示列(键)和列中的数据(值)。您的错误消息显示为@ {UserID=xxxxx@myCompany.com},因此要仅返回您将使用$user.UserID返回UserID值的电子邮件地址。

答案 1 :(得分:-1)

我把这个工作从CSV中拉出来只有这些用户才能被修改!!对CAPS感到厌烦,但我是一个总的NOOB,我相信我认为这是工作!我超越了STOKED !! :)

csv不需要标题,只需要在一列中修改用户的电子邮件地址

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