Skip to content

Instantly share code, notes, and snippets.

@Mazarith

Mazarith/--PC Lookup--.ps1

Last active Jul 24, 2019
Embed
What would you like to do?
If ($PCName -contains "-","_") {
#If it looks like a server, ignore it.
$LookupTest = "NWS"
#####This Should Trigger first, ignoring the rest of the statements#####
#####This looks for anything that follows our standard naming convention, granted nothing gets through the first If statement,
#and the other elseif statements I removed#####
} elseif ($PCName -like "*X[0-9][0-9][0-9][0-9][0-9][0-9]") {
$LookupTest = Get-ADComputer -Filter {SAMAccountName -like '$SAMName'} -Properties Name, LastLogonDate | Select Name, LastLogonDate
#If it doesn't match, do a wildcard search
if ($LookupTest -eq $null ) {
#Splits and sets up name for wildcard search
$SplitName = $PCName -Split '(?=X)'
$SplitName = $SplitName[1]
$LookupTest = "Get-ADComputer -Filter {SAMAccountName -like '*$SplitName'} -Properties LastLogonDate | Select Name, LastLogonDate
}
} else {
#If it looks like anything else, ignore it.
$LookupTest = "NWS"
}
#####This next part outputs the corresponding text to my CSV file######
if ($LookupTest -eq $null ) {
#If it doesn't exist, it outputs an error
$WSNF = "WS-$PCName not found, $PCName"
$WSNF
Out-File -FilePath "./$outputname" -InputObject $WSNF -Force -Append -Encoding UTF8
} elseif ($LookupTest -eq "NWS") {
#If it's not a workstation, it says so
$NWS = "NWS - $PCName is not a workstation, $PCName"
$NWS
Out-File -FilePath "./$outputname" -InputObject $NWS -Force -Append -Encoding UTF8
} else {
#If it does exist, it outputs
Echo $LookupTest
$LookupTest | Export-CSV -Path "./$outputname" -Force -Append
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment