Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ###Functions
- Function LoadModules()
- {
- ##Function that loads the required modules and tries to check if they're installed. For the Active Directory module no checks are implemented.
- #Is it "ImportExcel or Import-Excel? Verify and fix.
- Import-Module ActiveDirectory
- Import-Module ImportExcel
- if( get-module -name ImportExcel )
- {
- }
- else
- {
- #Display a popup telling the user the module is not installed and exit.
- $intAnswer = $global:comObject.popup("Required Module 'Import-Excel' not installed! Exiting ...",0,"Error",0)
- exit
- }
- }
- Function SelectFolderDialog
- {
- ##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.
- param([string]$Description="Please select the output folder for the inventory file",[string]$RootFolder="Desktop")
- $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)
- [System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") | Out-Null
- $objForm = New-Object System.Windows.Forms.FolderBrowserDialog
- $objForm.Rootfolder = $RootFolder
- $objForm.Description = $Description
- $Show = $objForm.ShowDialog((New-Object System.Windows.Forms.Form -Property @{TopMost = $true }))
- If ($Show -eq "OK")
- {
- Return $objForm.SelectedPath
- }
- Else
- {
- Write-Host "Operation cancelled by user."
- exit
- }
- }
- Function ExportXlsxSheet($customObject,$sheetName,$path)
- {
- #Function that exports an object to a named Excel sheet at a specified path.
- $customObject | Export-Excel -Path $path"\SunSet-Inventory.xlsx" -WorkSheetname $sheetName -FreezeTopRow -AutoSize -AutoFilter
- }
- Function ExportXlsxSingleColumn($customObject,$sheetName,$path,$startColumn,$title,$titleSize)
- {
- #Function that exports an object to a named Excel sheet at a specified path with a specified start column (starting position), title and titlesize.
- $customObject | Export-Excel -Path $path"\SunSet-Inventory.xlsx" -WorkSheetname $sheetName -StartColumn $startColumn -Title $title -titlesize $titleSize -FreezeTopRow -AutoSize -AutoFilter
- }
- Function ServerTest($ADQuery)
- {
- ##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.
- ##If the connection test is succesfull the test will continue and attempt to get the hardware model of the server
- Write-Host "Starting Connection Test"
- #create empty hash table , ordered
- $computerConnectionTest = [ordered]@{}
- $computerHardwareModel = [ordered]@{}
- foreach($computer in $ADQuery."Full Computername")
- {
- Write-Host $computer
- #each servername in the still loaded AD Query
- #ping hostname and output RAW IP
- $IP = (Test-Connection -ComputerName $computer -count 1 -ErrorAction SilentlyContinue).IPV4Address.IPAddressToString
- if ( Test-Connection -ComputerName $computer -count 1 -ErrorAction SilentlyContinue)
- {
- #reverse lookup raw IP to hostname, convert to uppercase
- $HostFromIP = [System.Net.Dns]::GetHostEntry($IP).HostName.ToUpper()
- #compare uppercase hostname to uppercase servername
- if($HostFromIP.StartsWith($computer.ToUpper()))
- {
- #write variable for result
- $pingResult = "Up"
- #Get the Hardware Model through WMI
- $modelResult = (Get-WmiObject -ComputerName $computer -Class win32_computersystem).Model
- Write-Host $modelResult
- }
- else
- {
- $pingResult = "Down"
- $modelResult = "Unknown"
- }
- }
- else
- {
- $pingResult = "Down"
- $modelResult = "Unknown"
- }
- #write variables to hash table
- $computerConnectionTest.add($computer,$pingResult)
- $computerHardwareModel.add($computer,$modelResult)
- }
- #write results to existing excel file, on corresponding row from hash table
- ExportXlsxSingleColumn $computerConnectionTest.values "Servers-Data" $global:path 8 "Ping Result" 11
- ExportXlsxSingleColumn $computerHardwareModel.values "Servers-Data" $global:path 13 "Hardware Model" 11
- }
- Function ADQueryServersAll()
- {
- Write-Host "Starting Servers Inventory"
- #Get all computers that do not have an operating system that matches "*server*".
- $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 = {}},
- @{Label = "Archive?"; Expression = {}},
- @{Label = "Full ComputerName";Expression = {$_.Name}},
- @{Label = "Operating System";Expression = {$_.OperatingSystem}},
- @{Label = "Last LogOn Date";Expression = {$_.lastlogondate}},
- @{Label = "Description";Expression = {$_.Description}},
- @{Label = "Owner/Responsible";Expression = {}},
- @{Label = "Ping Results";Expression = {}},
- @{Label = "Comments";Expression = {}},
- @{Label = "Due Date";Expression = {}},
- @{Label = "Sign Off date";Expression = {}},
- @{Label = "Actions/Escaltion";Expression = {}}
- ExportXLSXSheet $ADServerQuery "Servers-Data" $global:path
- ServerTest $ADServerQuery
- }
- Function ADQueryWorkstationsAll()
- {
- Write-Host "Starting Workstations Inventory"
- #Get all computers that do not have an operating system that does not match "*server*".
- $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 = {}},
- @{Label = "Archive?"; Expression = {}},
- @{Label = "Full ComputerName";Expression = {$_.Name}},
- @{Label = "Operating System";Expression = {$_.OperatingSystem}},
- @{Label = "Last LogOn Date";Expression = {$_.lastlogondate}},
- @{Label = "Description";Expression = {$_.Description}},
- @{Label = "Owner/Responsible";Expression = {}},
- @{Label = "Ping Results";Expression = {}},
- @{Label = "Comments";Expression = {}},
- @{Label = "Due Date";Expression = {}},
- @{Label = "Sign Off date";Expression = {}},
- @{Label = "Actions/Escaltion";Expression = {}}
- ExportXLSXSheet $ADWorkstationQuery "Workstations-Data" $global:path
- }
- Function ADQueryUsersAll()
- {
- Write-Host "Starting Users Inventory"
- #Get all users that are not disabled and have PasswordNeverExpires set to False.
- $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 = {}},
- @{Label = "Display Name";Expression = {$_.DisplayName}},
- @{Label = "Logon Name";Expression = {$_.sAMAccountName}},
- @{Label = "Description";Expression = {$_.Description}},
- @{Label = "Last LogOn Date";Expression = {$_.lastlogondate}},
- @{Label = "DN";Expression = {$_.DistinguishedName}}
- ExportXLSXSheet $ADUserQuery "Users-Data" $global:path
- }
- Function ADQueryServiceAccountsAll()
- {
- Write-Host "Starting Service Accounts Inventory"
- #Get all users that are not disabled and have PasswordNeverExpires set to True.
- $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 = {}},
- @{Label = "Display Name";Expression = {$_.DisplayName}},
- @{Label = "Logon Name";Expression = {$_.sAMAccountName}},
- @{Label = "Description";Expression = {$_.Description}},
- @{Label = "Last LogOn Date";Expression = {$_.lastlogondate}},
- @{Label = "DN";Expression = {$_.DistinguishedName}}
- ExportXLSXSheet $ADServiceAccountQuery "ServiceAccounts-Data" $global:path
- }
- Function ADQueryGroupsAll()
- {
- Write-Host "Starting Groups Inventory"
- #Get all groups that have at least 1 member and are of type Security.
- $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}},
- @{Label = "Pre-Windows 2000 Group Name";Expression = {$_.sAMAccountName}},
- @{Label = "Description";Expression = {$_.Description}},
- @{Label = "Group Type";Expression = {$_.GroupCategory}},
- @{Label = "DN";Expression = {$_.DistinguishedName}}
- ExportXLSXSheet $ADGroupQuery "Groups-Data" $global:path
- }
- Function StartInventory()
- {
- ##Function that calls all other inventory functions.
- Write-Host "Starting Inventory"
- ADQueryServersAll
- ADQueryWorkstationsAll
- ADQueryUsersAll
- ADQueryServiceAccountsAll
- ADQueryGroupsAll
- }
- ###Global Variables
- $global:comObject = new-object -comobject wscript.shell
- $global:domainController = "wom11srvhodc01.be.madm.net"
- ###Main
- LoadModules
- $global:path = SelectFolderDialog
- StartInventory
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement