Skip to content

Instantly share code, notes, and snippets.

@john-clark
Created October 25, 2013 14:48
Show Gist options
  • Save john-clark/7155853 to your computer and use it in GitHub Desktop.
Save john-clark/7155853 to your computer and use it in GitHub Desktop.
wip to automate remote assistance
<job id="Partially works">
'filename raa.wsf
<script language="VBScript">
'setup
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Const TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0
Set oShell = createobject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objHTTP = CreateObject("Microsoft.XMLHTTP")
Set WMI = GetObject("winmgmts:!\\.\root\cimv2")
bolDebug = True
strUserProfile = oShell.ExpandEnvironmentStrings("%USERPROFILE%")
strWinDir = oShell.ExpandEnvironmentStrings("%SYSTEMROOT%")
strTicket = objFSO.BuildPath(strUserProfile,"somehostname.msrcincident")
'del ticket file
DelAticket
'close ra if open
pid = WaitForProcess("msra.exe", 5)
Do while pid > 0
oShell.AppActivate pid
oShell.Sendkeys "%{F4}"
WScript.Sleep 200
oShell.Sendkeys "{Enter}"
WScript.Sleep 200
pid = WaitForProcess("msra.exe", 5)
Loop
DebugPrint "MSRA closed"
'enable remote desktop
oShell.run "reg add "& chr(34) & "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Terminal Server"& chr(34) & " /v fDenyTSConnections /t REG_DWORD /d 0 /f"
'oShell.RegWrite "HKEY_LOCAL_MACHINE\system\currentcontrolset\control\Terminal Server\fDenyTSConnections",0,"REG_DWORD"
'open firewall
ExecAcmd("netsh firewall Set service REMOTEDESKTOP ENABLE")
ExecAcmd("netsh firewall set service REMOTEAdmin ENABLE")
ExecAcmd("netsh advfirewall firewall set rule group="& chr(34) & "remote desktop"& chr(34) & " new enable=Yes")
ExecAcmd("netsh advfirewall firewall set rule group="& chr(34) & "file and printer sharing"& chr(34) & " new enable=Yes")
DebugPrint "firewall opened and TS allowed"
'start support
oShell.run "%windir%\system32\msra.exe /saveasfile %USERPROFILE%\somehostname somehostname"
WScript.Sleep 1000
pid = WaitForProcess("msra.exe", 5)
If pid > 0 Then
oShell.AppActivate pid
oShell.Sendkeys "%c"
End If
'wait for ticket
DebugPrint "msra started ticket [" & strTicket & "] created"
'read in ticket file
If objFSO.FileExists(strTicket) then
Set objFileStream = objFSO.OpenTextFile(strTicket, ForReading)
strText = objFileStream.ReadAll
objFileStream.Close
DebugPrint "ticket open"
DebugPrint ""
'post ticket file
'objHTTP.open "POST", "http://support.somehostname.com/ticket.php", False
'objHTTP.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
'objHTTP.send strText
'show response
'DebugPrint objHTTP.responseText
'show ticket
MsgBox strText
'DebugPrint strText
Else
DebugPrint "ticket not found"
End If
'wait for connection
'click yes to connect
'wait for process to end
pid = WaitForProcess("msra.exe", 5)
Do while pid > 0
DebugPrint "Waiting for MSRA to quit"
Wscript.Sleep 2000
pid = WaitForProcess("msra.exe", 5)
Loop
'cleanup
DelAticket
Set objHTTP = Nothing
Set objFSO = Nothing
Set oShell = Nothing
'end
'subs
Sub DelAticket
if objFSO.FileExists(strTicket) then
objFSO.DeleteFile(strTicket)
DebugPrint "ticket deleted"
else
DebugPrint "no ticket"
End if
End Sub
Sub ExecAcmd(filespec)
Dim oExec
Set oExec = oShell.Exec(filespec)
Do While oExec.Status = 0 : WScript.Sleep 100 : Loop
End Sub
Function WaitForProcess(imageName, tries)
Dim wql, process
wql = "SELECT ProcessId FROM Win32_Process WHERE Name = '" & imageName & "'"
WaitForProcess = 0
While tries > 0 And WaitForProcess = 0
For Each process In WMI.ExecQuery(wql)
WaitForProcess = process.ProcessId
Next
If WaitForProcess = 0 Then
WScript.Sleep 500
tries = tries - 1
End If
Wend
End Function
Function DebugPrint(DebugStr)
If bolDebug = True Then
Wscript.Echo Time & " - " & DebugStr
End If
End Function
</script>
</job>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment