【问题标题】:Send cmdlet output and another variable to a CSV file将 cmdlet 输出和另一个变量发送到 CSV 文件
【发布时间】:2014-02-26 16:31:03
【问题描述】:

我有一个想要为其重置密码的 Office 365 用户列表。我想为每个用户重置密码,然后将用户名和密码输出到 CSV 文件。

使用Set-MsolUserPassword cmdlet 只会返回密码,所以我有点卡住了。

到目前为止,这就是我所拥有的:

[Reflection.Assembly]::LoadWithPartialName("System.Web")
foreach ( $name in $names ) {
    $newPassword = $([System.Web.Security.Membership]::GeneratePassword(9,1))
    Set-MsolUserPassword -UserPrincipalName $name.UserPrincipalName -ForceChangePassword 1 -NewPassword $newPassword -TenantId $tenID
    }

这会返回一长串密码。我想做的是返回一个包含 $name.UserPrincipalName 和 $newPassword 的 CSV 文件。

【问题讨论】:

    标签: powershell azure


    【解决方案1】:

    一种选择是将新密码作为附加属性添加到现有对象,然后选择要导出的 2 个属性:

    [Reflection.Assembly]::LoadWithPartialName("System.Web")
    foreach ( $name in $names ) {
        $newPassword = $([System.Web.Security.Membership]::GeneratePassword(9,1))
        Set-MsolUserPassword -UserPrincipalName $name.UserPrincipalName -ForceChangePassword 1 -NewPassword $newPassword -TenantId $tenID
        $name | Add-Member -MemberType NoteProperty -Name NewPassword -Value $newPassword -PassThru
        }
    
    $names | 
     select UserPrincipalName,NewPassword |
     Export-Csv c:\somedir\somefile.csv
    

    【讨论】:

      【解决方案2】:

      未经测试,但这应该会让您朝着富有成效的方向前进:

      $passwordList = @()
      [Reflection.Assembly]::LoadWithPartialName("System.Web")
      foreach ( $name in $names ) {
      
          $newPassword = $([System.Web.Security.Membership]::GeneratePassword(9,1))
          Set-MsolUserPassword -UserPrincipalName $name.UserPrincipalName -ForceChangePassword 1 -NewPassword $newPassword -TenantId $tenID
          $temp = New-Object PSCustomObject -Property @{'Name' = $name; 'Password' = $newPassword;}
          $passwordList += $temp
          }
      $passwordList | Export-CSV C:\PATH\TO\File.csv -NoTypeInformation
      

      【讨论】:

        【解决方案3】:

        我想出了一种方法,从头开始创建一个全新的表:

        [Reflection.Assembly]::LoadWithPartialName("System.Web")
        $PasswordTable = New-Object system.Data.DataTable "PasswordTable"
        $col1 = New-Object system.Data.DataColumn UserPrincipalName,([string])
        $col2 = New-Object system.Data.DataColumn NewPassword,([string])
        $PasswordTable.Columns.Add($col1)
        $PasswordTable.Columns.Add($col2)
        foreach ( $name in $names ) {
            $newPassword = $([System.Web.Security.Membership]::GeneratePassword(10,1))
            Set-MsolUserPassword -UserPrincipalName $name.UserPrincipalName -ForceChangePassword 1 -NewPassword $newPassword [-TenantId $tenID]
            $row = $PasswordTable.NewRow()
            $row.UserPrincipalName = $name.UserPrincipalName
            $row.NewPassword = $newPassword
            $PasswordTable.Rows.Add($row)
            }
        $PasswordTable | Export-Csv C:\path\to\file.csv
        

        但老实说,我更喜欢 mjolinor 的回答 :)

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-12-01
          • 2014-02-26
          • 1970-01-01
          • 1970-01-01
          • 2018-03-02
          相关资源
          最近更新 更多