列表中每个用户的PowerShell Set-AdaccountPassword

发布于 2025-02-07 13:33:09 字数 348 浏览 2 评论 0原文

我有一个生成随机密码的函数。我最终希望导入用户名列表,然后在设置密码时为每个名称运行该功能。

Function New-Password {
$Password = $null
Get-RandomCharacters
Scramble-String
Write-Host "$Password"
}

New-Password
$Password

我如何将$密码合并

Set-ADAccountPassword -Reset -NewPassword (ConvertTo-SecureString -AsPlainText "$Password" -Force)

I have a function that generates a randomized password. I would ultimately like to import a list of usernames and then have that function run for each name while setting the password.

Function New-Password {
$Password = $null
Get-RandomCharacters
Scramble-String
Write-Host "$Password"
}

New-Password
$Password

How can I merge the $Password with

Set-ADAccountPassword -Reset -NewPassword (ConvertTo-SecureString -AsPlainText "$Password" -Force)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

維他命╮ 2025-02-14 13:33:09

您可以将开关添加到您的功能,以便在必要时转换。您的完整功能将包括以下内容,并引用您的其他自定义功能:

function Get-RandomCharacters {...}
function Scramble-String {...}

function New-Password {
  [CmdletBinding()]
  param(
    [switch]$converttoSS
  )

  $Password = $null
  $password = Get-RandomCharacters -length 10 -characters 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!"§$%&/()=?}][{@#*+'
  #Write-Host ('{0}' -f $Password)
  if ($converttoSS.IsPresent) {
    $password = ConvertTo-SecureString -String $password -AsPlainText -Force
    $password
  }
  else {
    $password
  }
}

其中new -password -converttoss应返回system.security.securestring

但是我认为有一种更简单的方法可以使用system.web组装来完成此操作。

Function New-Password {
  [CmdletBinding()]
  param(
    [switch]$converttoSS
  )
  $password = $null
  Add-Type -AssemblyName 'System.Web'
  $password = [System.Web.Security.Membership]::GeneratePassword(20, 5)

  if ($converttoSS.IsPresent) {
    $newpass = ConvertTo-SecureString $password -AsPlainText -Force
    Write-Warning ('The secure string is {0}' -f $password)
    $output = $newpass

  }
  else {
    Write-Warning ('The password is {0}' -f $password)
    $output = $password
  }

  return $output
}

使用此测试数据:

id,first_name,last_name,email,manager
1,Kelcy,Dannel,[email protected],Kelcy Dannel
2,Vivia,O'Kynsillaghe,[email protected],Vivia O'Kynsillaghe
3,Valerie,Cartmell,[email protected],Valerie Cartmell
4,Hilary,Checo,[email protected],Hilary Checo
5,Sonya,Isacsson,[email protected],Sonya Isacsson

您可以使用类似于下面的脚本。注意 - 请确保在生产中使用它之前对其进行测试。

Function New-Password {...}

$users = Import-Csv "C:\Downloads\MOCK_DATA.csv"

Foreach ($u in $users) {
  $newpass = New-Password -converttoSS
  Get-ADUser -Identity $user.email | Set-ADAccountPassword -Reset -NewPassword $newpass
  Write-Verbose $newpass
}

You could add a switch to your function to convert it if necessary. Your full function would include something below with a reference to your other custom functions:

function Get-RandomCharacters {...}
function Scramble-String {...}

function New-Password {
  [CmdletBinding()]
  param(
    [switch]$converttoSS
  )

  $Password = $null
  $password = Get-RandomCharacters -length 10 -characters 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!"§$%&/()=?}][{@#*+'
  #Write-Host ('{0}' -f $Password)
  if ($converttoSS.IsPresent) {
    $password = ConvertTo-SecureString -String $password -AsPlainText -Force
    $password
  }
  else {
    $password
  }
}

Where New-Password -converttoSS should return System.Security.SecureString.

But I think there's an easier way to do this with the System.Web assembly.

Function New-Password {
  [CmdletBinding()]
  param(
    [switch]$converttoSS
  )
  $password = $null
  Add-Type -AssemblyName 'System.Web'
  $password = [System.Web.Security.Membership]::GeneratePassword(20, 5)

  if ($converttoSS.IsPresent) {
    $newpass = ConvertTo-SecureString $password -AsPlainText -Force
    Write-Warning ('The secure string is {0}' -f $password)
    $output = $newpass

  }
  else {
    Write-Warning ('The password is {0}' -f $password)
    $output = $password
  }

  return $output
}

Using this test data:

id,first_name,last_name,email,manager
1,Kelcy,Dannel,[email protected],Kelcy Dannel
2,Vivia,O'Kynsillaghe,[email protected],Vivia O'Kynsillaghe
3,Valerie,Cartmell,[email protected],Valerie Cartmell
4,Hilary,Checo,[email protected],Hilary Checo
5,Sonya,Isacsson,[email protected],Sonya Isacsson

You could use a script similar to the one below. Note - Please make sure you test this before using it in production.

Function New-Password {...}

$users = Import-Csv "C:\Downloads\MOCK_DATA.csv"

Foreach ($u in $users) {
  $newpass = New-Password -converttoSS
  Get-ADUser -Identity $user.email | Set-ADAccountPassword -Reset -NewPassword $newpass
  Write-Verbose $newpass
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文