Link to home
Start Free TrialLog in
Avatar of HEMIChallenger
HEMIChallenger

asked on

csv file

import-csv Computers.csv | 
foreach {
    $system = "" | 
    select PCName, UserName, MacAddress, IPAddress
    
    $colItems = Get-WmiObject -Class Win32_NetworkAdapterConfiguration `
    -ComputerName $_.Computer -Filter "IpEnabled = TRUE AND NOT Caption LIKE '%VMware%' AND NOT Caption LIKE '%VirtualBox%'"
    ForEach ($objItem in $colItems){
    $System.MacAddress = $objItem.MacAddress
    $System.IPAddress = $objItem.IpAddress[0]}
 
    $server = Get-WmiObject -Class Win32_ComputerSystem `
    -ComputerName $_.Computer
    
    $System.PCName = $server.Name
    $System.UserName = $server.UserName

    $system
   }   |
   Export-Csv c:\Computer-info.csv -NoType 

Open in new window


Could someone help me out with this. I'm new to  powershell and the script I created kind of works. The issue is if the computer cant be contacted I get errors in powershell. The computer that are online the information is gathered. but is it possible if the computers that are not reachable that it write in the export CSV file that computer blank could not be contacted.
Avatar of SubSun
SubSun
Flag of India image

You can use Test-Connection to check if the server is up or not..
import-csv Computers.csv | 
foreach {
    $system = "" | 
    select PCName, UserName, MacAddress, IPAddress
    If (Test-Connection $_.Computer -Quiet -Count 2){
    $colItems = Get-WmiObject -Class Win32_NetworkAdapterConfiguration `
    -ComputerName $_.Computer -Filter "IpEnabled = TRUE AND NOT Caption LIKE '%VMware%' AND NOT Caption LIKE '%VirtualBox%'"
    ForEach ($objItem in $colItems){
    $System.MacAddress = $objItem.MacAddress
    $System.IPAddress = $objItem.IpAddress[0]}
 
    $server = Get-WmiObject -Class Win32_ComputerSystem `
    -ComputerName $_.Computer
    
    $System.PCName = $server.Name
    $System.UserName = $server.UserName

    $system
		}Else{
		$_.Computer | select @{N="PCName";E={$_}},UserName,MacAddress,@{N="IPAddres";E={"Down"}}
		}
   }   |
   Export-Csv c:\Computer-info.csv -NoType

Open in new window

Great solution provided.
@dbaduck Thanks.. :-)
Avatar of HEMIChallenger
HEMIChallenger

ASKER

That works great, thank you Sir. Curious if there no current user logged on. Is it possible to get the last person who logged on to the computer? By using a \\computer\c$\users\ and get the last modified name? Unless there another way.
ASKER CERTIFIED SOLUTION
Avatar of SubSun
SubSun
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you sir. okay I will keep that in mind next time sorry about that.
No problem!..