Advertisement
Guest User

Garbage

a guest
Jul 26th, 2017
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ###Functions
  2. Function LoadModules()
  3. {
  4.     ##Function that loads the required modules and tries to check if they're installed. For the Active Directory module no checks are implemented.
  5.     #Is it "ImportExcel or Import-Excel? Verify and fix.
  6.     Import-Module ActiveDirectory
  7.     Import-Module ImportExcel
  8.     if( get-module -name ImportExcel )
  9.     {
  10.     }
  11.     else
  12.     {
  13.         #Display a popup telling the user the module is not installed and exit.
  14.         $intAnswer = $global:comObject.popup("Required Module 'Import-Excel' not installed! Exiting ...",0,"Error",0)  
  15.         exit
  16.     }
  17. }
  18.  
  19. Function SelectFolderDialog
  20. {
  21.     ##Function that creates a "Folder Browser Dialog" and asks the user to select a path in this dialog. It returns the chosen path or exits on cancel.
  22.     param([string]$Description="Please select the output folder for the inventory file",[string]$RootFolder="Desktop")
  23.     $intAnswer = $global:comObject.popup("Please select the output folder for the inventory file. The file will be named SunSet-Inventory.xlsx",0,"Data Validation File",0)
  24.     [System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") | Out-Null    
  25.     $objForm = New-Object System.Windows.Forms.FolderBrowserDialog
  26.     $objForm.Rootfolder = $RootFolder
  27.     $objForm.Description = $Description
  28.     $Show = $objForm.ShowDialog((New-Object System.Windows.Forms.Form -Property @{TopMost = $true }))
  29.     If ($Show -eq "OK")
  30.     {
  31.         Return $objForm.SelectedPath
  32.     }
  33.     Else
  34.     {
  35.         Write-Host "Operation cancelled by user."
  36.         exit
  37.     }
  38. }
  39.  
  40. Function ExportXlsxSheet($customObject,$sheetName,$path)
  41. {
  42.     #Function that exports an object to a named Excel sheet at a specified path.
  43.     $customObject | Export-Excel -Path $path"\SunSet-Inventory.xlsx" -WorkSheetname $sheetName -FreezeTopRow -AutoSize -AutoFilter
  44. }
  45.  
  46. Function ExportXlsxSingleColumn($customObject,$sheetName,$path,$startColumn,$title,$titleSize)
  47. {
  48.     #Function that exports an object to a named Excel sheet at a specified path with a specified start column (starting position), title and titlesize.
  49.     $customObject | Export-Excel -Path $path"\SunSet-Inventory.xlsx" -WorkSheetname $sheetName -StartColumn $startColumn -Title $title -titlesize $titleSize -FreezeTopRow -AutoSize -AutoFilter
  50. }
  51.  
  52. Function ServerTest($ADQuery)
  53. {
  54.     ##Function to test if a computer is reachable over the network. If it is reachable a reverse dns lookup is performed to verify the computer is the right computer.
  55.     ##If the connection test is succesfull the test will continue and attempt to get the hardware model of the server
  56.     Write-Host "Starting Connection Test"
  57.     #create empty hash table , ordered
  58.     $computerConnectionTest = [ordered]@{}
  59.     $computerHardwareModel = [ordered]@{}
  60.     foreach($computer in $ADQuery."Full Computername")
  61.     {
  62.         Write-Host $computer
  63.         #each servername in the still loaded AD Query
  64.         #ping hostname and output RAW IP
  65.         $IP = (Test-Connection -ComputerName $computer -count 1 -ErrorAction SilentlyContinue).IPV4Address.IPAddressToString
  66.         if ( Test-Connection -ComputerName $computer -count 1 -ErrorAction SilentlyContinue)
  67.         {
  68.             #reverse lookup raw IP to hostname, convert to uppercase
  69.             $HostFromIP = [System.Net.Dns]::GetHostEntry($IP).HostName.ToUpper()
  70.             #compare uppercase hostname to uppercase servername
  71.             if($HostFromIP.StartsWith($computer.ToUpper()))
  72.             {
  73.                 #write variable for result
  74.                 $pingResult = "Up"
  75.                 #Get the Hardware Model through WMI
  76.                 $modelResult = (Get-WmiObject -ComputerName $computer -Class win32_computersystem).Model
  77.                 Write-Host $modelResult
  78.             }
  79.             else
  80.             {
  81.                 $pingResult = "Down"
  82.                 $modelResult = "Unknown"
  83.             }
  84.         }
  85.         else
  86.         {
  87.             $pingResult = "Down"
  88.             $modelResult = "Unknown"
  89.         }
  90.     #write variables to hash table
  91.     $computerConnectionTest.add($computer,$pingResult)
  92.     $computerHardwareModel.add($computer,$modelResult)
  93.     }
  94.     #write results to existing excel file, on corresponding row from hash table
  95.     ExportXlsxSingleColumn $computerConnectionTest.values "Servers-Data" $global:path 8 "Ping Result" 11
  96.     ExportXlsxSingleColumn $computerHardwareModel.values "Servers-Data" $global:path 13 "Hardware Model" 11
  97. }
  98.  
  99. Function ADQueryServersAll()
  100. {
  101.     Write-Host "Starting Servers Inventory"
  102.     #Get all computers that do not have an operating system that matches "*server*".
  103.     $ADServerQuery = Get-ADComputer -server $global:domainController -Properties Name,Description,Enabled,LastLogonDate,OperatingSystem -Filter {(OperatingSystem -Like "*server*") -and (Name -like "WOM11SRVHODC01")} | Select-Object @{Label = "Path";Expression = {}},
  104.         @{Label = "Archive?"; Expression = {}},
  105.         @{Label = "Full ComputerName";Expression = {$_.Name}},
  106.         @{Label = "Operating System";Expression = {$_.OperatingSystem}},
  107.         @{Label = "Last LogOn Date";Expression = {$_.lastlogondate}},
  108.         @{Label = "Description";Expression = {$_.Description}},
  109.         @{Label = "Owner/Responsible";Expression = {}},
  110.         @{Label = "Ping Results";Expression = {}},
  111.         @{Label = "Comments";Expression = {}},
  112.         @{Label = "Due Date";Expression = {}},
  113.         @{Label = "Sign Off date";Expression = {}},
  114.         @{Label = "Actions/Escaltion";Expression = {}}
  115.     ExportXLSXSheet $ADServerQuery "Servers-Data" $global:path
  116.     ServerTest $ADServerQuery
  117. }
  118.  
  119. Function ADQueryWorkstationsAll()
  120. {
  121.     Write-Host "Starting Workstations Inventory"
  122.     #Get all computers that do not have an operating system that does not match "*server*".
  123.     $ADWorkstationQuery = Get-ADComputer -server $global:domainController -Properties Name,Description,Enabled,LastLogonDate,OperatingSystem -Filter {(OperatingSystem -NotLike "*server*") -or (-not(OperatingSystem -like "*"))} | Select-Object @{Label = "Path";Expression = {}},
  124.         @{Label = "Archive?"; Expression = {}},
  125.         @{Label = "Full ComputerName";Expression = {$_.Name}},
  126.         @{Label = "Operating System";Expression = {$_.OperatingSystem}},
  127.         @{Label = "Last LogOn Date";Expression = {$_.lastlogondate}},
  128.         @{Label = "Description";Expression = {$_.Description}},
  129.         @{Label = "Owner/Responsible";Expression = {}},
  130.         @{Label = "Ping Results";Expression = {}},
  131.         @{Label = "Comments";Expression = {}},
  132.         @{Label = "Due Date";Expression = {}},
  133.         @{Label = "Sign Off date";Expression = {}},
  134.         @{Label = "Actions/Escaltion";Expression = {}}
  135.     ExportXLSXSheet $ADWorkstationQuery "Workstations-Data" $global:path
  136. }
  137.  
  138. Function ADQueryUsersAll()
  139. {
  140.     Write-Host "Starting Users Inventory"
  141.     #Get all users that are not disabled and have PasswordNeverExpires set to False.
  142.     $ADUserQuery = Get-ADUser -server $global:domainController -Properties DisplayName,sAMAccountName,Description,Enabled,PasswordNeverExpires,LastLogonDate,DistinguishedName -Filter {Enabled -eq $True -and PasswordNeverExpires -eq $False} | Select-Object @{Label = "Path";Expression = {}},
  143.         @{Label = "Display Name";Expression = {$_.DisplayName}},
  144.         @{Label = "Logon Name";Expression = {$_.sAMAccountName}},
  145.         @{Label = "Description";Expression = {$_.Description}},
  146.         @{Label = "Last LogOn Date";Expression = {$_.lastlogondate}},
  147.         @{Label = "DN";Expression = {$_.DistinguishedName}}  
  148.     ExportXLSXSheet $ADUserQuery "Users-Data" $global:path
  149. }
  150.  
  151. Function ADQueryServiceAccountsAll()
  152. {
  153.     Write-Host "Starting Service Accounts Inventory"
  154.     #Get all users that are not disabled and have PasswordNeverExpires set to True.
  155.     $ADServiceAccountQuery = Get-ADUser -server $global:domainController -Properties DisplayName,sAMAccountName,Description,Enabled,PasswordNeverExpires,LastLogonDate,  DistinguishedName -Filter {Enabled -eq $True -and PasswordNeverExpires -eq $True} | Select-Object @{Label = "Path";Expression = {}},
  156.         @{Label = "Display Name";Expression = {$_.DisplayName}},
  157.         @{Label = "Logon Name";Expression = {$_.sAMAccountName}},
  158.         @{Label = "Description";Expression = {$_.Description}},
  159.         @{Label = "Last LogOn Date";Expression = {$_.lastlogondate}},
  160.         @{Label = "DN";Expression = {$_.DistinguishedName}}
  161.     ExportXLSXSheet $ADServiceAccountQuery "ServiceAccounts-Data" $global:path
  162. }
  163.  
  164. Function ADQueryGroupsAll()
  165. {
  166.     Write-Host "Starting Groups Inventory"
  167.     #Get all groups that have at least 1 member and are of type Security.
  168.     $ADGroupQuery = get-adgroup -server $global:domainController -Filter {GroupCategory -eq 'Security'} -Properties Name,Members,GroupCategory,Description,sAMAccountName,DistinguishedName | where { $_.Members.Count -ne 0 } | Select-Object @{Label = "Group Name";Expression = {$_.Name}},
  169.     @{Label = "Pre-Windows 2000 Group Name";Expression = {$_.sAMAccountName}},
  170.     @{Label = "Description";Expression = {$_.Description}},
  171.     @{Label = "Group Type";Expression = {$_.GroupCategory}},
  172.     @{Label = "DN";Expression = {$_.DistinguishedName}}
  173.     ExportXLSXSheet $ADGroupQuery "Groups-Data" $global:path
  174. }
  175.  
  176. Function StartInventory()
  177. {
  178.     ##Function that calls all other inventory functions.
  179.     Write-Host "Starting Inventory"
  180.     ADQueryServersAll
  181.     ADQueryWorkstationsAll
  182.     ADQueryUsersAll
  183.     ADQueryServiceAccountsAll
  184.     ADQueryGroupsAll
  185. }
  186.  
  187. ###Global Variables
  188. $global:comObject = new-object -comobject wscript.shell
  189. $global:domainController = "wom11srvhodc01.be.madm.net"
  190.  
  191. ###Main
  192. LoadModules
  193. $global:path = SelectFolderDialog
  194. StartInventory
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement