diff --git a/Private/Apps.ps1 b/Private/Apps.ps1 index 80c72ef..fe6c95c 100644 --- a/Private/Apps.ps1 +++ b/Private/Apps.ps1 @@ -1,22 +1,6 @@ Function Get-Apps($ComputerName, $TableView) { - $SCCMServer = "shscm01.int.samhealth.net" - - $SCCMNameSpace = "root\sms\site_100" - - $SCCMX64Query = "select SMS_G_System_ADD_REMOVE_PROGRAMS_64.DisplayName, SMS_G_System_ADD_REMOVE_PROGRAMS_64.Version, SMS_G_System_ADD_REMOVE_PROGRAMS_64.Publisher, SMS_G_System_ADD_REMOVE_PROGRAMS_64.InstallDate from SMS_R_System inner join SMS_G_System_ADD_REMOVE_PROGRAMS_64 on - SMS_G_System_ADD_REMOVE_PROGRAMS_64.ResourceId = SMS_R_System.ResourceId inner join SMS_G_System_SYSTEM on - SMS_G_System_SYSTEM.ResourceId = SMS_R_System.ResourceId where SMS_G_System_SYSTEM.Name = '$ComputerName'" - - $SCCMX86Query = "select SMS_G_System_ADD_REMOVE_PROGRAMS.DisplayName, SMS_G_System_ADD_REMOVE_PROGRAMS.Version, SMS_G_System_ADD_REMOVE_PROGRAMS.Publisher, SMS_G_System_ADD_REMOVE_PROGRAMS.InstallDate from SMS_R_System inner join SMS_G_System_ADD_REMOVE_PROGRAMS on - SMS_G_System_ADD_REMOVE_PROGRAMS.ResourceID = SMS_R_System.ResourceId inner join SMS_G_System_SYSTEM on - SMS_G_System_SYSTEM.ResourceID = SMS_R_System.ResourceId where SMS_G_System_SYSTEM.Name = '$ComputerName'" - - $64apps = Get-WmiObject -namespace $SCCMNameSpace -DirectRead -computer $SCCMServer -query $SCCMX64Query | - Select-Object -Property DisplayName | Add-Member -NotePropertyName x86/x64 -NotePropertyValue 64 -PassThru | Sort-Object -Property DisplayName - - $86apps = Get-WmiObject -namespace $SCCMNameSpace -DirectRead -computer $SCCMServer -query $SCCMX86Query | - Select-Object -Property DisplayName | Add-Member -NotePropertyName x86/x64 -NotePropertyValue 32 -PassThru | Sort-Object -Property DisplayName + $86apps, $64apps = Get-SCCM_Apps $ComputerName #Checks if local computer if ($comp -eq $env:COMPUTERNAME) { diff --git a/Private/AssetConversion.ps1 b/Private/AssetConversion.ps1 deleted file mode 100644 index d01da36..0000000 --- a/Private/AssetConversion.ps1 +++ /dev/null @@ -1,22 +0,0 @@ -Function Get-AssetConversion($ComputerName){ - - $SiteName="100" - - $SCCMServer="shscm01.int.samhealth.net" - - $SCCMNameSpace="root\sms\site_100" - #Set Asset Tag to $comp - $assetTagSCCM = $ComputerName - - $SCCMQuery = "select SMS_R_System.Name from SMS_R_System inner join SMS_G_System_SYSTEM_ENCLOSURE on SMS_G_System_SYSTEM_ENCLOSURE.ResourceId = SMS_R_System.ResourceId Where SMS_G_System_SYSTEM_ENCLOSURE.SMBIOSAssetTag = '$assetTagSCCM'" - - #Set $comp to find hostname of PC - $nameHolder = Get-WmiObject -namespace $SCCMNameSpace -DirectRead -computer $SCCMServer -query $SCCMQuery | Select-Object -First 1 -ExpandProperty Name - - if($null -ne $nameHolder){ - $ComputerName = $nameHolder - return $ComputerName - } - - return $null -} diff --git a/Private/Get-Hostname.ps1 b/Private/Get-Hostname.ps1 new file mode 100644 index 0000000..9955d68 --- /dev/null +++ b/Private/Get-Hostname.ps1 @@ -0,0 +1,24 @@ +function Get-Hostname ($name) { + if ($name.Length -eq 5) { + $res = Get-AssetConversion $name + if ($res) { return $res } + } + # Regex to match IP Address brought to you by https://stackoverflow.com/a/36760050 + if ($name -match "^((25[0-5]|(2[0-4]|1\d|[1-9]|)\d)\.?\b){4}$") { + $res = Resolve-DnsName $name + if ($res -and $res.NameHost -and ($res.NameHost -match "([^.]*)\.int\.samhealth\.net")) { + return $Matches[1] + } + } + if ($name.Length -eq 7) { + $res = Get-ServiceTagConversion $name + if ($res) { return $res } + } + # Regex to match MAC Address brought to you by https://stackoverflow.com/a/4260512 + if ($name -match "^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$") { + $res = Get-SCCM_PCFromMAC $name + if ($res) { return $res } + } + + return $name +} \ No newline at end of file diff --git a/Private/HostnamesByPrinter.ps1 b/Private/HostnamesByPrinter.ps1 deleted file mode 100644 index 036b635..0000000 --- a/Private/HostnamesByPrinter.ps1 +++ /dev/null @@ -1,17 +0,0 @@ -Function HostnamesByPrinter($ComputerName){ - - $SiteName="100" - - $SCCMServer="shscm01.int.samhealth.net" - - $SCCMNameSpace="root\sms\site_100" - - $FindHostnamesByPrinterQuery = "select SMS_R_System.Name from SMS_R_System inner join SMS_G_System_PRINTER_DEVICE on - SMS_G_System_PRINTER_DEVICE.ResourceId = SMS_R_System.ResourceId where SMS_G_System_PRINTER_DEVICE.Name = '$ComputerName'" - - $FindHostnamesByPrinterSCCM = Get-WmiObject -namespace $SCCMNameSpace -DirectRead -computer $SCCMServer -query $FindHostnamesByPrinterQuery | - Select-Object -ExpandProperty Name - - Return $FindHostnamesByPrinterSCCM - -} \ No newline at end of file diff --git a/Private/PCMonitor.ps1 b/Private/PCMonitor.ps1 index 3683881..36b0233 100644 --- a/Private/PCMonitor.ps1 +++ b/Private/PCMonitor.ps1 @@ -27,7 +27,11 @@ function Invoke-PCMonitor { if ($notify) { $null = [System.Windows.MessageBox]::Show($msg) } +<<<<<<< HEAD Write-Host "$msg" -NoNewline +======= + Write-Output "$msg" +>>>>>>> HostnameRefactor } } # Update date and time so user knows script hasn't frozen diff --git a/Private/PrinterPurge.ps1 b/Private/PrinterPurge.ps1 index 8f47833..d180abc 100644 --- a/Private/PrinterPurge.ps1 +++ b/Private/PrinterPurge.ps1 @@ -5,17 +5,23 @@ function Invoke-PrinterPurge ($printer) { if (-not ($comps -is [array])) { $comps = @($comps) } + $res = Read-Host "$($comps.Length) computers found, remove $printer from all of them? (y/N)" if (-not ($res -match "^[yY]")) { return } + for ($i = 0; $i -lt $comps.Length; $i += 1) { $comp = $comps[$i] Write-Progress -Activity "Dispatching Remove-Printer Jobs for $printer" -PercentComplete (100*$i / $comps.Length) -CurrentOperation "$comp $i/$($comps.Length)" + if ($comp -like "*EPIC*") { continue } + if (-not (Test-Connection $comp -Count 1)) { Write-Output "$printer | $comp Offline" continue } + Remove-Printer $printer -ComputerName $comp -AsJob + Write-Output "$printer | $comp Job Sent" } Write-Progress -Activity "Dispatching Remove-Printer Jobs for $printer" -Completed diff --git a/Private/SCCMQuery.ps1 b/Private/SCCMQuery.ps1 index b7a058e..29240a2 100644 --- a/Private/SCCMQuery.ps1 +++ b/Private/SCCMQuery.ps1 @@ -420,4 +420,88 @@ Function Get-SCCM_USB($ComputerName) { SMS_R_System.ResourceId where SMS_R_System.Name = '$ComputerName'" return Get-WmiObject -namespace $SCCMNAMESPACE -DirectRead -computer $SCCMSERVER -query $usbDevicesQuery +} + +function Get-SCCM_PCFromMAC($mac) { + # Try to get address in the right format first like 12:34:56:78:9A:BC + # Covers deliminating '-', '.', or no delimeter + $mac = $mac -replace '([^:.-]{2})[-.]?[^:](?!$)', '$1:' + $pcFromMacQuery = "select SMS_R_System.Name from SMS_R_System where SMS_R_System.MACAddresses = '$mac'" + + return (Get-WmiObject -namespace $SCCMNAMESPACE -DirectRead -computer $SCCMSERVER -query $pcFromMacQuery).Name +} + +function Get-SCCM_Apps($ComputerName) { + + $SCCMX64Query = "select SMS_G_System_ADD_REMOVE_PROGRAMS_64.DisplayName, SMS_G_System_ADD_REMOVE_PROGRAMS_64.Version, SMS_G_System_ADD_REMOVE_PROGRAMS_64.Publisher, SMS_G_System_ADD_REMOVE_PROGRAMS_64.InstallDate from SMS_R_System inner join SMS_G_System_ADD_REMOVE_PROGRAMS_64 on + SMS_G_System_ADD_REMOVE_PROGRAMS_64.ResourceId = SMS_R_System.ResourceId inner join SMS_G_System_SYSTEM on + SMS_G_System_SYSTEM.ResourceId = SMS_R_System.ResourceId where SMS_G_System_SYSTEM.Name = '$ComputerName'" + + $SCCMX86Query = "select SMS_G_System_ADD_REMOVE_PROGRAMS.DisplayName, SMS_G_System_ADD_REMOVE_PROGRAMS.Version, SMS_G_System_ADD_REMOVE_PROGRAMS.Publisher, SMS_G_System_ADD_REMOVE_PROGRAMS.InstallDate from SMS_R_System inner join SMS_G_System_ADD_REMOVE_PROGRAMS on + SMS_G_System_ADD_REMOVE_PROGRAMS.ResourceID = SMS_R_System.ResourceId inner join SMS_G_System_SYSTEM on + SMS_G_System_SYSTEM.ResourceID = SMS_R_System.ResourceId where SMS_G_System_SYSTEM.Name = '$ComputerName'" + + $64apps = Get-WmiObject -namespace $SCCMNameSpace -DirectRead -computer $SCCMServer -query $SCCMX64Query | + Select-Object -Property DisplayName | Add-Member -NotePropertyName x86/x64 -NotePropertyValue 64 -PassThru | Sort-Object -Property DisplayName + + $86apps = Get-WmiObject -namespace $SCCMNameSpace -DirectRead -computer $SCCMServer -query $SCCMX86Query | + Select-Object -Property DisplayName | Add-Member -NotePropertyName x86/x64 -NotePropertyValue 32 -PassThru | Sort-Object -Property DisplayName + + return $86apps, $64apps +} + +Function Get-AssetConversion($ComputerName){ + + #Set Asset Tag to $comp + $assetTagSCCM = $ComputerName + + $SCCMQuery = "select SMS_R_System.Name from SMS_R_System inner join SMS_G_System_SYSTEM_ENCLOSURE on SMS_G_System_SYSTEM_ENCLOSURE.ResourceId = SMS_R_System.ResourceId Where SMS_G_System_SYSTEM_ENCLOSURE.SMBIOSAssetTag = '$assetTagSCCM'" + + #Set $comp to find hostname of PC + $nameHolder = Get-WmiObject -namespace $SCCMNameSpace -DirectRead -computer $SCCMServer -query $SCCMQuery | Select-Object -First 1 -ExpandProperty Name + + if($null -ne $nameHolder){ + $ComputerName = $nameHolder + return $ComputerName + } + + return $null +} + +Function HostnamesByPrinter($ComputerName){ + + $FindHostnamesByPrinterQuery = "select SMS_R_System.Name from SMS_R_System inner join SMS_G_System_PRINTER_DEVICE on + SMS_G_System_PRINTER_DEVICE.ResourceId = SMS_R_System.ResourceId where SMS_G_System_PRINTER_DEVICE.Name = '$ComputerName'" + + $FindHostnamesByPrinterSCCM = Get-WmiObject -namespace $SCCMNameSpace -DirectRead -computer $SCCMServer -query $FindHostnamesByPrinterQuery | + Select-Object -ExpandProperty Name + + Return $FindHostnamesByPrinterSCCM + +} + +Function Get-ServiceTagConversion($ComputerName){ + + #Set Service Tag to $comp + $ServiceTagSCCM = $ComputerName + + $SCCMQuery = "select SMS_R_System.Name from SMS_R_System inner join SMS_G_System_SYSTEM_ENCLOSURE on + SMS_G_System_SYSTEM_ENCLOSURE.ResourceId = SMS_R_System.ResourceId where SMS_G_System_SYSTEM_ENCLOSURE.SerialNumber = '$ServiceTagSCCM'" + + #Set $comp to find hostname of PC + $nameHolder = Get-WmiObject -namespace $SCCMNameSpace -DirectRead -computer $SCCMServer -query $SCCMQuery | Select-Object -First 1 -ExpandProperty Name + + if($null -ne $nameHolder) + { + $ComputerName = $nameHolder + return $ComputerName + } + +} + +Function Get-SCCM_UserLastLoggedOn($user) { + $SCCMUserQuery = "select distinct SMS_R_System.Name from SMS_R_System where SMS_R_System.LastLogonUserName = '$user'" + + $computerSCCM = Get-WmiObject -namespace $SCCMNameSpace -DirectRead -computer $SCCMServer -query $SCCMUserQuery | Select-Object -ExpandProperty Name + return $computerSCCM } \ No newline at end of file diff --git a/Private/SHSUser.ps1 b/Private/SHSUser.ps1 index cbc8ca0..d62a12f 100644 --- a/Private/SHSUser.ps1 +++ b/Private/SHSUser.ps1 @@ -37,16 +37,8 @@ return $obj } $manager = $UserInfo.Manager.Split("=,")[1] - - $SiteName="100" - - $SCCMServer="shscm01.int.samhealth.net" - - $SCCMNameSpace="root\sms\site_100" - - $SCCMUserQuery = "select distinct SMS_R_System.Name from SMS_R_System where SMS_R_System.LastLogonUserName = '$user'" - $computerSCCM = Get-WmiObject -namespace $SCCMNameSpace -DirectRead -computer $SCCMServer -query $SCCMUserQuery | Select-Object -ExpandProperty Name + $computerSCCM = Get-SCCM_UserLastLoggedOn $user $computerList = $computerSCCM -join ", " $props = [ordered]@{ Name=$UserInfo.DisplayName diff --git a/Private/ServiceTagConversion.ps1 b/Private/ServiceTagConversion.ps1 deleted file mode 100644 index ff12b6b..0000000 --- a/Private/ServiceTagConversion.ps1 +++ /dev/null @@ -1,23 +0,0 @@ -Function Get-ServiceTagConversion($ComputerName){ - - $SiteName="100" - - $SCCMServer="shscm01.int.samhealth.net" - - $SCCMNameSpace="root\sms\site_100" - #Set Service Tag to $comp - $ServiceTagSCCM = $ComputerName - - $SCCMQuery = "select SMS_R_System.Name from SMS_R_System inner join SMS_G_System_SYSTEM_ENCLOSURE on - SMS_G_System_SYSTEM_ENCLOSURE.ResourceId = SMS_R_System.ResourceId where SMS_G_System_SYSTEM_ENCLOSURE.SerialNumber = '$ServiceTagSCCM'" - - #Set $comp to find hostname of PC - $nameHolder = Get-WmiObject -namespace $SCCMNameSpace -DirectRead -computer $SCCMServer -query $SCCMQuery | Select-Object -First 1 -ExpandProperty Name - - if($null -ne $nameHolder) - { - $ComputerName = $nameHolder - return $ComputerName - } - -} \ No newline at end of file diff --git a/Private/UserProfileBackup.ps1 b/Private/UserProfileBackup.ps1 index dbc22eb..fcec9cb 100644 --- a/Private/UserProfileBackup.ps1 +++ b/Private/UserProfileBackup.ps1 @@ -135,10 +135,8 @@ New-Item -Path $Destination -ItemType Directory | Write-Verbose } elseif ($site -eq $numSites) { $BackupDestination = Read-Host "Enter a PC asset, hostname, or custom path" - # Parse input for asset tag, hostname, then full path - if ($BackupDestination -match "^[1-9]{5}$") { - $BackupDestination = Get-AssetConversion $BackupDestination - } + # Will attempt to convert to Hostname if possible, doesn't change if not + $BackupDestination = Get-Hostname $BackupDestination # Treat as hostname and see if it's reachable if (Test-Connection -ComputerName $BackupDestination -Count 1) { # If so, get the filesystem path diff --git a/Private/UserProfileTransfer.ps1 b/Private/UserProfileTransfer.ps1 index f4923bd..e60e412 100644 --- a/Private/UserProfileTransfer.ps1 +++ b/Private/UserProfileTransfer.ps1 @@ -4,7 +4,7 @@ .SYNOPSIS Backs up a user profile to a specified filepath. .DESCRIPTION - Takes a input of hostname and user and will make a copy of the user profile + Takes a input of a user profile filepath, will make a copy of the user profile and the Microsoft Roaming Folder and back it up to a specified filepath. #> diff --git a/Private/ViewerRemote.ps1 b/Private/ViewerRemote.ps1 index bcc43f3..97463bf 100644 --- a/Private/ViewerRemote.ps1 +++ b/Private/ViewerRemote.ps1 @@ -6,7 +6,6 @@ break } - $SiteName = "100" $RemoteViewerPath = "C:\Program Files (x86)\ConfigMgr\bin\i386\CmRcViewer.exe" $RemoteViewerPathAdd = "C:\Program Files (x86)\ConfigMgr Console\bin\i386\CmRcViewer.exe" $RemoteViewerPathAdd2 = "C:\Program Files (x86)\Microsoft Configuration Manager\AdminConsole\bin\i386\CmRcViewer.exe" diff --git a/Public/Get-PC.ps1 b/Public/Get-PC.ps1 index c6778ab..c2aa1ff 100644 --- a/Public/Get-PC.ps1 +++ b/Public/Get-PC.ps1 @@ -178,89 +178,50 @@ Function Get-PC { continue } - #Asset Tag Check - if ($comp.length -eq 5 -and !$SHSPrinter) { - $assettag = $comp - $comp = Get-AssetConversion $comp - - if ($null -eq $comp) { - Write-Host "`n$assettag Asset Tag not in SMBIOS or CMDB" -ForegroundColor Red - - $props = [Ordered]@{ - Hostname = "$assettag Asset Tag not in SMBIOS" - Status = "" - 'Current User' = "" - 'Last User(s)' = "" - 'IP | MAC' = "" - Model = "" - 'OS' = "" - 'OS Build' = "" - 'BIOS Ver' = "" - Encryption = "" - 'Free Space' = "" - RAM = "" - 'SSO Client' = "" - 'Kiosk Role' = "" - 'Citrix Ver' = "" - 'Asset Tag' = "" - 'Service Tag' = "" - 'Last Reboot' = "" - 'TPM Status' = "" - 'MBAM GPO' = "" - Printers = "" - } - $obj = New-Object -TypeName PSObject -Property $props - - $outPutArray += $obj - - continue + $oldcomp = $comp + $comp = Get-Hostname $comp + if (($oldcomp -eq $comp) -and (($comp.length -eq 5 -and !$SHSPrinter) -or ($comp.length -eq 7))) { + if ($comp.length -eq 5 -and !$SHSPrinter) { + $msg = "$comp Asset Tag not in SMBIOS or CMDB" + Write-Host "`n$msg" -ForegroundColor Red } - - #Service Tag Check - } - elseif ($comp.length -eq 7) { - if (Resolve-DnsName $comp) { - #DREW SUCKS + elseif ($comp.length -eq 7) { + $msg = "$comp Service Tag not found in SCCM" + Write-Host "`n$msg" -ForegroundColor Red } - else { - $serviceTag = $comp - $comp = Get-ServiceTagConversion $comp - if ($null -eq $comp) { - Write-Host "`n$serviceTag Service Tag not found in SCCM" -ForegroundColor Red - - $props = [Ordered]@{ - Hostname = "$serviceTag Service Tag not found in SCCM" - Status = "" - 'Current User' = "" - 'Last User(s)' = "" - 'IP | MAC' = "" - Model = "" - 'OS' = "" - 'OS Build' = "" - 'BIOS Ver' = "" - Encryption = "" - 'Free Space' = "" - RAM = "" - 'SSO Client' = "" - 'Kiosk Role' = "" - 'Citrix Ver' = "" - 'Asset Tag' = "" - 'Service Tag' = "" - 'Last Reboot' = "" - 'TPM Status' = "" - 'MBAM GPO' = "" - Printers = "" - } - $obj = New-Object -TypeName PSObject -Property $props - - $outPutArray += $obj - - continue - } + $props = [Ordered]@{ + Hostname = "$msg" + Status = "" + 'Current User' = "" + 'Last User(s)' = "" + 'IP | MAC' = "" + Model = "" + 'OS' = "" + 'OS Build' = "" + 'BIOS Ver' = "" + Encryption = "" + 'Free Space' = "" + RAM = "" + 'SSO Client' = "" + 'Kiosk Role' = "" + 'Citrix Ver' = "" + 'Asset Tag' = "" + 'Service Tag' = "" + 'Last Reboot' = "" + 'TPM Status' = "" + 'MBAM GPO' = "" + Printers = "" } + $obj = New-Object -TypeName PSObject -Property $props + + $outPutArray += $obj + + continue } + #DREW SUCKS + #Pulls basic SNMP Data from printer $comp if ($SHSPrinter) { Write-Progress -Activity "Querying Printers" -Status "$comp ($PCID/$NumberofComputers)" -PercentComplete (($PCID / $NumberofComputers) * 100) diff --git a/patchnotes.txt b/patchnotes.txt index f0ea819..ca1a4b1 100644 --- a/patchnotes.txt +++ b/patchnotes.txt @@ -1,3 +1,7 @@ +Patch: 2024-07-12 +-Refactored SCCM Queries +-Fix Get-PC now accepts IP addresses and MAC addresses along with asset tags and service tags + Patch: 2024-07-11 -Update Monitor to detect when printers go online and offline