![私のスクリプトは、各「foreach」ループの後に前のエントリを上書きします](https://rvso.com/image/1555399/%E7%A7%81%E3%81%AE%E3%82%B9%E3%82%AF%E3%83%AA%E3%83%97%E3%83%88%E3%81%AF%E3%80%81%E5%90%84%E3%80%8Cforeach%E3%80%8D%E3%83%AB%E3%83%BC%E3%83%97%E3%81%AE%E5%BE%8C%E3%81%AB%E5%89%8D%E3%81%AE%E3%82%A8%E3%83%B3%E3%83%88%E3%83%AA%E3%82%92%E4%B8%8A%E6%9B%B8%E3%81%8D%E3%81%97%E3%81%BE%E3%81%99.png)
ここで使用しているスクリプトは、必要な情報を書き出します。ただし、有効な新しいエントリがそれぞれ最後のエントリを置き換えていることに気づきました。その理由がわかりません。そのため、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