Merge branch 'HostnameRefactor'

This commit is contained in:
Zachary Gorman 2024-07-12 14:32:19 -07:00
commit f48cb0861d
14 changed files with 165 additions and 171 deletions

View file

@ -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) {

View file

@ -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
}

24
Private/Get-Hostname.ps1 Normal file
View file

@ -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
}

View file

@ -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
}

View file

@ -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

View file

@ -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

View file

@ -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
}

View file

@ -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

View file

@ -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
}
}

View file

@ -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

View file

@ -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.
#>

View file

@ -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"

View file

@ -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)

View file

@ -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