Desktop Slideshow

  1. <# 
  2. Script Name:  GetMappedNetworkPrinters.ps1 
  3.  
  4. Purpose: 
  5. This script can be used to collect the mapped network printer information from the users who are logged into the console of the Computer or Computers specified. 
  6.  
  7. Required Modules: 
  8. PSRemoteRegistry, and Active Directory 
  9.  
  10. Permission Requirements: 
  11. The user account that the script is run with needs to have administrative permissions on the workstations and permission to query Active Directory user accounts. 
  12. The computers firewall if enabled needs to allow it to be pinged, connections to WMI and also Remote Registry. 
  13. A user will need to be logged into the console so their mapped network printer information can be collected. 
  14.  
  15. How the script functions: 
  16. Create a text file that contains a list of Computer names that you want to get the mapped network printers info for. 
  17. Execute the script and you will be prompted for the path to the text file that contains the list. 
  18. Connectivity will be verified to each of the computers by pinging each of them. 
  19. Via WMI it will check to see which user is logged into the computers that responded to the ping. 
  20. Next it will query Active Directory for the SID of each of the users that were currently logged into one of the active computers polled. 
  21. Using the users SID a Remote Registry query is created to enumerate the list of mapped network printers for the logged on user. 
  22.  
  23. The Log files and CSV file containing the list of mapped printers is located in C:\temp\logs 
  24. FileNames: 
  25. MappedPrinters-(currentdate).csv -- Contains the list of mapped printers. 
  26. NoMappedPrinters-(currentdate).log -- Contains list of users that do not have network printers mapped on their computer. 
  27. NoReply-(currentdate).csv -- Contains list of computers that did not respond to ping. 
  28. NoUsrLoggedIn-(currentdate).log -- Contains list of computers that responded to ping but did not have a user logged into it. 
  29. RemoteRegNotRunning-(currentdate).log -- Contains a list of computers where the Remote Registry service is not running. 
  30. WmiError-(currentdate).log -- If there are computers that it is not able to connect to via wmi it will be listed here. 
  31. #> 
  32.  
  33.  
  34.  
  35. function global:Ping-Host {  
  36.     BEGIN { 
  37.          
  38.     
  39.         PROCESS { 
  40.         $results = gwmi -Query "SELECT * FROM Win32_PingStatus WHERE Address = '$_'" 
  41.         $obj2 = New-Object psobject 
  42.         $obj2 | Add-Member Noteproperty Computer $_ 
  43.         $obj2 | Add-Member Noteproperty IPAddress ($results.protocoladdress
  44.                  
  45.         if ($results.statuscode -eq 0) { 
  46.         $obj2 | Add-Member NoteProperty Responding $True 
  47.         } else
  48.         $obj2 | Add-Member NoteProperty Responding $False 
  49.     
  50.         Write-Output $obj2 
  51.          
  52.     
  53.     END {} 
  54.      
  55. function VerifyConnectivity { 
  56. param
  57. [parameter(ValueFromPipeline=$true)] 
  58. $compList 
  59. BEGIN { 
  60. $modeMSG = "Verifying Connectivity to Desktops" 
  61. $HostComputer = @() 
  62. $d = Get-Date 
  63. $strDate = $d.ToString() 
  64. $month = $d.Month 
  65. $day = $d.Day 
  66. $year = $d.Year 
  67. $cDate = "$month-$day-$year" 
  68. $logFilePath = "C:\temp\logs\" 
  69. $NoReplyLog = $logFilePath + "NoReply-" + $cDate + ".csv" 
  70. PROCESS { 
  71. $i =
  72. $numComp = $compList.Count 
  73. If ($numComp -ge 1){ 
  74. Talk $modeMSG 
  75. $HostComputer = $HostComputer + $
  76.     foreach ($computer in $compList){ 
  77.     Write-Progress -Activity $modeMSG -Status "Currently Processing: $computer" -CurrentOperation "$i of $numComp" -PercentComplete ($i/$numComp*100) 
  78.     $computer | Ping-Host 
  79.     $i = $i +
  80.  
  81. }) 
  82.  
  83. ElseIf ($numComp -lt 1){ 
  84. Write-Host "No Computers to Process" 
  85. Exit 
  86. END { 
  87. $Alive = $HostComputer | Where {$_.Responding -eq "$true"
  88. $global:Dead = $HostComputer | Where {$_.Responding -ne "$true"
  89. $global:Dead | select Computer | Export-Csv -Path $NoReplyLog 
  90. $Acomp = $Alive | select Computer 
  91. $Acomp 
  92.  
  93.  
  94. function GetPrinterInfo { 
  95. param
  96. [parameter(ValueFromPipeline=$true)] 
  97. $compList 
  98. BEGIN { 
  99. $d = Get-Date 
  100. $strDate = $d.ToString() 
  101. $month = $d.Month 
  102. $day = $d.Day 
  103. $year = $d.Year 
  104. $cDate = "$month-$day-$year" 
  105. $global:logFilePath = "C:\temp\logs\" 
  106. $NoPrtMapLog = $logFilePath + "NoMappedPrinters-" + $cDate + ".log" 
  107. $WmiErrorLog = $logFilePath + "WmiError-" + $cDate + ".log" 
  108. $MappedPrinters = $logFilePath + "MappedPrinters-" + $cDate + ".csv" 
  109. $NoUsrLoggedIn = $logFilePath + "NoUsrLoggedIn-" + $cDate + ".log" 
  110. $RemoteRegNotRunning = $logFilePath + "RemoteRegNotRunning-" + $cDate + ".log" 
  111. $ErrorActionPreference = 'SilentlyContinue' 
  112. Import-Module activedirectory 
  113. Import-Module psremoteregistry 
  114. $global:wmiErrors = @() 
  115. $global:NoUserLoggedIn = @() 
  116. $CompUserInfo = @() 
  117. $arrCompLogonInfo = @() 
  118. $arrRemoteRegSvcStopped = @() 
  119. $arrNoMappedPrinters = @() 
  120. $arrMappedPrinters = @() 
  121. $statusMSG = "Getting Logged on User Information" 
  122. $statusMSG2 = "Getting User SID from Active Directory" 
  123. $statusMSG3 = "Collecting Mapped Printer Information" 
  124. PROCESS { 
  125. $u =
  126. $Responded = VerifyConnectivity $compList 
  127. if ($Responded.count -gt 0){ 
  128. Talk $statusMSG 
  129. foreach ($client in $Responded){ 
  130.     [string]$c = $client.Computer 
  131.     $numClient = $Responded.Count 
  132.     $logonInfo = $null 
  133.     Write-Progress -Activity $statusMSG -Status "Currently Processing: $c" -CurrentOperation "$u of $numClient" -PercentComplete ($u/$numClient*100)   
  134.     $logonInfo = Get-WmiObject -ComputerName $c -Query "select * from win32_computersystem" | select Username 
  135.     if ($?){ 
  136.         if ($logonInfo.Username -ne $null){ 
  137.             [string]$strUserName = $logonInfo.Username 
  138.             $arrStrUserName = $strUserName.Split("\"
  139.             $strUser = $arrStrUserName[1]  
  140.             $objCUinfo = New-Object psobject 
  141.             $objCUinfo | Add-Member NoteProperty Workstation $c 
  142.             $objCUinfo | Add-Member NoteProperty User $strUser 
  143.             $CompUserInfo = $CompUserInfo + $objCUinfo             
  144.         
  145.         elseif ($logonInfo.Username -eq $null){ 
  146.         $global:NoUserLoggedIn = $global:NoUserLoggedIn + $c 
  147.         
  148.     
  149.     else
  150.         $global:wmiErrors = $global:wmiErrors + "Could not Execute WMI Query to collect user logon information on $c" 
  151.     
  152.     $u = $u +
  153.     
  154.     if ($CompUserInfo.Count -ge 1){ 
  155.         $u =
  156.         Talk $statusMSG2 
  157.         foreach ($logon in $CompUserInfo){ 
  158.         [string]$userLN = $logon.User 
  159.         $userCount = $CompUserInfo.count 
  160.         [string]$wrksta = $logon.Workstation 
  161.         Write-Progress -Activity $statusMSG2 -Status "Currently Processing: $userLN" -CurrentOperation "$u of $userCount" -PercentComplete ($u/$userCount*100) 
  162.         $getSID = Get-ADUser -Identity $userLN | select SID 
  163.         if ($?){ 
  164.             [string]$sid = $getSID.sid 
  165.             $LoggedOnUserInfo = New-Object psobject 
  166.             $LoggedOnUserInfo | Add-Member Noteproperty Workstation $wrksta 
  167.             $LoggedOnUserInfo | Add-Member Noteproperty User $userLN 
  168.             $LoggedOnUserInfo | Add-Member Noteproperty SID $sid 
  169.             $arrCompLogonInfo = $arrCompLogonInfo + $LoggedOnUserInfo 
  170.         
  171.         $u = $u +
  172.         
  173.     
  174.     if ($arrCompLogonInfo.count -ge 1){ 
  175.         $u =
  176.         Talk $statusMSG3 
  177.         foreach ($comp in $arrCompLogonInfo){ 
  178.         $numT = $arrCompLogonInfo.Count 
  179.         $Printers = $null 
  180.         [string]$cn = $comp.Workstation 
  181.         [string]$usid = $comp.sid 
  182.         [string]$uName = $comp.User 
  183.         Write-Progress -Activity $statusMSG3 -Status "Currently Processing: $cn" -CurrentOperation "$u of $numT" -PercentComplete ($u/$userCount*100) 
  184.         $regStat = Get-Service -ComputerName $cn -Name "RemoteRegistry" 
  185.         If ($?){ 
  186.             If ($regStat.Status -eq "Running"){ 
  187.                 $Printers =  Get-RegKey -ComputerName $cn -Hive "Users" -Key "$usid\Printers\Connections" -Recurse 
  188.                 If ($Printers -ne $null){ 
  189.                 foreach ($printer in $Printers){ 
  190.                 [string]$printerKey = $printer.key 
  191.                 $arrPrinterKey = $printerKey.Split("\"
  192.                 $PrinterNamePiece = $arrPrinterKey[3] 
  193.                 $arrPrinterParts = $PrinterNamePiece.Split(","
  194.                 $printServer = $arrPrinterParts[2] 
  195.                 $PrinterName = $arrPrinterParts[3] 
  196.                 $PrinterUnc = "\\$printServer\$PrinterName" 
  197.                 $printInfo = New-Object psobject 
  198.                 $printInfo | Add-Member NoteProperty Workstation $cn 
  199.                 $printInfo | Add-Member NoteProperty User $uName 
  200.                 $printInfo | Add-Member NoteProperty PrintServer $printServer 
  201.                 $printInfo | Add-Member NoteProperty PrinterName $PrinterName 
  202.                 $printInfo | Add-Member NoteProperty PrinterUNC $PrinterUnc 
  203.                 $arrMappedPrinters = $arrMappedPrinters + $printInfo 
  204.                 
  205.                 
  206.                 ElseIf ($Printers -eq $null){ 
  207.                     $arrNoMappedPrinters = $arrNoMappedPrinters + "$uName has no mapped printers on $cn" 
  208.                     
  209.             
  210.             ElseIf ($regStat.Status -eq "Stopped"){ 
  211.                 $arrRemoteRegSvcStopped = $arrRemoteRegSvcStopped + $cn 
  212.             
  213.         
  214.         $u = $u +
  215.         
  216.      
  217.      
  218.     
  219.      
  220. END { 
  221.     $arrMappedPrinters | Export-Csv -Path $MappedPrinters 
  222.     Add-Content $NoPrtMapLog $arrNoMappedPrinters 
  223.     Add-Content $WmiErrorLog $wmiErrors 
  224.     Add-Content $NoUsrLoggedIn $global:NoUserLoggedIn 
  225.     Add-Content $RemoteRegNotRunning $arrRemoteRegSvcStopped 
  226.     
  227.  
  228. function Talk { 
  229. param
  230. [parameter(ValueFromPipeline=$true)] 
  231. $talk 
  232. Add-Type -AssemblyName System.Speech 
  233. $synthesizer = New-Object -TypeName System.Speech.Synthesis.SpeechSynthesizer 
  234. $synthesizer.Speak($talk
  235.  
  236.  
  237. cls 
  238. $getPath = $(Read-Host "Enter path to the text file that contains the list of Computer Names`n"
  239. cls 
  240. if ($getPath -like "*.txt"){ 
  241.     $valid = Test-Path -Path $getPath 
  242.     if ($valid -eq $true){ 
  243.         $compList = get-content -Path $getPath  
  244.         GetPrinterInfo $compList 
  245.         Write-Host "The Script Output is located in $logfilepath" 
  246.         Exit 
  247.  
  248.     
  249.  
  250.     Else
  251.     Write-Host "Path to file is not valid" -ForegroundColor Red 
  252.     
  253. Elseif ($getPath -notlike "*.txt"){ 
  254.     Write-Host "Path to file is not valid" 
  255.     Exit 
 
Loading...
Concentrated Tech NSoftware Dell Compellent Sponsored by Idera and Concentrated Tech and NSoftware and Dell Compellent
Copyright 2011 PowerShell.com. All rights reserved.