# Script: ADMT-comp-user-info
# Kyle Weir
# jkw117@gmail.com
# http://virtual-joe.blogspot.com/
# 11/23/2008
$Excel = New-Object -Com Excel.Application
$Excel.Visible = $True
cls
$WorkBook = $Excel.WorkBooks.Add()
$WorkSheet = $WorkBook.WorkSheets.Item(1)
$erroractionpreference = "Silentlycontinue"

$WorkSheet.Cells.Item(1,1) = "RPC"
$WorkSheet.Cells.Item(1,2) = "Ping"
$WorkSheet.Cells.Item(1,3) = "C$ share"
$WorkSheet.Cells.Item(1,4) = "Machine Name"
$WorkSheet.Cells.Item(1,5) = "User Name"
$WorkSheet.Cells.Item(1,6) = "C: filesystem"
$CellRange = $WorkSheet.UsedRange
$CellRange.Interior.ColorIndex = 19
$CellRange.Font.ColorIndex = 11
$intRow = 2
$Drive = "K:" # Drive Letter to use to test network shares
$net = New-Object -com WScript.Network; # Setup Object to connect to network shares
# You can adjust the number of computers with the size limit below
# Also you can set the search root by adding this to the get-qadcomputer line below, -SearchRoot 'domain.domain.com/computers/'
$c = Get-QADComputer -ComputerRole member -SizeLimit 0
$numcomp = $c.length-1
for ($x=0;$x -le $numcomp;$x++)
{
 $percent = $x / $numcomp * 100
 $compname=$c[$x].name
 $childperc = 0;
 Write-Progress -Id 1 -Activity "Working on machine.. $x of $numcomp " -PercentComplete $percent -CurrentOperation "$percent% complete" -Status "Computer name $compname";
 write-progress -id 2 -parentId 1 -activity "Pinging" -status "" -percentComplete ($childperc)
 $ping = new-object Net.NetworkInformation.Ping
 $pgresult = $ping.Send($compname)
 $Worksheet.Cells.Item($intRow, 2) = $pgresult.status.ToString()
 $Worksheet.Cells.Item($intRow, 4) = $c[$x].name
 $UNC = "\\"+ $compname +"\C$"
 $childperc+=5
 write-progress -id 2 -parentId 1 -activity "Mapping Drive" -status "Dismounting" -percentComplete ($childperc)
 $net.removenetworkdrive($Drive)
 $childperc+=5
 write-progress -id 2 -parentId 1 -activity "Mapping Drive" -status "Mounting" -percentComplete ($childperc)
 $net.mapnetworkdrive($Drive,$Unc)
 
 if($?) #If we can connect to the drive then we continue otherwise we go to the next and document
 {
   $Worksheet.Cells.Item($intRow, 3) = 'Drive Mapping Successful'
 }else{
   
   $Worksheet.Cells.Item($intRow, 3) = 'Drive Mapping Failed'
 }
 $colntfsdrives= gwmi win32_logicaldisk -ComputerName $c[$x].name
 $childperc+=5
 write-progress -id 2 -parentId 1 -activity "Checking Drive" -status "FileSystem" -percentComplete ($childperc)
 for($ntfsx=0;$ntfsx -le $colntfsdrives.count;$ntfsx++)
 {
  if($colntfsdrives[$ntfsx].DeviceID -eq "C:")
  {
   $Worksheet.Cells.Item($intRow, 6) = $colntfsdrives[$ntfsx].filesystem
  }
 }
 $childperc+=5
 write-progress -id 2 -parentId 1 -activity "Checking Accounts" -status "Collecting Account information" -percentComplete ($childperc)
 $colComputer = gwmi win32_environment -ComputerName $c[$x].name
 if ($?)
  {
  for ($useracct = 0;$useracct -le $colComputer.count-1; $useracct++)
   {
   $childperc = (($useracct / $colComputer.count-1) * 80) + 20
   write-progress -id 2 -parentId 1 -activity "Processing user accounts on machine" -status "$colComputer[$useracct]" -percentComplete ($childperc)
   while($colComputer[$useracct].username -match 'system')
    {$useracct++}
   while($colComputer[$useracct].username -match 'Administrator')
    {$useracct++}
   while($colComputer[$useracct].username -match 'tech')
    {$useracct++}
   while($colComputer[$useracct].username -match 'service')
    {$useracct++}
   $Worksheet.Cells.Item($intRow, 1) = 'RPC Successful'
   $Worksheet.Cells.Item($intRow, 2) = $pgresult.status.ToString()
   $Worksheet.Cells.Item($intRow, 3) = 'Drive Mapping Successful'
   $Worksheet.Cells.Item($intRow, 4) = $c[$x].name
   $Worksheet.Cells.Item($intRow, 5) = $colComputer[$useracct].username
   $intRow++
   }  
  }
 else
 {
  $Worksheet.Cells.Item($intRow, 1) = 'RPC Failed'
  $intRow++
 } 
}

$Cellrange.Font.Bold = $True
$Cellrange.EntireColumn.AutoFit()