여기에 있는 스크립트는 내가 원하는 정보를 기록합니다. 그러나 나는 각각의 새로운 유효한 항목이 마지막 항목을 대체한다는 것을 확인했습니다. 이유를 알 수 없습니다. 따라서 1000대의 컴퓨터가 감지되더라도 매번 항목이 1개만 남게 됩니다. 이 문제를 해결하는 데 도움을 주세요.
$computers = Get-ADComputer -SearchBase "DC=some,DC=web,DC=com" -Filter * | Select-Object -ExpandProperty Name
foreach ($computer in $computers){
if(!(Test-Connection -CN $computer -BufferSize 16 -Count 1 -ea 0 -quiet))
{write-host "Cannot reach $computer offline." -f red}
else {
$outtbl = @()
Try{
$sr=Get-WmiObject -Class Win32_BIOS -ComputerName $computer -ErrorAction Continue
$Xr=Get-WmiObject -Class Win32_Processor -ComputerName $computer -ErrorAction Continue
$ld=Get-ADComputer $computer -Properties * -ErrorAction Continue
$r="{0} GB" -f ((Get-WmiObject Win32_PhysicalMemory -ComputerName $computer |Measure-Object Capacity -Sum).Sum / 1GB)
$x = gwmi win32_computersystem -ComputerName $computer |select @{Name = "Type";Expression = {if (($_.pcsystemtype -eq '2') )
{'Laptop'} Else {'Desktop Or Other something else.'}}},Manufacturer,@{Name = "Model";Expression = {if (($_.model -eq "$null") ) {'Virtual'} Else {$_.model}}},username -ErrorAction Continue
$t= New-Object PSObject -Property @{
ServiceTag = $sr.serialnumber
ComputerName = $ld.name
IPV4Address=$ld.ipv4Address
Enabled=$ld.Enabled
Description=$ld.description
OU=$ld.DistinguishedName.split(',')[1].split('=')[1]
Type = $x.type
Manufacturer=$x.Manufacturer
Model=$x.Model
RAM=$R
ProcessorName=($xr.name | Out-String).Trim()
NumberOfCores=($xr.NumberOfCores | Out-String).Trim()
NumberOfLogicalProcessors=($xr.NumberOfLogicalProcessors | Out-String).Trim()
AddressWidth=($xr.Addresswidth | Out-String).Trim()
OperatingSystem=$ld.operatingsystem
OperatingSystemServicePack=$ld.OperatingSystemServicePack
OperatingSystemVersion=$ld.OperatingSystemVersion
OperatingSystemHotfix=$ld.OperatingSystemHotfix
LastLogonDate=$ld.lastlogondate
ObjectCreated=$ld.Created
ObjectModified=$ld.whenChanged
LoggedInUser=$x.username
}
$outtbl += $t
}
catch [Exception]
{
"Error communicating with $computer, skipping to next"
}
$outtbl | select Computername,ServiceTag,IPV4Address,Description,Enabled,OU,Type,Manufacturer,Model,RAM,ProcessorName,NumberOfCores,NumberOfLogicalProcessors,AddressWidth,OperatingSystem,OperatingSystemServicePack,OperatingSystemVersion,OperatingSystemHotfix,ObjectCreated,ObjectModified,LoggedInUser,LastLogonDate | Format-Table * -Wrap -AutoSize | Out-String -Width 4096 | Out-File $env:USERPROFILE\Desktop\AD-Inventory.txt
}
}
답변1
문제는 모든 루프에서 파일을 덮어 썼다는 것입니다. 이제 스크립트는 먼저 모든 것을 변수에 저장한 다음 마지막에 내보냅니다.
스크립트에 몇 가지 문제가 더 있습니다.
- 종료 오류가 없기 때문에 try/catch는 현재 아무것도 잡을 수 없습니다.
- 스크립트 형식이 형편없었습니다. 형식을 더 잘 지정해 주세요. 그렇지 않으면 읽기가 매우 어렵습니다.
- 왜 내보내기 전에 결국 모든 것을 선택합니까? 모든 것을 내보내려면 항목을 선택할 필요가 없습니다. 정렬 목적인 경우
New-Object PSObject -property
로 변경[pscustomobject]
하면 이미 올바른 정렬 순서가 적용됩니다.
업데이트된 스크립트는 다음과 같습니다
$computers = Get-ADComputer -SearchBase "DC=some,DC=web,DC=com" -Filter * | Select-Object -ExpandProperty Name
$outtbl = foreach ($computer in $computers){
if(!(Test-Connection -CN $computer -BufferSize 16 -Count 1 -ea 0 -quiet))
{
write-host "Cannot reach $computer offline." -f red
}
else {
Try{
$sr = Get-WmiObject -Class Win32_BIOS -ComputerName $computer -ErrorAction Continue
$Xr = Get-WmiObject -Class Win32_Processor -ComputerName $computer -ErrorAction Continue
$ld = Get-ADComputer $computer -Properties * -ErrorAction Continue
$r = "{0} GB" -f ((Get-WmiObject Win32_PhysicalMemory -ComputerName $computer | Measure-Object Capacity -Sum).Sum / 1GB)
$x = gwmi win32_computersystem -ComputerName $computer | select @{Name = "Type";Expression = {if (($_.pcsystemtype -eq '2') )
{'Laptop'} Else {'Desktop Or Other something else.'}}},Manufacturer,@{Name = "Model";Expression = {if (($_.model -eq "$null") ) {'Virtual'} Else {$_.model}}},username -ErrorAction Continue
New-Object PSObject -Property @{
ServiceTag = $sr.serialnumber
ComputerName = $ld.name
IPV4Address=$ld.ipv4Address
Enabled=$ld.Enabled
Description=$ld.description
OU=$ld.DistinguishedName.split(',')[1].split('=')[1]
Type = $x.type
Manufacturer=$x.Manufacturer
Model=$x.Model
RAM=$R
ProcessorName=($xr.name | Out-String).Trim()
NumberOfCores=($xr.NumberOfCores | Out-String).Trim()
NumberOfLogicalProcessors=($xr.NumberOfLogicalProcessors | Out-String).Trim()
AddressWidth=($xr.Addresswidth | Out-String).Trim()
OperatingSystem=$ld.operatingsystem
OperatingSystemServicePack=$ld.OperatingSystemServicePack
OperatingSystemVersion=$ld.OperatingSystemVersion
OperatingSystemHotfix=$ld.OperatingSystemHotfix
LastLogonDate=$ld.lastlogondate
ObjectCreated=$ld.Created
ObjectModified=$ld.whenChanged
LoggedInUser=$x.username
}
}
catch [Exception]
{
"Error communicating with $computer, skipping to next"
}
}
}
$outtbl | select Computername,ServiceTag,IPV4Address,Description,Enabled,OU,Type,Manufacturer,Model,RAM,ProcessorName,NumberOfCores,NumberOfLogicalProcessors,AddressWidth,OperatingSystem,OperatingSystemServicePack,OperatingSystemVersion,OperatingSystemHotfix,ObjectCreated,ObjectModified,LoggedInUser,LastLogonDate | Format-Table * -Wrap -AutoSize | Out-String -Width 4096 | Out-File $env:USERPROFILE\Desktop\AD-Inventory.txt