forked from jagilber/powershellScripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Umdh-task-2k8.ps1
138 lines (113 loc) · 4.84 KB
/
Umdh-task-2k8.ps1
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
# This script will help with the deployment and undeployment of umdh.
#
# File Name : umdh-manager-2k8.ps1
# Author : jagilber
# Version : 150413
#
$scriptName = "umdh-manager-2k8.ps1"
$logFile = "%systemroot%\temp\umdh-manager-2k8.log"
$sleepTimeMins = 60
$processWaitMs = 100
# ----------------------------------------------------------------------------------------------------------------
function main()
{
runas-admin
set-location $psscriptroot
$workingDir = (get-location).path
$umdhExe = "$($workingDir)\umdh.exe"
if(![System.IO.File]::Exists($umdhExe))
{
log-info "$($umdhExe) does not exist. copy umdh.exe into same directory as script. exiting"
return
}
#[Environment]::SetEnvironmentVariable( "_NT_SYMBOL_PATH", "c:\mysymbols;srv*c:\mycache*http://msdl.microsoft.com/download/symbols", [System.EnvironmentVariableTarget]::Machine )
# verify termservice is in its own process
if((get-service -Name TermService).ServiceType -imatch "Win32ShareProcess")
{
#$retVal = Read-Host -Prompt "this will restart remote desktop services. existing connections will be dropped, but sessions will remain. clients can reconnect. is this ok? [yes|no]"
#if(![regex]::IsMatch($retVal,"yes",[System.Text.RegularExpressions.RegexOptions]::IgnoreCase))
#{
# log-info "exiting script. no changes made"
# return
#}
log-info "Error: remote desktop services is not in its own process. exiting."
return
#log-info "restarting remote desktop services. existing connections will be dropped, but sessions will remain. clients can reconnect."
#$retVal = run-process -processName "sc" -arguments "config termservice type= own" -wait $true
#$service = get-service -Name TermService
#Restart-Service -InputObject $service -Force
}
else
{
log-info "termservice already set to use its own process"
}
if((get-service -Name TermService).ServiceType -imatch "Win32ShareProcess")
{
log-info "Error:TermService not configured to run its own process."
log-info "Run script with -action deploy to change. This will require restart of terminal service. exiting..."
return
}
while($true)
{
$outputFile = [Environment]::ExpandEnvironmentVariables("%systemroot%\temp\umdh-$([System.DateTime]::Now.ToString(`"yy-mm-dd-HH-mm`")).txt")
$processId = (gwmi Win32_Service -Filter "Name LIKE 'TermService'").ProcessId
$arguments = "-p:$($processId) -f:$($outputFile)"
run-process -processName $umdhExe -arguments $arguments -wait $true
sleep -Seconds ($sleepTimeMins * 60)
}
}
# ----------------------------------------------------------------------------------------------------------------
function run-process([string] $processName, [string] $arguments, [bool] $wait = $false)
{
log-info "Running process $processName $arguments"
$exitVal = 0
$process = New-Object System.Diagnostics.Process
$process.StartInfo.UseShellExecute = $false
$process.StartInfo.RedirectStandardOutput = $true
$process.StartInfo.RedirectStandardError = $true
$process.StartInfo.FileName = $processName
$process.StartInfo.Arguments = $arguments
$process.StartInfo.CreateNoWindow = $true
#only needed if useshellexecute is true
$process.StartInfo.WorkingDirectory = get-location #$workingDirectory
[void]$process.Start()
if($wait -and !$process.HasExited)
{
$process.WaitForExit($processWaitMs)
$exitVal = $process.ExitCode
$stdOut = $process.StandardOutput.ReadToEnd()
$stdErr = $process.StandardError.ReadToEnd()
log-info "Process output:$stdOut"
if(![System.String]::IsNullOrEmpty($stdErr) -and $stdErr -notlike "0")
{
log-info "Error:$stdErr `n $Error"
$Error.Clear()
}
}
elseif($wait)
{
log-info "Process ended before capturing output."
}
#return $exitVal
return $stdOut
}
# ----------------------------------------------------------------------------------------------------------------
function log-info($data)
{
$data = "$([System.DateTime]::Now):$($data)`n"
#Write-Host $data
out-file -Append -InputObject $data -FilePath ([Environment]::ExpandEnvironmentVariables($logFile))
}
# ----------------------------------------------------------------------------------------------------------------
function runas-admin([string] $arguments)
{
if (!([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole( `
[Security.Principal.WindowsBuiltInRole] "Administrator"))
{
log-info "please restart script as administrator. exiting..."
exit
}
}
# ----------------------------------------------------------------------------------------------------------------
main
log-info "finished"