Re: (Remote) Batch Writing/Execution
:thumbsup2:
:thumbsup2:
Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
LPCWSTR User = L"USER";
LPCWSTR Domain = L"DOMAIN";
HANDLE TokenH;
BOOL result = LogonUser(User, Domain, NULL, LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, &TokenH);
std::cout << (result ? "Successful logon!" : "Failed to logon...") << std::endl;
std::cin.get();
return 0;
int _tmain(int argc, _TCHAR* argv[])
{
LPCWSTR User = L"Guest";
LPCWSTR Domain = L"DOMAIN";
HANDLE TokenH;
BOOL result = LogonUser(User, Domain, NULL, LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, &TokenH);
printf_s("%s logon attempt.\nToken Handle: %d",
result ? "Successful" : "Failed",
result ? &TokenH : 0);
std::cin.get();
return 0;
}
/k echo Environment Var ^%USERNAME^% = %USERNAME%
@ECHO OFF && SETLOCAL EnableDelayedExpansion
CALL :GetIdentifiers
ECHO Exporting data for '%USER%' with SID:%SID%...
REG EXPORT HKU\%SID%\Network "%USERPROFILE%\Desktop\Network Drives.reg"
REG EXPORT HKU\%SID%\Printers "%USERPROFILE%\Desktop\Network Printers1.reg"
REG EXPORT "HKU\%SID%\Software\Microsoft\Windows NT\CurrentVersion\Devices" "%USERPROFILE%\Desktop\Network Printers2.reg"
REG EXPORT "HKU\%SID%\Software\Microsoft\Windows NT\CurrentVersion\PrinterPorts" "%USERPROFILE%\Desktop\Network Printers3.reg"
PAUSE && GOTO :EOF
:GetIdentifiers
CALL :GetUser && CALL :GetUserSID
goto :EOF
:GetUser
for /f "tokens=2 skip=1 delims=\ " %%G IN ('"wmic computersystem get Username"') DO (
set USER=%%G&& goto :EOF
)
:GetUserSID
FOR /F "skip=1" %%G IN ('"wmic path win32_useraccount where name='%USER%' get sid"') DO (
SET SID=%%G&& GOTO :EOF
)
@echo off
ver | find "XP" > nul
if %ERRORLEVEL% == 0 goto ver_xp
if not exist %SystemRoot%\system32\systeminfo.exe goto warnthenexit
systeminfo | find "OS Name" > %TEMP%\osname.txt
FOR /F "usebackq delims=: tokens=2" %%i IN (%TEMP%\osname.txt) DO set vers=%%i
echo %vers% | find "Windows 7" > nul
if %ERRORLEVEL% == 0 goto ver_7
goto warnthenexit
:ver_7
:Run Windows 7 specific commands here.
set os = win7
goto exit
:ver_xp
:Run Windows XP specific commands here.
set os = winxp
goto exit
:warnthenexit
echo Machine undetermined.
:exit
@echo off
ver | find "XP" > nul
if %ERRORLEVEL% == 0 goto ver_xp
if not exist %SystemRoot%\system32\systeminfo.exe goto warnthenexit
systeminfo | find "OS Name" > %TEMP%\osname.txt
FOR /F "usebackq delims=: tokens=2" %%i IN (%TEMP%\osname.txt) DO set vers=%%i
echo %vers% | find "Windows 7" > nul
if %ERRORLEVEL% == 0 goto ver_7
goto warnthenexit
:ver_7
:Run Windows 7 specific commands here.
set os = win7
goto Begin
:ver_xp
:Run Windows XP specific commands here.
set os = winxp
goto Begin
:warnthenexit
echo Machine undetermined.
goto Finish
:Begin
::------------------
::My Code Here
::Check %os% for winxp or win7
::------------------
:Finish
::Do something in the case not Windows 7 or XP Here... If anything
@echo off
ver | find "XP" > nul
if %ERRORLEVEL% == 0 goto ver_xp
if not exist %SystemRoot%\system32\systeminfo.exe goto warnthenexit
systeminfo | find "OS Name" > %TEMP%\osname.txt
FOR /F "usebackq delims=: tokens=2" %%i IN (%TEMP%\osname.txt) DO set vers=%%i
echo %vers% | find "Windows 7" > nul
if %ERRORLEVEL% == 0 goto ver_7
goto warnthenexit
:ver_7
:Run Windows 7 specific commands here.
set PATH = "C:\Users\%USER%\"
goto Begin
:ver_xp
:Run Windows XP specific commands here.
set PATH = "D:\Documents and Settings\%USER%\"
goto Begin
:warnthenexit
echo Machine undetermined.
goto Finish
:Begin
CALL :GetIdentifiers
ECHO Exporting data for '%USER%' with SID:%SID%...
REG EXPORT HKU\%SID%\Network "%PATH%\Network Drives.reg"
REG EXPORT HKU\%SID%\Printers "%PATH%\Network Printers1.reg"
REG EXPORT "HKU\%SID%\Software\Microsoft\Windows NT\CurrentVersion\Devices" "%PATH%\Network Printers2.reg"
REG EXPORT "HKU\%SID%\Software\Microsoft\Windows NT\CurrentVersion\PrinterPorts" "%PATH%\Network Printers3.reg"
PAUSE && GOTO :EOF
:GetIdentifiers
CALL :GetUser && CALL :GetUserSID
goto :EOF
:GetUser
for /f "tokens=2 skip=1 delims=\ " %%G IN ('"wmic computersystem get Username"') DO (
set USER=%%G&& goto :EOF
)
:GetUserSID
FOR /F "skip=1" %%G IN ('"wmic path win32_useraccount where name='%USER%' get sid"') DO (
SET SID=%%G&& GOTO :EOF
)
:Finish
@ECHO OFF && SETLOCAL EnableDelayedExpansion
CALL :GetIdentifiers
:Begin
ECHO Exporting data for '%USER%' with SID '%SID%' to '%STORETOPATH%'...
REG EXPORT HKU\%SID%\Network "%STORETOPATH%\Network Drives.reg"
REG EXPORT HKU\%SID%\Printers "%STORETOPATH%\Network Printers1.reg"
REG EXPORT "HKU\%SID%\Software\Microsoft\Windows NT\CurrentVersion\Devices" "%STORETOPATH%\Network Printers2.reg"
REG EXPORT "HKU\%SID%\Software\Microsoft\Windows NT\CurrentVersion\PrinterPorts" "%STORETOPATH%\Network Printers3.reg"
PAUSE && GOTO :EOF
:GetIdentifiers
CALL :GetUser && CALL :GetUserSID && CALL :version_finder
goto :EOF
:GetUser
for /f "tokens=2 skip=1 delims=\ " %%G IN ('"wmic computersystem get Username"') DO (
set USER=%%G&& goto :EOF
)
:GetUserSID
FOR /F "skip=1" %%G IN ('"wmic path win32_useraccount where name='%USER%' get sid"') DO (
SET SID=%%G&& GOTO :EOF
)
:version_finder
ver | find "XP" > nul
if %ERRORLEVEL% == 0 goto ver_xp
if not exist %SystemRoot%\system32\systeminfo.exe goto warnthenexit
systeminfo | find "OS Name" > %TEMP%\osname.txt
FOR /F "usebackq delims=: tokens=2" %%i IN (%TEMP%\osname.txt) DO set vers=%%i
echo %vers% | find "Windows 7" > nul
if %ERRORLEVEL% == 0 goto ver_7
goto warnthenexit
:ver_7
set STORETOPATH = "C:\Users\%USER%\"
goto :EOF
:ver_xp
set STORETOPATH = "D:\Documents and Settings\%USER%\"
goto :EOF
:warnthenexit
echo Machine undetermined.
goto Finish
:Finish
:warnthenexit
echo Machine undetermined.
goto Finish
:Finish
set STORETOPATH = "D:\Documents and Settings\%USER%\"
{SPACE}"D:\Documents and Settings\MYUSERNAME\"
set USER=%%G&& goto :EOF
Option Explicit
Dim strComputer
Dim objWMI
Dim colItems
Dim objItem
Dim VerBig
Dim OSystem
Dim WSHShell
Dim colItems2
Dim objItem2
Dim arrName
Dim domain
Dim currentUser
Dim colItems3
Dim objItem3
Dim SID
Dim objAccount
Dim storeToPath
' Here is where we interrogate the Operating System
' On Error Resume Next
' Get the computer name (ie . = localhost)
strComputer = InputBox("Computer Name or IP Address: ")
' Remote connection path
Set objWMI = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
' This is where WMI interrogates the operating system
Set colItems = objWMI.ExecQuery("Select * from Win32_OperatingSystem",,48)
' Here we filter Version from the dozens of properties
For Each objItem in colItems
VerBig = Left(objItem.Version,3)
Next
' Spot VerBig variable in previous section
' Note the output variable is called OSystem
' This is where WMI interrogates the computer system
Set colItems2 = objWMI.ExecQuery("Select * from Win32_ComputerSystem",,48)
Select Case VerBig
Case "6.1"
OSystem = "7"
Set WSHShell = CreateObject("WScript.Shell")
domain = WshShell.ExpandEnvironmentStrings("%userdomain%")
currentUser = WshShell.ExpandEnvironmentStrings("%username%")
Set objAccount = objWMI.Get("Win32_UserAccount.Name='" & currentUser & "',Domain='" & domain & "'")
SID = objAccount.SID
Case "5.1"
OSystem = "XP"
' Here we filter UserName from the dozens of properties
For Each objItem2 in colItems2
arrName = Split(objItem2.UserName, "\")
domain = arrName(0)
currentUser = arrName(1)
' This is where WMI interrogates the user account
' Set colItems3 = objWMI.ExecQuery("Select * from Win32_UserAccount",,48)
Set colItems3 = objWMI.ExecQuery("Select * from Win32_Account where Domain='" & domain & "' and Name = '" & currentUser & "'" )
' Here we filter SID from the dozens of properties
For Each objItem3 in colItems3
SID = objItem3.SID
Next
Next
Case Else OSystem = "Invalid OS"
End Select
' Spot VerBig variable in previous section
' Note the output variable is called storeToPath
Select Case VerBig
Case "6.1"
storeToPath = "C:\Users\" & currentUser
Case "5.1"
storeToPath = "D:\Documents and Settings\" & currentUser
End Select
Wscript.Echo "Version No: " & VerBig & vbCr _
& "OS System: " & OSystem & vbCr _
& "Save to Path: " & storeToPath & vbCr _
& "User SID: " & SID
WScript.Quit
' End of script
Dim x,y,z
.
MsgBox "Version No: " & VerBig & vbCr _
& "OS System: " & OSystem & vbCr _
& "Save to Path: " & storeToPath & vbCr _
& "User SID: " & SID, 64, "Information"
Option Explicit
Dim objWMI, strComputer
Dim colItems, objItem, VerBig, OSystem, WSHShell
Dim colItems2, objItem2, arrName, domain, currentUser
Dim colItems3, objItem3, SID, objAccount
Dim storeToPath
' Here is where we interrogate the Operating System
' On Error Resume Next
' Get the computer name (ie . = localhost)
strComputer = InputBox("Computer Name or IP Address: ")
' Remote connection path
Set objWMI = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
' This is where WMI interrogates the operating system
Set colItems = objWMI.ExecQuery("Select * from Win32_OperatingSystem",,48)
' Here we filter Version from the dozens of properties
For Each objItem in colItems
VerBig = Left(objItem.Version,3)
Next
' Spot VerBig variable in previous section
' Note the output variable is called OSystem
' This is where WMI interrogates the computer system
Set colItems2 = objWMI.ExecQuery("Select * from Win32_ComputerSystem",,48)
Select Case VerBig
Case "6.2"
OSystem = "8"
Set WSHShell = CreateObject("WScript.Shell")
domain = WshShell.ExpandEnvironmentStrings("%userdomain%")
currentUser = WshShell.ExpandEnvironmentStrings("%username%")
Set objAccount = objWMI.Get("Win32_UserAccount.Name='" & currentUser & "',Domain='" & domain & "'")
SID = objAccount.SID
Case "6.1"
OSystem = "7"
Set WSHShell = CreateObject("WScript.Shell")
domain = WshShell.ExpandEnvironmentStrings("%userdomain%")
currentUser = WshShell.ExpandEnvironmentStrings("%username%")
Set objAccount = objWMI.Get("Win32_UserAccount.Name='" & currentUser & "',Domain='" & domain & "'")
SID = objAccount.SID
Case "5.1"
OSystem = "XP"
' Here we filter UserName from the dozens of properties
For Each objItem2 in colItems2
arrName = Split(objItem2.UserName, "\")
domain = arrName(0)
currentUser = arrName(1)
' This is where WMI interrogates the user account
' Set colItems3 = objWMI.ExecQuery("Select * from Win32_UserAccount",,48)
Set colItems3 = objWMI.ExecQuery("Select * from Win32_Account where Domain='" & domain & "' and Name = '" & currentUser & "'" )
' Here we filter SID from the dozens of properties
For Each objItem3 in colItems3
SID = objItem3.SID
Next
Next
Case Else OSystem = "Invalid OS"
End Select
' Spot VerBig variable in previous section
' Note the output variable is called storeToPath
Select Case VerBig
Case "6.1" || "6.2"
storeToPath = "C:\Users\" & currentUser
Case "5.1"
storeToPath = "D:\Documents and Settings\" & currentUser
End Select
MsgBox "Version No: " & VerBig & vbCr _
& "OS System: " & OSystem & vbCr _
& "Save to Path: " & storeToPath & vbCr _
& "User SID: " & SID, 64, "Information"
WScript.Quit
Ran To
---------------
Ran From | . | XP | 7 |
---------|---|----|----|
XP | G | G | E1 |
---------|---|----|----|
7 | G | E2 | E1 |
------------------------
G = Good
E1 = Error: Not found; Code: 80041002; Source: SWbemServicesEx
E2 = SID is Null
Option Explicit
Dim strComputer
Dim objWMI
Dim colItems
Dim objItem
Dim VerBig
Dim OSystem
Dim WSHShell
Dim colItems2
Dim objItem2
Dim arrName
Dim domain
Dim currentUser
Dim colItems3
Dim objItem3
Dim SID
Dim objAccount
Dim storeToPath
' Here is where we interrogate the Operating System
' On Error Resume Next
' Get the computer name (ie . = localhost)
strComputer = InputBox("Computer Name or IP Address: ")
' Remote connection path
Set objWMI = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
' This is where WMI interrogates the operating system
Set colItems = objWMI.ExecQuery("Select * from Win32_OperatingSystem",,48)
' Here we filter Version from the dozens of properties
For Each objItem in colItems
VerBig = Left(objItem.Version,3)
Next
' Spot VerBig variable in previous section
' Note the output variable is called OSystem
' This is where WMI interrogates the computer system
Set colItems2 = objWMI.ExecQuery("Select * from Win32_ComputerSystem",,48)
Select Case VerBig
Case "6.1"
OSystem = "7"
Set WSHShell = CreateObject("WScript.Shell")
domain = WshShell.ExpandEnvironmentStrings("%userdomain%")
currentUser = WshShell.ExpandEnvironmentStrings("%username%")
Case "5.1"
OSystem = "XP"
' Here we filter UserName from the dozens of properties
For Each objItem2 in colItems2
arrName = Split(objItem2.UserName, "\")
domain = arrName(0)
currentUser = arrName(1)
Next
Case Else OSystem = "Invalid OS"
End Select
' This is where WMI interrogates the user account
Set objAccount = GetObject("winmgmts:root/cimv2:Win32_UserAccount.Domain='" & domain & "'" & ",Name='" & currentUser & "'")
SID = objAccount.SID
' Spot VerBig variable in previous section
' Note the output variable is called storeToPath
Select Case VerBig
Case "6.1"
storeToPath = "C:\Users\" & currentUser
Case "5.1"
storeToPath = "D:\Documents and Settings\" & currentUser
End Select
Wscript.Echo "Version No: " & VerBig & vbCr _
& "OS System: " & OSystem & vbCr _
& "Save to Path: " & storeToPath & vbCr _
& "User SID: " & SID
WScript.Quit
' End of script
0x800410xx - 0x800440: Errors that originate in WMI itself.
A specific WMI operation failed because of
• An error in the request, for example, a WQL query fails or the account does not have the correct permissions.
• A WMI infrastructure problem, such as incorrect CIM or DCOM registration.
Option Explicit
Dim strComputer
Dim objWMI
Dim colItems
Dim objItem
Dim VerBig
Dim OSystem
Dim WSHShell
Dim colItems2
Dim objItem2
Dim arrName
Dim domain
Dim currentUser
Dim colItems3
Dim objItem3
Dim SID
Dim objAccount
Dim storeToPath
Dim batchStoreToPath
Dim fileSys
Dim batchStorePath
Dim exportBatch
Dim objWMIService
Dim intProcessID
' Here is where we interrogate the Operating System
' On Error Resume Next
' Get the computer name (ie . = localhost)
strComputer = InputBox("Computer Name or IP Address: ")
' Remote connection path
Set objWMI = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
' This is where WMI interrogates the operating system
Set colItems = objWMI.ExecQuery("Select * from Win32_OperatingSystem",,48)
' Here we filter Version from the dozens of properties
For Each objItem in colItems
VerBig = Left(objItem.Version,3)
Next
' Spot VerBig variable in previous section
' Note the output variable is called OSystem
' This is where WMI interrogates the computer system
Set colItems2 = objWMI.ExecQuery("Select * from Win32_ComputerSystem",,48)
Select Case VerBig
Case "6.1"
OSystem = "7"
Set WSHShell = CreateObject("WScript.Shell")
domain = WshShell.ExpandEnvironmentStrings("%userdomain%")
currentUser = WshShell.ExpandEnvironmentStrings("%username%")
Case "5.1"
OSystem = "XP"
' Here we filter UserName from the dozens of properties
For Each objItem2 in colItems2
arrName = Split(objItem2.UserName, "\")
domain = arrName(0)
currentUser = arrName(1)
Next
Case Else OSystem = "Invalid OS"
End Select
' This is where WMI interrogates the user account
Set objAccount = GetObject("winmgmts:root/cimv2:Win32_UserAccount.Domain='" & domain & "'" & ",Name='" & currentUser & "'")
SID = objAccount.SID
' Spot VerBig variable in previous section
' Note the output variable is called storeToPath
Select Case VerBig
Case "6.1"
storeToPath = "C:\Users\" & currentUser
Select Case strComputer
Case ".", "localhost", "127.0.0.1"
batchStoreToPath = storeToPath
Case Else
batchStoreToPath = "\\" & strComputer & "\c$\Users\" & currentUser
End Select
Case "5.1"
storeToPath = "D:\Documents and Settings\" & currentUser
Select Case strComputer
Case ".", "localhost", "127.0.0.1"
batchStoreToPath = storeToPath
Case Else
batchStoreToPath = "\\" & strComputer & "\d$\Documents and Settings\" & currentUser
End Select
End Select
Wscript.Echo "Version No: " & VerBig & vbCr _
& "OS System: " & OSystem & vbCr _
& "Save to Path: " & storeToPath & vbCr _
& "User SID: " & SID
' This is the creation of a batch file on the remote machine to export the registry entries for network mappings
Set fileSys = CreateObject("Scripting.fileSystemObject")
Set batchStorePath = fileSys.GetFolder(batchStoreToPath)
Set exportBatch = batchStorePath.CreateTextFile("export.bat", True)
exportBatch.WriteLine("@ECHO OFF && SETLOCAL EnableDelayedExpansion")
exportBatch.WriteLine("")
exportBatch.WriteLine("ECHO Exporting data for '" & currentUser & "' with SID '" & SID & "' to '" & storeToPath & "'...")
exportBatch.WriteLine("REG EXPORT HKU\" & SID & "\Network " & Chr(34) & storeToPath & "\1.reg" & Chr(34))
exportBatch.WriteLine("REG EXPORT HKU\" & SID & "\Printers " & Chr(34) & storeToPath & "\2.reg" & Chr(34))
exportBatch.WriteLine("REG EXPORT " & Chr(34) & "HKU\" & SID & "\Software\Microsoft\Windows NT\CurrentVersion\Devices" & Chr(34) & " " & Chr(34) & storeToPath & "\3.reg" & Chr(34))
exportBatch.WriteLine("REG EXPORT " & Chr(34) & "HKU\" & SID & "\Software\Microsoft\Windows NT\CurrentVersion\PrinterPorts" & Chr(34) & " " & Chr(34) & storeToPath & "\4.reg" & Chr(34))
exportBatch.WriteLine("echo.>>" & Chr(34) & storeToPath & "\Network Mappings.reg" & Chr(34))
exportBatch.WriteLine("type " & Chr(34) & storeToPath & "\1.reg" & Chr(34) & " | find /v " & Chr(34) & "Windows Registry Editor Version 5.00" & Chr(34) & ">>" & Chr(34) & storeToPath & "\Network Mappings.reg" & Chr(34))
exportBatch.WriteLine("type " & Chr(34) & storeToPath & "\2.reg" & Chr(34) & " | find /v " & Chr(34) & "Windows Registry Editor Version 5.00" & Chr(34) & ">>" & Chr(34) & storeToPath & "\Network Mappings.reg" & Chr(34))
exportBatch.WriteLine("type " & Chr(34) & storeToPath & "\3.reg" & Chr(34) & " | find /v " & Chr(34) & "Windows Registry Editor Version 5.00" & Chr(34) & ">>" & Chr(34) & storeToPath & "\Network Mappings.reg" & Chr(34))
exportBatch.WriteLine("type " & Chr(34) & storeToPath & "\4.reg" & Chr(34) & " | find /v " & Chr(34) & "Windows Registry Editor Version 5.00" & Chr(34) & ">>" & Chr(34) & storeToPath & "\Network Mappings.reg" & Chr(34))
exportBatch.WriteLine("del " & Chr(34) & storeToPath & "\1.reg" & Chr(34))
exportBatch.WriteLine("del " & Chr(34) & storeToPath & "\2.reg" & Chr(34))
exportBatch.WriteLine("del " & Chr(34) & storeToPath & "\3.reg" & Chr(34))
exportBatch.WriteLine("del " & Chr(34) & storeToPath & "\4.reg" & Chr(34))
exportBatch.WriteLine("")
exportBatch.WriteLine("PAUSE && GOTO :EOF")
exportBatch.Close
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2:Win32_Process")
objWMIService.Create batchStoreToPath & "\export.bat", Null, Null, intProcessID
WScript.Quit
' End of script
Has Sysnative Forums helped you? Please consider donating to help us support the site!