首页 文章

PowerShell AD用户

提问于
浏览
-1

我想有一个脚本,它接受AD用户ID的txt文件,指示哪些是"not found",为找到的属性提供 DisplayName 属性并将结果导出到文件 . 那可能吗?

我试过这个,但它没有返回已知的有效用户ID .

$ user = Get-Content“c:\ users \ oh75256 \ desktop \ powershell learning \ userids.txt”$ user | foreach {$ Name =“$ _”$ Searcher = [ADSISearcher]“(displayname = $ Name)”$ Results = $ Searcher.FindOne()if($ Results -eq $ null){“$ Name not in AD”> > C:\ users \ oh75256 \ userstatus.csv} else {$ status =(Get-ADUser $ Name).Enabled if($ status -eq“True”){“$ Name is valid”>> C:\ users \ oh75256 \ userstatus.csv} else {“$ Name无效”>> C:\ users \ oh75256 \ userstatus.csv}}}

这样可行!现在我有两个工作的PowerShell脚本,我想将它们合并为一个 . 我有一个包含4列的txt文件:hostname,lastuser,status,AD . 我想从我的ping脚本开始,它ping主机名,如果ping,它会返回登录用户名 . 如果没有ping,则返回“offline”然后我想将lastuser列作为脱机主机名并检查AD以返回用户名或“未在AD中找到” . 以下是文本文件的示例:pingnames.txt

HostName LastUser状态(离线或SamAccountName)AD(名称或未找到)

NAHNT-1234567 bruteaa
NAHNT-2345678 OH75256
NAHNT-3456789 YD13099
NAHNT-4567891 rh16633
NAHNT-5678912管理员
NAHNT-6789123 xt07718
NAHNT-8912345 AA00718
NAHNT-9123456 LR17114

这是我的ping脚本并获取当前登录用户的脚本 .

function get-avail {
param ( $computername )
$availCheck = New-Object psobject -property @{
ComputerName = $computername
Status = 'Offline'
LogonUser = 'None'
}
$test = Test-Connection -ComputerName $computername -Count 1
-errorAction:SilentlyContinue
if ( $test ) {
$availCheck.Status = 'Online'
$wmics = Get-WmiObject Win32_ComputerSystem -ComputerName $computername
}
if ( $wmics.UserName ) {
$availCheck.LogonUser = $wmics.UserName
}
$availCheck | select ComputerName,Status,LogonUser
}
cat "c:\myfiles\pingnames.txt" | %{ get-avail $_ } | export-csv c:\myfiles\results.csv

这是我在AD中验证userID的脚本

$userProfile = Get-Content "c:\myfiles\userids.txt" |
Select @{Expression={$_};Label="SamAccountName"},
@{Expression={" "};Label="First Name"},
@{Expression={" "};Label="Last Name"},
@{Expression={$False};Label="ExistInAD"}

foreach($user in $userProfile)
{
$Account =  Get-ADUser $user.SamAccountName 
 -erroraction silentlycontinue
If($?)
{
    $user.SamAccountName
    $user.ExistInAD = $True
    $user.'First Name' = $Account.GivenName
    $user.'Last Name' = $Account.Surname
}
Else
{

    "------------------------ $($user.SamAccountName)" 
}
}

$userProfile | export-csv "c:\myfiles\mycsv.csv" -NoTypeInformation

是否可以将它们结合起来?谢谢,凯伦

1 回答

  • 0

    您声明您正在寻找用户ID,但您的代码正在搜索显示名称 .

    我想你想改变这条线:

    $Searcher = [ADSISearcher]"(SamAccountName=$Name)"
    

    如果您需要有关列的其他信息,则需要创建PSObject:

    $User = Get-Content "c:\users\oh75256\desktop\powershell learning\userids.txt"
    $User | foreach {
        $Name = "$_"
        $Searcher = [ADSISearcher]"(SamAccountName=$Name)"
        $Results = $Searcher.FindOne()
        $Object = New-Object -TypeName PSObject
        $Object | Add-Member -Name "SamAccountName" -MemberType NoteProperty -Value $_
        if ($Results -eq $null) 
        {
            $Object | Add-Member -Name "Status" -MemberType NoteProperty -Value "Not in AD"
            $Object | Export-Csv C:\users\oh75256\userstatus.csv -NoTypeInformation -Append
        } 
        else 
        {
            [string]$DisplayName = $Results.Properties.name
            $Object | Add-Member -Name "Name" -MemberType NoteProperty -Value $DisplayName
    
            $status = (Get-ADUser $Name).Enabled
            if ($status -eq "True") 
            {
                $Object | Add-Member -Name "Status" -MemberType NoteProperty -Value "Enabled"
                $Object | Export-Csv C:\users\oh75256\userstatus.csv -NoTypeInformation -Append
            } 
            else 
            {
                $Object | Add-Member -Name "Status" -MemberType NoteProperty -Value "Disabled"
                $Object | Export-Csv C:\users\oh75256\userstatus.csv -NoTypeInformation -Append
            }
        }
    }
    

相关问题