-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSet-ComputerDescription.ps1
120 lines (107 loc) · 4.76 KB
/
Set-ComputerDescription.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# Settings
$computersOU = "OU=Workstations,DC=sberbank,DC=hr"
$domainServer = "domain.local"
$logSource = "My-PowerShell"
# Variables
$computerUser = $null
$computerName = $null
$computerAlive = $false
$computerUser = $null
$computerUserArray = $null
$computerUserName = $null
$user = $null
$userInfo = $null
$newLine = [Environment]::NewLine
$scriptName = $MyInvocation.MyCommand.Name
$scriptPath = Split-Path -Parent $MyInvocation.MyCommand.Path
# Create Event Log Source
try
{
New-EventLog -LogName Application -Source $logSource -ErrorAction Stop
}
catch
{
if(($Error[0].Message.Contains("Access is denied")) -eq $true)
{
Write-Host "Cannot create Event Log source with name $logSource. Please create Event Log Source with name $logSource as Administrator, otherwise no Event logging will be performed."
}
}
Write-EventLog -LogName Application -Source $logSource -EntryType Information -EventId 26061 -Message "$scriptName${NewLine}cmdlet starting"
# Get list of computers
try
{
$computers = Get-ADComputer -Filter * -SearchBase $computersOU -SearchScope Subtree -Server $domainServer -ErrorAction Stop
Write-EventLog -LogName Application -Source $logSource -EntryType Information -EventId 26062 -Message "$scriptName${NewLine}Successfuly loaded a list of computers"
}
catch
{
Write-EventLog -LogName Application -Source $logSource -EntryType Error -EventId 26064 -Message "$scriptName${NewLine}Failed to get a list of computers"
}#end try
# Loop through all computers
foreach($computer in $computers)
{
$computerName = $computer.Name
# Check if computer is turned on
$computerAlive = Test-Connection -ComputerName $computerName -BufferSize 16 -Count 1 -Quiet
if($computerAlive -eq $true)
{
# Get loggedon user
try
{
$computerUser = Get-WmiObject -Class Win32_ComputerSystem -ComputerName $computerName -ErrorAction Stop | Select-Object UserName
Write-EventLog -LogName Application -Source $logSource -EntryType Information -EventId 26062 -Message "$scriptName${NewLine}Successfuly got $computerUser from $computerName"
}
catch
{
$computerUser = $null
Write-EventLog -LogName Application -Source $logSource -EntryType Error -EventId 26064 -Message "$scriptName${NewLine}Failed to get Win32_ComputerSystem WMI from $computerName"
}#end try
if($computerUser.UserName -ne $null)
{
$computerUserArray = $computerUser.UserName.Split("\")
$computerUserName = $computerUserArray[1]
# Get user from AD
try
{
$user = Get-ADUser -Filter {SamAccountName -eq $computerUserName} -Properties Initials, physicalDeliveryOfficeName -Server $domainServer
if($user -eq $null)
{
$userInfo = $null
Write-EventLog -LogName Application -Source $logSource -EntryType Warning -EventId 26063 -Message "$scriptName${NewLine}$computerUserName not found in Active Directory"
}
else
{
$userInfo = $user.Name + ", " + $user.physicalDeliveryOfficeName
Write-EventLog -LogName Application -Source $logSource -EntryType Information -EventId 26062 -Message "$scriptName${NewLine}Successfuly loaded user information: $userInfo"
}#end if
}
catch
{
$userInfo = $null
}#end try
# Set user information to Computer account description
if($userInfo -ne $null)
{
try
{
Set-ADComputer -Identity $computerName -Description $userInfo -Server $domainServer -ErrorAction Stop
Write-EventLog -LogName Application -Source $logSource -EntryType Information -EventId 26062 -Message "$scriptName${NewLine}Successfuly set $userInfo to computer $computerName"
}
catch
{
Write-EventLog -LogName Application -Source $logSource -EntryType Error -EventId 26064 -Message "$scriptName${NewLine}Failed to set user info to computer $computerName"
}#end try
}#end if
}#end if
}#end if
# Clear variables
$computerUser = $null
$computerName = $null
$computerAlive = $false
$computerUser = $null
$computerUserArray = $null
$computerUserName = $null
$user = $null
$userInfo = $null
}#end foreach
Write-EventLog -LogName Application -Source $logSource -EntryType Information -EventId 26061 -Message "$scriptName${NewLine}cmdlet finished"