<\/div>\n<\/aside>\n","upvoteCount":1,"datePublished":"2021-11-09T16:04:50.000Z","url":"https://community.spiceworks.com/t/ps-script-assistance-please/816427/3","author":{"@type":"Person","name":"Evan7191","url":"https://community.spiceworks.com/u/Evan7191"}},{"@type":"Answer","text":"
Thanks, but what I posted already gives me the adapter information. My challenge is applying that to a list of computers then dumping it to a csv. Forgive me if I am at a lost with your suggestion<\/p>","upvoteCount":0,"datePublished":"2021-11-09T16:24:55.000Z","url":"https://community.spiceworks.com/t/ps-script-assistance-please/816427/4","author":{"@type":"Person","name":"zayscott5065","url":"https://community.spiceworks.com/u/zayscott5065"}},{"@type":"Answer","text":"\n\n
<\/div>\n
Zay1967:<\/div>\n
\nThanks, but what I posted already gives me the adapter information. My challenge is applying that to a list of computers then dumping it to a csv. Forgive me if I am at a lost with your suggestion<\/p>\n<\/blockquote>\n<\/aside>\n
it only gives you the adapter information for your LOCAL machine, not for remote machines.<\/p>\n
you have to invoke the command on a remote machine or start a CIM session to get the same info.<\/p>\n
What exactly are you lost with?<\/p>","upvoteCount":0,"datePublished":"2021-11-09T16:28:36.000Z","url":"https://community.spiceworks.com/t/ps-script-assistance-please/816427/5","author":{"@type":"Person","name":"Neally","url":"https://community.spiceworks.com/u/Neally"}},{"@type":"Answer","text":"
Correct I can do invoke-command -computer “computernamehere” get-ciminstance win32_networkadapter . My issue, whether with your suggestion or what I posted script-wise, is combining it to do the same for a list of computers then piping it out to a csv.<\/p>","upvoteCount":0,"datePublished":"2021-11-09T16:55:23.000Z","url":"https://community.spiceworks.com/t/ps-script-assistance-please/816427/6","author":{"@type":"Person","name":"zayscott5065","url":"https://community.spiceworks.com/u/zayscott5065"}},{"@type":"Answer","text":"
Instead of this:<\/p>\n
$nicspeed = @{name = 'Speed'; expression = {$_.linkspeed}}\n$nicobjects = $nic | select-object $nicname, $nicinterface, $nicstatus, $nicmac, $nicspeed\n<\/code><\/pre>\nhow about<\/p>\n
$nicspeed = @{name = 'Speed'; expression = {$_.linkspeed}}\n$Comp - @{Name='ComputerName'; expression= $env:computername}\n$nicobjects = $nic | select-object $nicname, $nicinterface, $nicstatus, $nicmac, $nicspeed\n<\/code><\/pre>","upvoteCount":0,"datePublished":"2021-11-09T19:28:06.000Z","url":"https://community.spiceworks.com/t/ps-script-assistance-please/816427/8","author":{"@type":"Person","name":"DoctorDNS","url":"https://community.spiceworks.com/u/DoctorDNS"}},{"@type":"Answer","text":"Perhaps you could try incorporating an if statement, have it output to one csv for a single NIC, and a different one if there are more than one.<\/p>\n
$nics = Get-NetAdapter -Physical\n\nif (($nics.Name).count -gt 1){\nWrite-Output \"More than one\"\n}\nElse\n{\nWrite-output \"Only 1\"\n}\n<\/code><\/pre>","upvoteCount":1,"datePublished":"2021-11-09T22:36:14.000Z","url":"https://community.spiceworks.com/t/ps-script-assistance-please/816427/9","author":{"@type":"Person","name":"titusovermyer","url":"https://community.spiceworks.com/u/titusovermyer"}},{"@type":"Answer","text":"Leave the ‘export=…’ line where it is, then wrap all the rest in a ForEach loop going over all the PCs.<\/p>\n
ForEach ($PC in {wherever you get the list, just the names}){\n # Do what you're already doing\n # $PC is the name of the PC you're doing it to\n} # End, For Each PC in the list\n<\/code><\/pre>\nOn a performance note, if you have a lot of these, the ForEach piping to Export-CSV -Append will eat a lot of time per each PC (file-open, write data, file close). It would be faster to store the results in a variable for the run, then one Export-CSV at the end.<\/p>\n
$Result = ForEach ($PC in (Get-ADComputer -Properties whatever -Filter something){\n # As before. This will be MUCH easier with a \n # PSCustomObject.\n}\n$Result | Export-CSV \"filename\" -etc.\n\n<\/code><\/pre>","upvoteCount":1,"datePublished":"2021-11-11T13:14:17.000Z","url":"https://community.spiceworks.com/t/ps-script-assistance-please/816427/10","author":{"@type":"Person","name":"jimlong3","url":"https://community.spiceworks.com/u/jimlong3"}},{"@type":"Answer","text":"\n\n
<\/div>\n
Jim6795:<\/div>\n
\nLeave the ‘export=…’ line where it is, then wrap all the rest in a ForEach loop going over all the PCs.<\/p>\n
ForEach ($PC in {wherever you get the list, just the names}){\n # Do what you're already doing\n # $PC is the name of the PC you're doing it to\n} # End, For Each PC in the list\n<\/code><\/pre>\nOn a performance note, if you have a lot of these, the ForEach piping to Export-CSV -Append will eat a lot of time per each PC (file-open, write data, file close). It would be faster to store the results in a variable for the run, then one Export-CSV at the end.<\/p>\n
$Result = ForEach ($PC in (Get-ADComputer -Properties whatever -Filter something){\n # As before. This will be MUCH easier with a \n # PSCustomObject.\n}\n$Result | Export-CSV \"filename\" -etc.\n\n<\/code><\/pre>\n<\/blockquote>\n<\/aside>\nThis is good advice. The Get-ADComputer query should be moved before the loop and stored in a variable. If it is inside the loop condition, the query will run repeatedly.<\/p>\n
$Computers = Get-ADComputer -Properties whatever -Filter 'something -eq \"something\"'\n$Result = ForEach ($PC in $Computers){\n # As before. This will be MUCH easier with a \n # PSCustomObject.\n}\n$Result | Export-CSV \"filename\" -etc.\n<\/code><\/pre>","upvoteCount":0,"datePublished":"2021-11-11T13:40:33.000Z","url":"https://community.spiceworks.com/t/ps-script-assistance-please/816427/11","author":{"@type":"Person","name":"Evan7191","url":"https://community.spiceworks.com/u/Evan7191"}}]}}
Hello team, I am stuck with this script and just need some assistance completing it. I am trying to query computers that have more than one physical NIC and then dump it out to a CSV file. I have the dump working for the NICs, I just need help tying it to a list of computers so the CSV will have a column for computer names then matched to the corresponding columns. This is what I have so far:
$export= 'C:\TEMP\temp.csv'
$nics = get-netadapter -physical
foreach ($nic in $nics){
$nicname = @{name = 'Name'; expression = {$_.name}}
$nicinterface = @{name = 'Interface'; expression = {$_.interfacedescription}}
$nicstatus = @{name = 'Status'; expression = {$_.status}}
$nicmac = @{name = 'MAC'; expression = {$_.macaddress}}
$nicspeed = @{name = 'Speed'; expression = {$_.linkspeed}}
$nicobjects = $nic | select-object $nicname, $nicinterface, $nicstatus, $nicmac, $nicspeed
$nicobjects | export-csv -append -path $export -notypeinformation
}
2 Spice ups
Neally
(Neally)
November 9, 2021, 3:58pm
2
please look into custom objects!
if you have WinRM setup, you can invoke the command on the remote machine to get the info.
Or you can use WIM /CIM
get-ciminstance win32_networkadapter -computername $remoteMAchine
give it a try
1 Spice up
Evan7191
(Evan7191)
November 9, 2021, 4:04pm
3
Here is the documentation for PSCustomObjects.
1 Spice up
Thanks, but what I posted already gives me the adapter information. My challenge is applying that to a list of computers then dumping it to a csv. Forgive me if I am at a lost with your suggestion
Neally
(Neally)
November 9, 2021, 4:28pm
5
it only gives you the adapter information for your LOCAL machine, not for remote machines.
you have to invoke the command on a remote machine or start a CIM session to get the same info.
What exactly are you lost with?
Correct I can do invoke-command -computer “computernamehere” get-ciminstance win32_networkadapter . My issue, whether with your suggestion or what I posted script-wise, is combining it to do the same for a list of computers then piping it out to a csv.
Neally
(Neally)
November 9, 2021, 4:59pm
7
Zay1967:
Correct I can do invoke-command -computer “computernamehere” get-ciminstance win32_networkadapter . My issue, whether with your suggestion or what I posted script-wise, is combining it to do the same for a list of computers then piping it out to a csv.
e.g.
you probably want to add error handling (e.g. if winrm does not work, or the computer is unreachable etc.)
But this is the idea.
# my csv in this sample has a 'computername' header
$export = import-csv 'C:\TEMP\temp.csv'
$nicReport =
foreach ($remoteMachine in $export.computername) {
try {
$nicInfo =
Invoke-Command -ComputerName $remoteMachine -ScriptBlock {
Get-NetAdapter -Physical
} -ErrorAction Stop
foreach ($nic in $nicInfo) {
[pscustomobject]@{
ComputerName = $remoteMachine
NIC_Name = $nic.name
NIC_Interface = $nic.interfacedescription
NIC_Status = $nic.status
NIC_Mac = $nic.macaddress
NIC_Speed = $nic.linkspeed
}
}
}
catch {
[pscustomobject]@{
ComputerName = $remoteMachine
NIC_Name = "-"
NIC_Interface = "-"
NIC_Status = $error[0].exception.message
NIC_Mac = "-"
NIC_Speed = "-"
}
}
}
$nicreport |
export-csv "C:\temp\NIC_Report.csv" -NoTypeInformation
1 Spice up
DoctorDNS
(DoctorDNS)
November 9, 2021, 7:28pm
8
Instead of this:
$nicspeed = @{name = 'Speed'; expression = {$_.linkspeed}}
$nicobjects = $nic | select-object $nicname, $nicinterface, $nicstatus, $nicmac, $nicspeed
how about
$nicspeed = @{name = 'Speed'; expression = {$_.linkspeed}}
$Comp - @{Name='ComputerName'; expression= $env:computername}
$nicobjects = $nic | select-object $nicname, $nicinterface, $nicstatus, $nicmac, $nicspeed
Perhaps you could try incorporating an if statement, have it output to one csv for a single NIC, and a different one if there are more than one.
$nics = Get-NetAdapter -Physical
if (($nics.Name).count -gt 1){
Write-Output "More than one"
}
Else
{
Write-output "Only 1"
}
1 Spice up
jimlong3
(Jim6795)
November 11, 2021, 1:14pm
10
Leave the ‘export=…’ line where it is, then wrap all the rest in a ForEach loop going over all the PCs.
ForEach ($PC in {wherever you get the list, just the names}){
# Do what you're already doing
# $PC is the name of the PC you're doing it to
} # End, For Each PC in the list
On a performance note, if you have a lot of these, the ForEach piping to Export-CSV -Append will eat a lot of time per each PC (file-open, write data, file close). It would be faster to store the results in a variable for the run, then one Export-CSV at the end.
$Result = ForEach ($PC in (Get-ADComputer -Properties whatever -Filter something){
# As before. This will be MUCH easier with a
# PSCustomObject.
}
$Result | Export-CSV "filename" -etc.
1 Spice up
Evan7191
(Evan7191)
November 11, 2021, 1:40pm
11
Jim6795:
Leave the ‘export=…’ line where it is, then wrap all the rest in a ForEach loop going over all the PCs.
ForEach ($PC in {wherever you get the list, just the names}){
# Do what you're already doing
# $PC is the name of the PC you're doing it to
} # End, For Each PC in the list
On a performance note, if you have a lot of these, the ForEach piping to Export-CSV -Append will eat a lot of time per each PC (file-open, write data, file close). It would be faster to store the results in a variable for the run, then one Export-CSV at the end.
$Result = ForEach ($PC in (Get-ADComputer -Properties whatever -Filter something){
# As before. This will be MUCH easier with a
# PSCustomObject.
}
$Result | Export-CSV "filename" -etc.
This is good advice. The Get-ADComputer query should be moved before the loop and stored in a variable. If it is inside the loop condition, the query will run repeatedly.
$Computers = Get-ADComputer -Properties whatever -Filter 'something -eq "something"'
$Result = ForEach ($PC in $Computers){
# As before. This will be MUCH easier with a
# PSCustomObject.
}
$Result | Export-CSV "filename" -etc.