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
|
#ScriptInsideService.ps1
#Démo de Winsw.exe
<# Send-TSMessageBox
.SYNOPSIS
Send a message or prompt to the interactive user with the ability to get the results.
.DESCRIPTION
Allows the administrator to send a message / prompt to an interactive user.
.EXAMPLE
"Send a message immediately w/o waiting for a responce."
Send-TSMessageBox -Title "Email Problem" -Message "We are currently having delays and are working on the issue."
"Send a message waiting 60 seconds for a reponse of [Yes / No]."
$Result = Send-TSMessageBox -Title "System Updated" -Message "System requires a reboot. Would you like to the reboot system now?" `
-ButtonSet 4 -Timeout 60 -WaitResponse $true
.ButtonSets
0 = OK
1 = Ok/Cancel
2 = Abort/Retry/Ignore
3 = Yes/No/Cancel
4 = Yes/No
5 = Retry/Cancel
6 = Cancel/Try Again/Continue
.Results
"" = 0
"Ok" = 1
"Cancel" = 2
"Abort" = 3
"Retry" = 4
"Ignore" = 5
"Yes" = 6
"No" = 7
"Try Again" = 10
"Continue" = 11
"Timed out" = 32000
"Not set to wait" = 32001
.NOTES
Author: Raymond H Clark
Twitter: @Rowdybullgaming
.RESOURCES
http://technet.microsoft.com/en-us/query/aa383488
http://technet.microsoft.com/en-us/query/aa383842
http://pinvoke.net/default.aspx/wtsapi32.WTSSendMessage
#>
Function Send-TSMessageBox
{
param([string]$Title = "Title", [string]$Message = "Message", [int]$ButtonSet = 0, [int]$Timeout = 0, [bool]$WaitResponse = $false)
$Signature = @"
[DllImport("wtsapi32.dll", SetLastError = true)]
public static extern bool WTSSendMessage(
IntPtr hServer,
[MarshalAs(UnmanagedType.I4)] int SessionId,
String pTitle,
[MarshalAs(UnmanagedType.U4)] int TitleLength,
String pMessage,
[MarshalAs(UnmanagedType.U4)] int MessageLength,
[MarshalAs(UnmanagedType.U4)] int Style,
[MarshalAs(UnmanagedType.U4)] int Timeout,
[MarshalAs(UnmanagedType.U4)] out int pResponse,
bool bWait);
[DllImport("kernel32.dll")]
public static extern uint WTSGetActiveConsoleSessionId();
"@
[int]$TitleLength = $Title.Length;
[int]$MessageLength = $Message.Length;
[int]$Response = 0;
$MessageBox = Add-Type -memberDefinition $Signature -name "WTSAPISendMessage" -namespace "WTSAPI" -passThru
$SessionId = $MessageBox::WTSGetActiveConsoleSessionId()
#todo return objet ResultOfWTSSendMessage
$result=$MessageBox::WTSSendMessage(0, $SessionId, $Title, $TitleLength, $Message, $MessageLength, $ButtonSet, $Timeout, [ref] $Response, $WaitResponse)
$Response
}
function Test-IsAdministrator
{
param()
$currentUser = [Security.Principal.WindowsIdentity]::GetCurrent()
(New-Object Security.Principal.WindowsPrincipal $currentUser).IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator)
}
Function Get-ParentProcess {
#Permet de retrouver le process parent ayant exécuté
#la session Powershell exécutant ce script/module
param($ID)
$parentID=$ID
$Result=@(
Do {
$Process=Get-WmiObject Win32_Process -Filter "ProcessID='$parentID'" -property Name,CommandLine,ParentProcessID
#Permet de retrouver la tâche appelante
#On peut inclure le nom de la tâche dans la ligne d'appel :
# ... -Command "$TaskName='MyTask';..."
#$Logger.DebugFormat("Name :{0}`t cmdLine={1}",@($Process.name,$Process.CommandLine))
$parentID=$Process.ParentProcessID
try {
(get-process -ID $parentID).Name
$exit=$true
}
catch [Microsoft.PowerShell.Commands.ProcessCommandException] {
$exit=$false
}
} until ($Exit)
)
$ofs='.'
[Array]::Reverse($Result)
"$Result"
} #Get-ParentProcess
$title = "Test DialogBox inside a service"
$message = "Message Yes No Cancel"
$Result=Send-TSMessageBox -Title $title -Message $message -ButtonSet 3 -WaitResponse $true -Timeout 60
$ResultTSMessageBox =@{
0=""
1="Ok"
2="Cancel"
3="Abort"
4="Retry"
5="Ignore"
6="Yes"
7="No"
10="Try Again"
11="Continue"
32000="Timed out"
32001="Not set to wait"
}
$title = "DialogBox result"
$ParentProcess=Get-ParentProcess $pid
$message = @"
Le process parent = $ParentProcess
compte administrateur : $(Test-IsAdministrator)
Resultat précédent = $($ResultTSMessageBox.$result)
"@
$Result=Send-TSMessageBox -Title $title -Message $message -ButtonSet 1 -WaitResponse $true -Timeout 60 |
Partager