[英]Msgbox in PowerShell script run from task scheduler not working
I have a PowerShell script that creates a schedule task to launch the script. 我有一个PowerShell脚本,该脚本创建计划任务以启动脚本。 The idea is there are some task in the script that requires reboot.
这个想法是脚本中有一些任务需要重新启动。 At the end of the PowerShell a message box should prompt the user to let the user knows that all the tasks are completed.
在PowerShell的末尾,将出现一个消息框,提示用户让用户知道所有任务都已完成。 What am i doing wrong?
我究竟做错了什么?
Add-Type -AssemblyName PresentationFramework
TaskName = "Run Agents Install Script"
$TaskDescription = "Run Agents Install Script at logon"
$Action = New-ScheduledTaskAction -Execute 'Powershell.exe' `
-Argument "-executionpolicy remotesigned -File $PSScriptRoot\AgentInstall.ps1"
$Trigger = New-ScheduledTaskTrigger -AtLogOn
Register-ScheduledTask -Action $Action -Trigger $Trigger -TaskName $TaskName -Description $TaskDescription -User "System"
$MsgBoxInput = [System.Windows.MessageBox]::Show('Installation completed successfully.','Agent Install','OK')
Switch ($MsgBoxInput) {
'OK'
{
$MsgBoxInput = [System.Windows.MessageBox]::Show('WARNING! Please install Imprivata agent manually if applicable.','Agent Install','OK')
}
}
One option is to use the Terminal Services API to send a message to the console. 一种选择是使用终端服务API将消息发送到控制台。 Unfortunately, it is native API, so you need to use .NET interop to call it, but in this case it isn't too tricky:
不幸的是,它是本机API,因此您需要使用.NET interop来调用它,但是在这种情况下,它并不是很棘手:
$typeDefinition = @"
using System;
using System.Runtime.InteropServices;
public class WTSMessage {
[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
);
static int response = 0;
public static int SendMessage(int SessionID, String Title, String Message, int Timeout, int MessageBoxType) {
WTSSendMessage(IntPtr.Zero, SessionID, Title, Title.Length, Message, Message.Length, MessageBoxType, Timeout, out response, true);
return response;
}
}
"@
Add-Type -TypeDefinition $typeDefinition
[WTSMessage]::SendMessage(1, "Message Title", "Message body", 30, 36)
This is essentially a thin wrapper to the WTSSendMessage function. 从本质上讲,这是WTSSendMessage函数的精简包装。
You will need to get the SessionID
via some tool like query
. 您将需要通过诸如
query
工具获取SessionID
。 This script might help with that: Get-UserSession . 该脚本可能会帮助您: Get-UserSession 。
The TimeOut
value here is 30, which means the pop-up will wait 30 seconds before returning with a value of '32000'. 这里的
TimeOut
值为30,这意味着弹出窗口将等待30秒,然后返回值32000。 Set to '0' to wait forever. 设置为“ 0”将永远等待。
The MessageBoxType
is a combination of the values for uType
here: MessageBox Function . MessageBoxType
是此处uType
的值的组合: MessageBox Function 。 So the '36' in the example is a combination of the values for 'MB_YESNO' and 'MB_ICONQUESTION', so will show a message with a question mark icon and 'yes'/'no' buttons. 因此,示例中的“ 36”是“ MB_YESNO”和“ MB_ICONQUESTION”的值的组合,因此将显示带有问号图标和“是” /“否”按钮的消息。 Note that the documentation gives the values in hexadecimal, so you'll need to convert them.
请注意,文档以十六进制形式提供值,因此您需要对其进行转换。
I tested this as a scheduled task running as an admin and it was able to show a message on the desktop of a different logged on user. 我将其作为管理员运行的计划任务进行了测试,它能够在其他登录用户的桌面上显示一条消息。 hope it helps.
希望能帮助到你。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.