【发布时间】:2019-10-03 15:41:17
【问题描述】:
此脚本通过 PowerShell 将域用户添加到任何其他或远程域计算机/系统的“管理员”组。
这会在 csv 中返回包含三列(计算机名称、可用性、状态)的最终状态
我需要在包含时间和日期的输出文件中添加第四列。
#Create a file in the required path and update in the below command line
$Output = "C:\CSV\Output.csv"
#The output field of the computer will blank if the user is already exist in the group
Add-Content -Path $Output -Value "ComputerName,Availability,Status"
$status = $null
$availability = $null
#Save the CSV (Comma seperated) file with the server host name and the username to be added
Import-Csv C:\CSV\Computer.csv | ForEach-Object {
$Computer=$_.Computer
$User=$_.user
if (Test-Connection -ComputerName $Computer -Count 1 -Quiet) {
Write-Verbose "$Computer : Online"
$availability="Oniline"
try {
$GroupObj=[ADSI]"WinNT://$Computer/Administrators,group"
$GroupObj.PSBase.Invoke("Add",([ADSI]"WinNT://jdom.edu/$User").Path)
$status="Success"
#Update the status in the output file
Add-Content -Path $Output -Value ("{0},{1},{2}" -f $Computer, $availability, $status)
} catch {
Write-Verbose "Failed"
}
} else {
Write-Warning "$Computer : Offline"
$availability = "Offline"
$status = "failed"
#Update the status in the output file
Add-Content -Path $Output -Value ("{0},{1},{2}" -f $Computer, $availability, $status)
}
}
这是输出文件的样子,这是我想在第四列添加日期和时间的地方:
计算机名称、可用性、状态 TD123696WJN339P,在线,成功 TD123419WJN339P,在线,成功 计算机名称、可用性、状态 5VERF9097LTIO01,离线,失败 ZF001024DJH706G,离线,失败 5MICF9017LTIO01,离线,失败【问题讨论】:
标签: powershell