powershell - 用于填充描述字段的 Powershell 脚本
问题描述
我一直在寻找帮助编译我可以手动运行的脚本(不是作为登录脚本),它将查询特定 OU 中的每个计算机帐户,获取当前/上次登录的用户、用户显示名称、IP、系统制造商和型号编号、BIOS 序列号/戴尔服务标签。
看起来像这样的东西:
用户 | 用户显示名 | 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”
}
解决方案
作为以分布式方式在用户上下文中运行的进程的一部分,作为诸如登录脚本之类的东西的一部分或作为简单的另一个触发任务(例如在登录时运行的计划任务以保持最新信息)。
然后在实现类似的东西时,您可以在所述脚本中收集您需要的详细信息并将其发布回计算机 AD 对象。在那里,您的查询不再需要设备在线,也不再需要长时间运行的脚本。
这是您可以部署以在计算机上运行的示例脚本:
# 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("|")
最终,这可用于交付您想要做的事情。
推荐阅读
- ios - 如何更改第二个 UIWindow 的状态栏颜色?
- excel - 将 Excel 选择(包含多个图表)导出到 Outlook
- javascript - nuxt js中的异步组件延迟
- c++ - 将常量减去或添加到大型数组
- python - Visual Studio 代码断点设置为灰色且无法正常工作。错误(可能由于“justMyCode”选项而被排除)
- wordpress - 我该如何解决这个问题 publish_post 挂钩不起作用
- swift - tableview单元格横向collectionview滚动问题
- css - 选择子元素,除了第一个
- c++ - 隐式转换函数的返回对象时是否会影响性能?
- assembly - .word 123:4 在 MIPS 汇编中做了什么,一个冒号和一个数字后面的另一个数字?