用于填充描述字段的 Powershell 脚本

Powershell Script to fill the Description field

我一直在寻求编译脚本的帮助,我可以手动 运行(不是登录脚本),它将查询特定 OU 中的每个计算机帐户,获取 current/last 登录用户、用户显示名称、IP、系统制造商和型号、BIOS serial/dell 服务标签。

看起来像这样的东西:

用户 |用户显示名称 | IP地址 |戴尔 Optiplex 3050 | 1ASDFG5

我尝试使用的代码是下面的代码,但我需要更多帮助来获取其余字段并以上面的格式编译它

$computers = Get-ADComputer -Filter * -searchBase "OU=workstations,OU=workstation,DC=mydomain,DC=local"
foreach ($computer in $computers)
{
$vendor = (Get-WMIObject -ComputerName $computer.Name Win32_ComputerSystemProduct).Vendor
$name = (Get-WMIObject -ComputerName $computer.Name Win32_ComputerSystemProduct).Name
$identifyingNumber = (Get-WMIObject -ComputerName $computer.Name Win32_ComputerSystemProduct).IdentifyingNumber
$vendor
$name
$identifyingNumber
Set-ADComputer $computer –Description “$vendor | $name | $identifyingNumber”
}

作为 运行 以分布式方式在用户上下文中作为某些东西的一部分的过程的一部分,这将更好地实现 例如登录脚本 或作为另一个触发的任务(例如 运行 在登录时执行的计划任务,以便使信息保持最新)。

然后在实现类似的东西时,您可以在所述脚本中收集所需的详细信息,然后 post 将其返回到计算机 AD 对象。到达那里后,您的查询不再需要设备在线,也不再是冗长的 运行ning 脚本。

这是您可以部署到 运行 计算机上的示例脚本:

# Function to Set Value on Computer Object
function Set-ADComputerObj
{
    Param ( $ComputerID, $PropertyName, $Value )

    $ComputerSearcher = New-Object DirectoryServices.DirectorySearcher
    $ComputerSearcher.SearchRoot = "LDAP://$("DC=$(($ENV:USERDNSDOMAIN).Replace(".",",DC="))")"
    $ComputerSearcher.Filter = "(&(objectCategory=Computer)(CN=$ComputerID))"

    $computerObj = [ADSI]$ComputerSearcher.FindOne().Path

    $computerObj.Put( $PropertyName, $Value ) 
    $computerObj.SetInfo()

}

# Function to Get Values from User Object
function Get-ADUserObj
{
    Param ( [string]$Identity = $env:USERNAME )
    IF ($Identity)
    {

        $UserSearcher = New-Object DirectoryServices.DirectorySearcher
        $UserSearcher.SearchRoot = "LDAP://$("DC=$(($ENV:USERDNSDOMAIN).Replace(".",",DC="))")"
        $UserSearcher.Filter = "(&(objectCategory=person)(SAMAccountName=$Identity))"

        $UserSearcher.FindOne() | foreach {New-Object PSObject -Property:$_.Properties}
    }
}

#===================================

# SAMAccountName for computer object
$ComputerID = $env:COMPUTERNAME

# SAMAccountName for user object
$UserID = $env:USERNAME

# User AD object
$UserObj = Get-ADUserObj

# Display Name for the current user
$UserDisplayName = $UserObj.displayname

# Get LAN IP Address (may need to be modified to account for multiple adapters)
$IPAddress = (Get-NetIPAddress -InterfaceAlias ((Get-NetAdapter | where {$_.Status -eq "Up" -and ($_.HardwareInterface -eq $true)}).Name) | where {$_.AddressFamily -eq "IPv4"}).IPAddress

# Get Asset Product Details
$Asset = (Get-WMIObject -ComputerName $env:COMPUTERNAME Win32_ComputerSystemProduct)
$AssetVendor = $Asset.Vendor
$AssetName = $Asset.Name
$AssetNumber = $Asset.IdentifyingNumber

# Set Parameter Values for the Set Command on Computer Object
$ComputerObjProperty = "description"
$ComputerObjValue = "$UserID|$UserDisplayName|$IPAddress|$ProductVendor|$ProductName|$ProductNumber"

#===================================

Try
{
    # Set Value on Computer Object
    Set-ADComputerObj -ComputerID $ComputerID -PropertyName $ComputerObjProperty -Value $ComputerObjValue
}
Catch
{
    # Write error exception
    $error[0].Exception
}

此脚本接下来要做的是创建您要求的字符串,并将其放在 AD 计算机对象的描述中。从那里,您可以检索描述字段并按分隔符将其拆分。

例如:

$UserID,$UserDisplayName,$IPAddress,$ProductVendor,$ProductName,$ProductNumber = ((Get-ADComputer $ComputerName).description).split("|")

最终这可以用来交付您想要做的事情。