Get-Cesi.psm1
function Get-Cesi{ [cmdletbinding()] Param ( [Parameter( ValueFromPipeline=$true, ParameterSetName='vm' )] [VMware.VimAutomation.ViCore.Types.V1.Inventory.VirtualMachine[]] $VM, [Parameter(ParameterSetName='dept')] [String] $department, [Parameter()] [String] $csvPath = '~/git/virt/scripts/Hosting Customer Catalog - Customer Catalog.csv', [Parameter(ParameterSetName='vm')] [Switch] $groupByDept ) begin { try{ # check age of csv, recommend downloading again if past 30 days? $csv = $csvPath|Get-ChildItem -ErrorAction Stop if ($csv.CreationTime -lt ((Get-Date).AddMonths(-1))) { Write-Warning "The CSV file is older than 30 days. Please download a new copy." Write-Warning "https://docs.google.com/spreadsheets/d/1Uyd0_dhfadwGINJ-KvKu5nGNq_27VRG5RUq1Jio392c/edit#gid=1297794778" } # try to import the data $csvData = Import-Csv -path $csv } catch { throw "Unable to import CSV file. Please check the path and try again." } $objects = @() } process { if ($VM){ $department = ($vm | Get-TagAssignment -Category 'Department').Tag.Name } if ($department ){#-and ($objects.DepartmentId -notcontains $department)){ $deptInfo = $csvData | Where-Object {$_.'Unit Short ID' -eq $department} $deptObj = [PSCustomObject]@{ VM = $vm.Name DepartmentId = $deptInfo.'Unit Short ID' DepartmentName = $deptInfo.'Unit Long Name' Contacts = $deptInfo.'Contact(s) (Customer Contacts for General Notifications & Inquiries and Outages & Issues)' Director = $deptInfo.'IT Director, Unit IT Lead, or Service Owner' WinAdmins = $deptInfo.'Administrators Windows' LinuxAdmins = $deptInfo.'Administrators Linux' SMEAdmins = $deptInfo.'Administrators Self-Managed' WinRequestors = ($deptInfo.'Authorized Requestors Windows'.split(",").trim()|ForEach-Object {$_ + "@umn.edu"}) -join "; " LinuxRequestors = ($deptInfo.'Authorized Requestors Linux'.split(",").trim()|ForEach-Object {$_ + "@umn.edu"}) -join "; " SMERequestors = ($deptInfo.'Authorized Requestors Self-Managed'.split(",").trim()|ForEach-Object {if ($_){$_ + "@umn.edu"}}) -join "; " OnBoardDate = $deptInfo.'On-boarding Date' } $objects += $deptObj } else { $deptObj = [PSCustomObject]@{ VM = $vm.Name DepartmentId = 'Undefined' DepartmentName = 'Undefined' } $objects += $deptObj } } End { $count = $objects.count Write-Host "Found $count vms" if ($groupByDept){ #how do $objects = $objects | Group-Object -Property 'DepartmentId' -AsHashTable $groupedObjects = @() foreach ($item in $objects.keys){ $deptObj = [PSCustomObject]@{ VM = $objects.item($item).vm -join " | " DepartmentId = $objects.item($item).departmentid|Get-unique DepartmentName = $objects.item($item).departmentName|Get-unique Contacts = $objects.item($item).Contacts|Get-unique Director = $objects.item($item).Director|Get-unique WinAdmins = $objects.item($item).WinAdmins|Get-unique LinuxAdmins = $objects.item($item).LinuxAdmins|Get-unique SMEAdmins = $objects.item($item).SMEAdmins|Get-unique WinRequestors = $objects.item($item).WinRequestors|Get-unique LinuxRequestors = $objects.item($item).LinuxRequestors|Get-unique SMERequestors = $objects.item($item).SMERequestors|Get-unique OnBoardDate = $objects.item($item).OnBoardDate|Get-unique } $groupedobjects += $deptObj } return $groupedObjects } else { return $objects } } } |