【问题标题】:Foreach Powershell command for Sky for Business适用于 Sky for Business 的 Foreach Powershell 命令
【发布时间】:2020-02-21 09:56:54
【问题描述】:

我正在 SFB 中创建我的用户报告。只从一个用户那里收集信息,它工作得很好,但是我需要从每个人那里收集它,所以我使用了命令 Get-Content 然后 foreach 来让我的用户群中的每一行都在 users.txt 中注册 如果这是语法错误,有人可以帮助我吗?

谢谢

$users = Get-Content C:\temp\skypeproject\users.txt
$list = foreach ( $user in $users ) 
{ 
    Get-CsOnlineUser -Identity $user | Format-table -AutoSize DisplayName, EnterpriseVoiceEnabled,HostedVoiceMail,OnPremLineURI,LineURI 
}

Error Result

【问题讨论】:

  • 该错误似乎显示.txt文件中的测试内容。检查此项并删除任何测试/虚假条目
  • 我这样做了,是同样的信息

标签: powershell foreach


【解决方案1】:

根据 cmets,由于输入文件中的一些错误数据,您似乎确实遇到了错误。处理此问题的一种方法可能是使用 Try..Catch 块,这样当单个用户导致您出错时,它不会停止整个脚本。

$Users = Get-Content C:\temp\skypeproject\users.txt

$List = foreach ( $User in $Users ) { 
    Try {
        Get-CsOnlineUser -Identity $user -ErrorAction Stop
    }
    Catch {
        "Error for $User: $_"
    }
}

$List | Format-table -AutoSize DisplayName, EnterpriseVoiceEnabled,HostedVoiceMail,OnPremLineURI,LineURI 

我还将Format-Table 的使用移到了循环之外,否则你的$List 变量最终不会包含一个对象,如果你需要对它做任何其他事情时可能会有用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-11-09
    • 1970-01-01
    • 1970-01-01
    • 2016-01-31
    • 1970-01-01
    • 2022-01-23
    • 1970-01-01
    相关资源
    最近更新 更多