Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 342
  • Last Modified:

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.
0
HEMIChallenger
Asked:
HEMIChallenger
  • 4
  • 2
1 Solution
 
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
Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

 
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
 
HEMIChallengerAuthor Commented:
Thank you sir. okay I will keep that in mind next time sorry about that.
0
 
SubsunCommented:
No problem!..
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now