admin管理员组文章数量:1315258
So I was working on a script and found an unusual behavior:
$Drives = Get-WmiObject -Class Win32_logicalDisk -Filter 'DriveType=3' | Select -ExpandProperty DeviceID #this will exclude CD drives and shared drives, etc, returning only local logical drives.
$MisconfiguredConfigFiles = [List[object]]::new()
$AllConfigFiles = [List[object]]::new()
foreach ($drive in $Drives) {
$ConfigFiles = dir "$drive\*.config" -Recurse
$AllConfigFiles.Add($ConfigFiles.FullName)
}
If I do $AllConfigFiles.Count
with that code, it gives me the number of drives instead of the number of files. But if I do this:
$Drives = Get-WmiObject -Class Win32_logicalDisk -Filter 'DriveType=3' | Select -ExpandProperty DeviceID #this will exclude CD drives and shared drives, etc, returning only local logical drives.
$MisconfiguredConfigFiles = [List[object]]::new()
$AllConfigFiles = @()
foreach ($drive in $Drives) {
$ConfigFiles = dir "$drive\*.config" -Recurse
$AllConfigFiles += $ConfigFiles.FullName
}
Then, $AllConfigFiles.Count
will return the accurate number of files I was expecting. How can I continue to use the List object (and its correlated "Add" method) instead of an array using the "+=" operator?
So I was working on a script and found an unusual behavior:
$Drives = Get-WmiObject -Class Win32_logicalDisk -Filter 'DriveType=3' | Select -ExpandProperty DeviceID #this will exclude CD drives and shared drives, etc, returning only local logical drives.
$MisconfiguredConfigFiles = [List[object]]::new()
$AllConfigFiles = [List[object]]::new()
foreach ($drive in $Drives) {
$ConfigFiles = dir "$drive\*.config" -Recurse
$AllConfigFiles.Add($ConfigFiles.FullName)
}
If I do $AllConfigFiles.Count
with that code, it gives me the number of drives instead of the number of files. But if I do this:
$Drives = Get-WmiObject -Class Win32_logicalDisk -Filter 'DriveType=3' | Select -ExpandProperty DeviceID #this will exclude CD drives and shared drives, etc, returning only local logical drives.
$MisconfiguredConfigFiles = [List[object]]::new()
$AllConfigFiles = @()
foreach ($drive in $Drives) {
$ConfigFiles = dir "$drive\*.config" -Recurse
$AllConfigFiles += $ConfigFiles.FullName
}
Then, $AllConfigFiles.Count
will return the accurate number of files I was expecting. How can I continue to use the List object (and its correlated "Add" method) instead of an array using the "+=" operator?
1 Answer
Reset to default 3The "direct" answer is Mathias': you are adding a collection, so you should use .AddRange()
.
+=
unpacks the collection automatically but that obviously is a problem if you want to add a collection as a single item :)
That said, the best way to deal with your needs is direct assignment.
# DO NOT USE WMI CMDLETS. It has been Obsoleted snce Powershell 3.
# And completely removed from 6+ .
# Use CIM ones instead.
# Force-casting removes a few hassle in calling the drive later on.
# Specifying the property on Get-CimInstance is mostly superfluous optimization
# in this case: it makes it so the cmdlet only returns that property of the
# device.
# I added it mostly as a Teaching Moment(TM) about Left Side Filtering.
[System.IO.DriveInfo[]]$DriveList = Get-CimInstance -Class Win32_logicalDisk -Filter 'DriveType=3' -Property DeviceID |
Select-Object -ExpandProperty DeviceID
# Force-casted direct assignment FTW.
# If you do NOT need to add\remove items AFTER the collection has been created,
# remove the casting and you'll have a nice Array perfectly fine to use.
[System.Collections.Generic.List[object]]$AllConfigFiles = foreach ($Drive in $DriveList) {
# Evaluate adding -Force, -Depth and -ErrorAction SilentlyContinue .
Get-ChildItem -LiteralPath $Drive -File -Filter '*.config' -Recurse
}
$AllConfigFiles.Count
本文标签: windows serverList objects vs Arrays in PowerShellStack Overflow
版权声明:本文标题:windows server - List objects vs. Arrays in PowerShell - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741979190a2408294.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
$ConfigFiles.FullName
evaluates to an array, change$AllConfigFiles.Add($ConfigFiles.FullName)
to$AllConfigFiles.AddRange($ConfigFiles.FullName)
– Mathias R. Jessen Commented Jan 30 at 11:09$null
- all of which are assignable to[object]
, which is why it never errors :) – Mathias R. Jessen Commented Jan 30 at 11:42