Apologies, wasn’t sure how to title this. Thanks to Neally, I have a script that somewhat works. The only issue I’m having is exporting the results for all the PC’s in the list.<\/p>\n
$computers = Get-Content \\\\server\\Common\\Drives\\list.TXT\nForEach ($Computer in $Computers) {\nGet-PhysicalDisk |\nselect-object $computer,FriendlyName,MediaType,HealthStatus,@{n='size';e={[math]::Round($_.size / 1gb)}}|\nExport-Csv \\\\server\\Common\\Drives\\list.csv -NoTypeInformation\n}\n<\/code><\/pre>\n
If I run this script as is, it will only name the bottom entry in the list.TXT file with my PC’s hard drive specifics, so I’m guessing the select-object is incorrect. Could someone assist please? Thank you.<\/p>","upvoteCount":7,"answerCount":8,"datePublished":"2021-06-09T20:11:59.000Z","author":{"@type":"Person","name":"Jackal-Lear","url":"https://community.spiceworks.com/u/Jackal-Lear"},"acceptedAnswer":{"@type":"Answer","text":"
e.g.<\/p>\n
$computers = Get-Content \"\\\\server\\Common\\Drives\\list.TXT\"\n\n$report = \nForEach ($Computer in $Computers) {\n if(Test-Connection $computer -Count 1 -Quiet){\n $remoteMachine = \n invoke-command -computername $computer -ScriptBlock {\n Get-PhysicalDisk\n }\n foreach($disk in $remoteMachine){\n [pscustomobject]@{\n ComputerName = $computer\n FriendlyName = $disk.FriendlyName\n MediaType = $disk.MediaType\n HealthStatus = $disk.HealthStatus\n Size =[math]::Round($disk.size / 1gb)\n }\n }\n }\n else{\n [pscustomobject]@{\n ComputerName = $computer\n FriendlyName = '-'\n MediaType = '-'\n HealthStatus = '-'\n Size = '-'\n }\n }\n}\n\n$report |\nExport-Csv \"\\\\server\\Common\\Drives\\list.csv\" -NoTypeInformation\n\n<\/code><\/pre>","upvoteCount":2,"datePublished":"2021-06-09T20:33:13.000Z","url":"https://community.spiceworks.com/t/powershell-get-content-foreach/802318/5","author":{"@type":"Person","name":"Neally","url":"https://community.spiceworks.com/u/Neally"}},"suggestedAnswer":[{"@type":"Answer","text":"Apologies, wasn’t sure how to title this. Thanks to Neally, I have a script that somewhat works. The only issue I’m having is exporting the results for all the PC’s in the list.<\/p>\n
$computers = Get-Content \\\\server\\Common\\Drives\\list.TXT\nForEach ($Computer in $Computers) {\nGet-PhysicalDisk |\nselect-object $computer,FriendlyName,MediaType,HealthStatus,@{n='size';e={[math]::Round($_.size / 1gb)}}|\nExport-Csv \\\\server\\Common\\Drives\\list.csv -NoTypeInformation\n}\n<\/code><\/pre>\nIf I run this script as is, it will only name the bottom entry in the list.TXT file with my PC’s hard drive specifics, so I’m guessing the select-object is incorrect. Could someone assist please? Thank you.<\/p>","upvoteCount":7,"datePublished":"2021-06-09T20:11:59.000Z","url":"https://community.spiceworks.com/t/powershell-get-content-foreach/802318/1","author":{"@type":"Person","name":"Jackal-Lear","url":"https://community.spiceworks.com/u/Jackal-Lear"}},{"@type":"Answer","text":"
easy way → you have to append the CSV<\/p>\n
Export-Csv \\\\server\\Common\\Drives\\list.csv -NoTypeInformation -append\n<\/code><\/pre>","upvoteCount":0,"datePublished":"2021-06-09T20:23:09.000Z","url":"https://community.spiceworks.com/t/powershell-get-content-foreach/802318/2","author":{"@type":"Person","name":"Neally","url":"https://community.spiceworks.com/u/Neally"}},{"@type":"Answer","text":"better way → assign output to a variable and then export it<\/p>\n
$computers = Get-Content \"\\\\server\\Common\\Drives\\list.TXT\"\n\n$report = \nForEach ($Computer in $Computers) {\n Get-PhysicalDisk |\n select-object $computer,FriendlyName,MediaType,HealthStatus,\n @{n='size';e={[math]::Round($_.size / 1gb)}}\n}\n\n$report | Export-Csv \"\\\\server\\Common\\Drives\\list.csv\" -NoTypeInformation\n<\/code><\/pre>","upvoteCount":0,"datePublished":"2021-06-09T20:24:14.000Z","url":"https://community.spiceworks.com/t/powershell-get-content-foreach/802318/3","author":{"@type":"Person","name":"Neally","url":"https://community.spiceworks.com/u/Neally"}},{"@type":"Answer","text":"oh also<\/p>\n
Get-PhysicalDisk\n<\/code><\/pre>\nonly works on the local machine you run it against, but you want data from remote computers? \nthat needs a good chunk more code, you need to invoke the command on the remote machine.<\/p>","upvoteCount":0,"datePublished":"2021-06-09T20:26:07.000Z","url":"https://community.spiceworks.com/t/powershell-get-content-foreach/802318/4","author":{"@type":"Person","name":"Neally","url":"https://community.spiceworks.com/u/Neally"}},{"@type":"Answer","text":"\n\n
<\/div>\n
Neally:<\/div>\n
\noh also<\/p>\n
Get-PhysicalDisk\n<\/code><\/pre>\nonly works on the local machine you run it against, but you want data from remote computers? \nthat needs a good chunk more code, you need to invoke the command on the remote machine.<\/p>\n<\/blockquote>\n<\/aside>\n
That would explain why it’s only showing my PC’s details, thank you for that. I’ll look into the invoke-command functionality.<\/p>","upvoteCount":0,"datePublished":"2021-06-09T20:34:22.000Z","url":"https://community.spiceworks.com/t/powershell-get-content-foreach/802318/6","author":{"@type":"Person","name":"Jackal-Lear","url":"https://community.spiceworks.com/u/Jackal-Lear"}},{"@type":"Answer","text":"\n\n
<\/div>\n
Neally:<\/div>\n
\ne.g.<\/p>\n
$computers = Get-Content \"\\\\server\\Common\\Drives\\list.TXT\"\n\n$report = \nForEach ($Computer in $Computers) {\n if(Test-Connection $computer -Count 1 -Quiet){\n $remoteMachine = \n invoke-command -computername $computer -ScriptBlock {\n Get-PhysicalDisk\n }\n foreach($disk in $remoteMachine){\n [pscustomobject]@{\n ComputerName = $computer\n FriendlyName = $disk.FriendlyName\n MediaType = $disk.MediaType\n HealthStatus = $disk.HealthStatus\n Size =[math]::Round($disk.size / 1gb)\n }\n }\n }\n else{\n [pscustomobject]@{\n ComputerName = $computer\n FriendlyName = '-'\n MediaType = '-'\n HealthStatus = '-'\n Size = '-'\n }\n }\n}\n\n$report |\nExport-Csv \"\\\\server\\Common\\Drives\\list.csv\" -NoTypeInformation\n\n<\/code><\/pre>\n<\/blockquote>\n<\/aside>\nThanks again, Neally, really appreciate your responses. This works beautifully.<\/p>","upvoteCount":0,"datePublished":"2021-06-09T20:37:07.000Z","url":"https://community.spiceworks.com/t/powershell-get-content-foreach/802318/7","author":{"@type":"Person","name":"Jackal-Lear","url":"https://community.spiceworks.com/u/Jackal-Lear"}},{"@type":"Answer","text":"
Just a small comment<\/p>\n
I am not sure what you are doing with this line<\/p>\n
select-object $computer,FriendlyName,MediaType,HealthStatus,@{n='size';e={[math]::Round($_.size / 1gb)}}|\n\n<\/code><\/pre>\nSelect–object, as you are using it here, filters the properties you add from the original objects, and creates a new property on the fly.<\/p>\n
However, using $Computer does not do anything as I suspect a computer name is not a property of the input object.<\/p>\n
What are you trying to achieve??<\/p>","upvoteCount":0,"datePublished":"2021-06-10T11:07:53.000Z","url":"https://community.spiceworks.com/t/powershell-get-content-foreach/802318/8","author":{"@type":"Person","name":"DoctorDNS","url":"https://community.spiceworks.com/u/DoctorDNS"}}]}}
Apologies, wasn’t sure how to title this. Thanks to Neally, I have a script that somewhat works. The only issue I’m having is exporting the results for all the PC’s in the list.
$computers = Get-Content \\server\Common\Drives\list.TXT
ForEach ($Computer in $Computers) {
Get-PhysicalDisk |
select-object $computer,FriendlyName,MediaType,HealthStatus,@{n='size';e={[math]::Round($_.size / 1gb)}}|
Export-Csv \\server\Common\Drives\list.csv -NoTypeInformation
}
If I run this script as is, it will only name the bottom entry in the list.TXT file with my PC’s hard drive specifics, so I’m guessing the select-object is incorrect. Could someone assist please? Thank you.
7 Spice ups
Neally
(Neally)
June 9, 2021, 8:23pm
2
easy way → you have to append the CSV
Export-Csv \\server\Common\Drives\list.csv -NoTypeInformation -append
Neally
(Neally)
June 9, 2021, 8:24pm
3
better way → assign output to a variable and then export it
$computers = Get-Content "\\server\Common\Drives\list.TXT"
$report =
ForEach ($Computer in $Computers) {
Get-PhysicalDisk |
select-object $computer,FriendlyName,MediaType,HealthStatus,
@{n='size';e={[math]::Round($_.size / 1gb)}}
}
$report | Export-Csv "\\server\Common\Drives\list.csv" -NoTypeInformation
Neally
(Neally)
June 9, 2021, 8:26pm
4
oh also
Get-PhysicalDisk
only works on the local machine you run it against, but you want data from remote computers?
that needs a good chunk more code, you need to invoke the command on the remote machine.
Neally
(Neally)
June 9, 2021, 8:33pm
5
e.g.
$computers = Get-Content "\\server\Common\Drives\list.TXT"
$report =
ForEach ($Computer in $Computers) {
if(Test-Connection $computer -Count 1 -Quiet){
$remoteMachine =
invoke-command -computername $computer -ScriptBlock {
Get-PhysicalDisk
}
foreach($disk in $remoteMachine){
[pscustomobject]@{
ComputerName = $computer
FriendlyName = $disk.FriendlyName
MediaType = $disk.MediaType
HealthStatus = $disk.HealthStatus
Size =[math]::Round($disk.size / 1gb)
}
}
}
else{
[pscustomobject]@{
ComputerName = $computer
FriendlyName = '-'
MediaType = '-'
HealthStatus = '-'
Size = '-'
}
}
}
$report |
Export-Csv "\\server\Common\Drives\list.csv" -NoTypeInformation
2 Spice ups
That would explain why it’s only showing my PC’s details, thank you for that. I’ll look into the invoke-command functionality.
Thanks again, Neally, really appreciate your responses. This works beautifully.
DoctorDNS
(DoctorDNS)
June 10, 2021, 11:07am
8
Just a small comment
I am not sure what you are doing with this line
select-object $computer,FriendlyName,MediaType,HealthStatus,@{n='size';e={[math]::Round($_.size / 1gb)}}|
Select–object, as you are using it here, filters the properties you add from the original objects, and creates a new property on the fly.
However, using $Computer does not do anything as I suspect a computer name is not a property of the input object.
What are you trying to achieve??