我有一个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文件。
答案 0 :(得分:3)
一个选项是将新密码作为附加属性添加到现有对象,然后选择要导出的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
答案 1 :(得分:0)
未经测试,但这应该让你朝着富有成效的方向前进:
$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
答案 2 :(得分:0)
我想通过从头开始创建一个全新的表来实现这一目标:
[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的答案:)