1

下面我有一个 PS 命令来获取一些有用的用户信息。我还想从带有 PC 名称的 csv 文件中获得输入。我怎样才能给所有东西相同的格式?

PC 名称后的冒号与我从get-aduser. 有人可以建议我该怎么做吗?

我所拥有的示例

Clear-Host
do {

write-Host " Enter the user ID: " -ForegroundColor Cyan -NoNewline
$UserName = Read-Host
Write-Host ""


Get-ADUser -Identity $Username -Properties * |  Select-Object  DisplayName,  mail, LastLogonDate, LastBadPasswordAttempt, AccountExpirationDate, PasswordLastSet , OfficePhone, Department, Manager
    

$Processes = Import-CSV 'C:\Users\w0vlnd\Desktop\Powershells\Computers in a specific workgroup or domain.csv'
$processes | where "User ID" -like $Username | Select-Object "PC name" 


net user $Username /domain | find "Password expires" 
net user $Username /domain | find "Password changeable" 
write-host "`n"


} while ($Username -notcontains $Processes)
4

1 回答 1

0

您可以构建一个对象,将 AD 用户与来自 CSV 的信息和来自net user. 这是一个如何处理它的示例:

$csv = Import-CSV 'path\to\csvhere.csv'
$ADprops = @(
    'DisplayName'
    'mail'
    'LastLogonDate'
    'LastBadPasswordAttempt'
    'AccountExpirationDate'
    'PasswordLastSet'
    'OfficePhone'
    'Department'
    'Manager'
)

$filter = @(
    $ADprops
    @{
        Name = 'PC Name'
        Expression = { $pcName }
    }, @{
        Name = 'Password Changeable'
        Expression = { $changeable }
    }, @{
        Name = 'Password Expires'
        Expression = { $expires }
    }
)

Clear-Host
do
{
    Write-Host " Enter the user ID: " -ForegroundColor Cyan -NoNewline
    $UserName = Read-Host
    Write-Host ""

    $pcName = $csv.Where({ $_."User ID" -match $Username })."PC Name"
    $expires, $changeable = net user $Username /domain |
    Select-String -Pattern 'Password Changeable|Password Expires' |
    ForEach-Object { ($_ -split '\s{2,}')[1] }
    Get-ADUser -Identity $Username -Properties $ADprops |
    Select-Object $filter

} while ($csv.'User ID' -notcontains $Username)
于 2021-12-28T01:01:54.597 回答