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.
HEMIChallengerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

SubsunCommented:
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

0
DBAduck - Ben MillerPrincipal ConsultantCommented:
Great solution provided.
0
SubsunCommented:
@dbaduck Thanks.. :-)
0
Hey MSSPs! What's your total cost of ownership?

WEBINAR: Managed security service providers often deploy & manage products from a variety of solution vendors. But is this really the best approach when it comes to saving time AND money? Join us on Aug. 15th to learn how you can improve your total cost of ownership today!

HEMIChallengerAuthor Commented:
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.
0
SubsunCommented:
You can try with Wmi class win32_userprofile.. Try this updated script..
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
    If ($server.UserName -eq $null){
      $System.UserName = (Get-wmiobject -computername $_.Computer -class win32_userprofile | 
        ?{ $_.localpath -like "C:\users\*" } |
         Select @{N="LastUseTime";E={[System.Management.ManagementDateTimeConverter]::ToDateTime($_.LastUseTime)}},`
     @{N="User";E={((New-Object System.Security.Principal.SecurityIdentifier($_.SID)).Translate([System.Security.Principal.NTAccount])).Value}}|
    ?{$_.LastUseTime}| Sort LastUseTime -Descending | Select -First 1).User
		
  }Else{
    $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

PS : In future, please open a new question for additional requests.. :-)
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
HEMIChallengerAuthor Commented:
Thank you sir. okay I will keep that in mind next time sorry about that.
0
SubsunCommented:
No problem!..
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.