-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathTapsa-tasks.psm1
275 lines (242 loc) · 9.38 KB
/
Tapsa-tasks.psm1
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
# ---
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#
# ---
Function Get-Dice {
<#
.SYNOPSIS
Throw dice
.DESCRIPTION
Returns random value 1-6
.EXAMPLE
Get-Dice
Returns random value 1-6
#>
[CmdletBinding()]
Param()
$result = Get-Random -Minimum 1 -Maximum 7
return $result
}
Function Get-Employee {
<#
.SYNOPSIS
Search Active Directory accounts by title or name
.DESCRIPTION
Retrieves names and job titles based on search terms. Name or job title must be used as parameter.
.PARAMETER search
One or multiple search terms separated by ','. Search by job title or name.
.EXAMPLE
Get-Employee -search janitor,teacher
Retrieves all janitors and teachers
.EXAMPLE
Get-Employee -search John
Retrieves everyone named John
#>
[CmdletBinding()]
Param
(
[Parameter(Mandatory=$True)]
[string]$search
)
$searchlist = $search.Split(",") # Separate search terms
Try
{
foreach ($search in $searchlist)
{
# Search AD user by Title
$wildsearch = "*$search*"
$thisresult = Get-ADUser -Filter {title -like $wildsearch -and enabled -eq $true} -Properties title | fl name,title
# Search AD user by givenname/surname/fullname
if ("$thisresult" -eq '')
{
$surname = $search.Split(" ")|select -Last 1
$givenname = $search.Split(" ")|select -First 1
$fullname = "$surname $givenname"
$thisresult = Get-ADUser -Filter {givenname -like $search -or surname -like $search -or name -like $search -or name -like $fullname} -Properties title | fl name,title | Out-String
}
if ("$thisresult" -eq '')
{
$thisresult = "$search not found"
}
$result += "`n`n"+"$thisresult".trim()
}
}
Catch
{
write-host "fail $error"
$result = "Something went wrong"
}
return $result
}
function Start-AADsync {
<#
.SYNOPSIS
Start Azure AD synchronization.
.DESCRIPTION
This function executes AAD Sync delta synchronization command on a remote server that has Azure AD syncronization tool installed.
.PARAMETER ComputerName
A single computer name.
.EXAMPLE
Start-AADsync -ComputerName server01
Run Azure AD sync on server01
#>
[CmdletBinding()]
Param
(
[Parameter(Mandatory=$True)]
[string]$ComputerName
)
Try
{
# Credentials
$user = "admin@domain.com"
$pass = ConvertTo-SecureString -string "Password1245" -Force -AsPlainText
$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $user,$pass
# Run remote commands
Invoke-Command -Credential $cred -ComputerName $ComputerName {
Import-Module ADSync
Start-ADSyncSyncCycle -PolicyType Delta
}
# Send information about the progress. Note this works only from Skype-bot!! You need to remove this part if you want to use the function elsewhere.
#############################################################
$info = "Synchronization started, please wait 1 minute"
$msg = New-Object "System.Collections.Generic.Dictionary[Microsoft.Lync.Model.Conversation.InstantMessageContentType,String]"
$msg.Add(0, $info)
Send -msg $msg
sleep 60
############################################################# END OF BOT RELATED CODE
$result = "Synchronization finished"
}
Catch
{
write-host "fail $error"
$result = "Something went wrong"
}
return $result
}
Function Get-Lunch {
<#
.SYNOPSIS
Retreives lunch details from Sharepoint Online intranet-site.
.DESCRIPTION
This function connects to Sharepoint Online (Office 365) and retrieves this week's lunch menu from a sharepoint-list. It is done by parsing the RSS-feed data of the list. Lunch for current day is displayed.
.EXAMPLE
Get-Lunch
Retrieves lunch info
#>
[CmdletBinding()]
Param()
$date = get-date -Format %d.%M.20%y # make sure this is in the same format as your date in the sharepoint list
# Credentials
$user = "user@domain.com"
$password = ConvertTo-SecureString -string "Password1" -Force -AsPlainText
$credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($user, $password)
Try
{
Try
{
if (!(Test-Path .\$date.xml)) # Get a new list if the xml list doesn't exist
{
# Sharepoint client assemblies
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client") | Out-Null
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client.Runtime") | Out-Null
# Sharepoint list details
$WebUrl = "https://domain.sharepoint.com/somesite" # sharepoint online site
$ListId = "{B26413C4-75A2-482A-87D3-4284094D69D0}" # ID of the sharepoint list
$listRssUrl = "$WebUrl/_layouts/15/listfeed.aspx?List=$ListId" # RSS URL
# Download RSS
$client = New-Object System.Net.WebClient
$client.Credentials = $credentials
$client.Headers.Add("X-FORMS_BASED_AUTH_ACCEPTED", "f")
$content = $client.DownloadString($listRssUrl)
$client.Dispose()
# Save RSS as XML
[xml]$rss = $content
$rss.save(".\$date.xml")
}
# Read XML and parse content
$lunchxml = [xml](Get-Content ".\$date.xml")
$menu = $lunchxml.rss.channel.item.description."#cdata-section" -replace "<.*?>" -split "´n"
}
Catch
{
write-host $error
$result = "Something went wrong"
}
# pick today's lunch details from menu
$lunch = $menu | where { ($_.contains("$date") -eq $true)}
if ($lunch -eq $null)
{
$lunch = "I don't know the lunch for the date: $date"
}
$result = $lunch
}
Catch
{
write-host "Food fail: $error"
$result = "Everything went wrong"
}
return $result
}
function Get-License {
<#
.SYNOPSIS
Check user's Office 365 license or assign a new license
.DESCRIPTION
Connects to Office 365 and retrieves license information of a single user or assigns a new license
.PARAMETER upn
User email-address / userprincipalname
.PARAMETER assign
Assign O365 license, $true or $false (default)
.EXAMPLE
Get-License -user john.smith@domain.com
.EXAMPLE
Get-License -user john.smith@domain.com -assign $true
Assign O365 license
#>
[CmdletBinding()]
Param(
[Parameter(Mandatory=$True)]
[string]$upn,
[string]$assign=$false
)
Try
{
# Connect to Office 365
$user = "admin@domain.com"
$pass = ConvertTo-SecureString -string "Password1234" -Force -AsPlainText
$O365Cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $user,$pass
$O365Session = New-PSSession –ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell -Credential $O365Cred -Authentication Basic -AllowRedirection
Import-PSSession $O365Session -AllowClobber | Out-Null
Connect-MsolService –Credential $O365Cred
# Office 365 tenant
$tenant = "domain"
# Remove skype messaging related stuff
$upn = $upn.Replace("o365 license","").Replace("set","").Trim()
$upn = $upn.split(" ") | select -First 1 # Skype automatically formats email-addresses as "name@domain.com <mailto:name@domain.com>" so we select just the first one.
# Assign Office 365 license if Assign parameter is $true
if ($assign)
{
Set-MsolUser -UserPrincipalName $upn -UsageLocation "FI" # Usage location for Finland
Set-MsolUserLicense -UserPrincipalName $upn -AddLicenses ($tenant+":STANDARDWOFFPACK")
}
# retrieve license info
$result = Get-MsolUser -UserPrincipalName $upn | fl displayname,licenses
}
Catch
{
write-host "fail: $error"
$result = "Something went wrong"
}
Finally
{
# Close Office 365 session
Remove-PSSession $O365Session
}
return $result
}