forked from nickrod518/PowerShell-Scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSend-CatFactMessage.ps1
More file actions
executable file
·79 lines (66 loc) · 2.83 KB
/
Copy pathSend-CatFactMessage.ps1
File metadata and controls
executable file
·79 lines (66 loc) · 2.83 KB
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
function Send-CatFactMessage {
<#
.SYNOPSIS
Send a cat fact to users on a computer.
.DESCRIPTION
Send a random cat fact to any number of computers and all users or a specific user. Supports credential passing.
.EXAMPLE
Send-CatFactMessage -PlayAudio
Sends cat fact message to all users on localhost and outputs fact through speakers.
.EXAMPLE
Get-ADComputer -Filter * | Send-CatFactMessage -UserName JDoe -Credential (Get-Credential)
Send cat fact to jDoe on all AD computers. Prompt user for credentials to run command with.
.EXAMPLE
Send-CatFactMessage -ComputerName pc1, pc2, pc3
Send cat fact to all users on provided computer names.
.PARAMETER ComputerName
The computer name to execute against. Default is local computer.
.PARAMETER UserName
The name the user to display the message to. Default is all users.
.PARAMETER PlayAudio
Use Windows Speech Synthesizer to output the fact using text to speech.
.PARAMETER Credential
The credential object to execute the command with.
#>
[CmdletBinding(SupportsShouldProcess = $true)]
param (
[Parameter(
Mandatory = $false,
ValueFromPipeline = $true,
ValueFromPipelineByPropertyName = $true
)]
[string[]]$ComputerName = $env:COMPUTERNAME,
[Parameter(Mandatory = $false)]
[string]$UserName = '*',
[Parameter(Mandatory = $false)]
[switch]$PlayAudio,
[Parameter(Mandatory = $false)]
[PSCredential]$Credential
)
$CatFact = (ConvertFrom-Json (Invoke-WebRequest -Uri 'http://catfacts-api.appspot.com/api/facts')).facts
if ($pscmdlet.ShouldProcess("User: $UserName, Computer: $ComputerName", "Send cat fact, $CatFact")) {
$ScriptBlock = {
param (
[string]$UserName,
[string]$CatFact,
[bool]$PlayAudio = $false
)
msg $UserName $CatFact
if ($PlayAudio) {
Add-Type -AssemblyName System.Speech
$SpeechSynth = New-Object System.Speech.Synthesis.SpeechSynthesizer
$SpeechSynth.Speak($CatFact)
}
}
if ($Credential) {
Write-Verbose "Sending cat fact using credential $($Credential.UserName)"
Invoke-Command -ComputerName $ComputerName -ScriptBlock $ScriptBlock `
-ArgumentList $UserName, $CatFact, $PlayAudio -AsJob -Credential $Credential
} else {
Invoke-Command -ComputerName $ComputerName -ScriptBlock $ScriptBlock `
-ArgumentList $UserName, $CatFact, $PlayAudio -AsJob
}
Get-Job | Wait-Job | Receive-Job
Get-Job | Remove-Job
}
}