'Executable to install Psexec

I am a school technician and I was asked to make all classes computers accessibles from psexec. I'm quite new to this tool but another techician said that this is what we needed so I have written this .bat file that is supposed to automatically install psexec from my flash drive and allow everything needed to use It. The purpose was to make my work faster then manually install psexec on all 43 computers. Unfortunately this executable works perfectly on the server computers but when I try to connect to those from a Client computer they all return one of these two errors (I believe randomly):

  1. "Make sure that the default admin$ share is enabled"
  2. "Couldn't access xxx.xxx.xx.xx: The user name or password is incorrect."

All the computer are in a workgroup so I have to run this bat one computer at a time. What do I have to add to my .bat file to reach my purpose? Could you please help me to complete my project? Thank you <3

Here's the code (after this there's the explanation):

@echo off
>nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"

if '%errorlevel%' NEQ '0' (
    echo Requesting administrative privileges...
    goto UACPrompt
) else ( goto gotAdmin )

:UACPrompt
    echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
    set params = %*:"="
    echo UAC.ShellExecute "cmd.exe", "/c %~s0 %params%", "", "runas", 1 >> "%temp%\getadmin.vbs"

    "%temp%\getadmin.vbs"
    del "%temp%\getadmin.vbs"
    exit /B

:gotAdmin
    pushd "%CD%"
    CD /D "%~dp0"
set "Host-address="
set Host-address=xxx.xxx.xx.xx
set /p NAME="Name: "
for /f "delims=[] tokens=2" %%a in ('ping -4 -n 1 %ComputerName% ^| findstr [') do set NetworkIP=%%a
echo %NetworkIP% >"%~d0\Server installation\IP"\%NAME%.txt
SETLOCAL
SET "admins="
SET "prev="
FOR /f "delims=" %%A IN ('net localgroup administrators') DO (
 CALL SET "admins=%%admins%% %%prev%%"
 SET "prev=%%A")
SET admins=%admins:*- Administrator =%
echo %admins%> "%~d0\Server installation\Username"\%NAME%.txt
PowerShell.exe -Command "Set-ExecutionPolicy Bypass"
PowerShell.exe -Command "Set-NetFirewallRule -DisplayGroup 'File and Printer Sharing' -Enabled True -Profile Any"
PowerShell.exe -Command "Set-NetFirewallRule -DisplayGroup 'File and Printer Sharing over SMBDirect' -Enabled True -Profile Any"
PowerShell.exe -Command "Enable-PSRemoting -Force"
powerShell.exe -Command "Set-Item WSMan:\localhost\Client\TrustedHosts -value %Host-address% -Force"
PowerShell.exe -Command "Restart-Service WInRM"
reg add HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\system /v LocalAccountTokenFilterPolicy /t REG_DWORD /d 0 /f
PowerShell.exe -Command "Set-ItemProperty -Name AutoShareWks -Path "HKLM:\SYSTEM\CurrentControlSet\Services\LanmanServer\Parameters" -Value 1"
xcopy "%~d0\Server installation\PSTools" "C:\Windows\System32\PSTools" /E /H /C /I /Y
setx /M Path "%path%;C:\Windows\System32\PSTools"
PowerShell.exe -Command "psexec"
PowerShell.exe -Command "psexec /accepteula"
net share admin$ delete
net share C:\Windows /delete
net share admin$ /UNLIMITED
net share admin="C:\Windows" /GRANT:"%admins%",Full /UNLIMITED
netsh advfirewall firewall add rule name="TCP Port 445" dir=in action=allow protocol=TCP localport=445
netsh advfirewall firewall add rule name="TCP Port 135" dir=in action=allow protocol=TCP localport=135

Here's the explanation of what I have writed since now:

  1. Automatically runs the bat file as Admin:
>nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"

if '%errorlevel%' NEQ '0' (
   echo Requesting administrative privileges...
   goto UACPrompt
) else ( goto gotAdmin )

:UACPrompt
   echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
   set params = %*:"="
   echo UAC.ShellExecute "cmd.exe", "/c %~s0 %params%", "", "runas", 1 >> "%temp%\getadmin.vbs"
   "%temp%\getadmin.vbs"
   del "%temp%\getadmin.vbs"
   exit /B
:gotAdmin
   pushd "%CD%"
   CD /D "%~dp0" 
  1. To make my work easier if I ever wanted to change the ip of the client i created a variable with the client ip, so wherever i needed It I just used the variable value:
set Host-address=xxx.xxx.xx.xx
  1. Creates a variable that askes me the name of the computer to save the following files with Its name:
set /p NAME="Name: "
  1. Saves the ip of the computer I'm running the bat on in a folder called "IP", inside the folder "Server Installation", in my flash drive that has a variable letter assigned "%~d0" (that's because very often I have multiple flash drives connected to this computers):
for /f "delims=[] tokens=2" %%a in ('ping -4 -n 1 %ComputerName% ^| findstr [') do set NetworkIP=%%a
echo %NetworkIP% >"%~d0\Server installation\IP"\%NAME%.txt
  1. Saves the adminastrator's name in a folder called "Username" always in the "Server installation" folder in the same flash drive:
SETLOCAL
SET "admins="
SET "prev="
FOR /f "delims=" %%A IN ('net localgroup administrators') DO (
 CALL SET "admins=%%admins%% %%prev%%"
 SET "prev=%%A")
SET admins=%admins:*- Administrator =%
echo %admins%> "%~d0\Server installation\Username"\%NAME%.txt
  1. Lets me execute any command, enables "File and Printer Sharing", enables "File and Printer Sharing over SMBDirect", enables "PSRemoting", adds my host address as a trusted host and finally restarts "WInRM":
PowerShell.exe -Command "Set-ExecutionPolicy Bypass"
PowerShell.exe -Command "Set-NetFirewallRule -DisplayGroup 'File and Printer Sharing' -Enabled True -Profile Any"
PowerShell.exe -Command "Set-NetFirewallRule -DisplayGroup 'File and Printer Sharing over SMBDirect' -Enabled True -Profile Any"
PowerShell.exe -Command "Enable-PSRemoting -Force"
powerShell.exe -Command "Set-Item WSMan:\localhost\Client\TrustedHosts -value %Host-address% -Force"
PowerShell.exe -Command "Restart-Service WInRM"
  1. Enable C$ sharing and sets the C$ sharing as automatic:
reg add HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\system /v LocalAccountTokenFilterPolicy /t REG_DWORD /d 0 /f
PowerShell.exe -Command "Set-ItemProperty -Name AutoShareWks -Path "HKLM:\SYSTEM\CurrentControlSet\Services\LanmanServer\Parameters" -Value 1"
  1. Installs the "pstools" folder containing the "psexec" tool inside my flash drive inside the computer, add its path to the system path variables and accepts psexec eula:
xcopy "%~d0\Server installation\PSTools" "C:\Windows\System32\PSTools" /E /H /C /I /Y
setx /M Path "%path%;C:\Windows\System32\PSTools"
PowerShell.exe -Command "psexec"
PowerShell.exe -Command "psexec /accepteula"
  1. Erases any admin$ sharing and ant "C:\Windows" sharing, shares admin$ again and then shares "C:\Windows" with the name "admin" to my administrator's name. Then opens the 445 port and the 135 port:
net share admin$ delete
net share C:\Windows /delete
net share admin$ /UNLIMITED
net share admin="C:\Windows" /GRANT:"%admins%",Full /UNLIMITED
netsh advfirewall firewall add rule name="TCP Port 445" dir=in action=allow protocol=TCP localport=445
netsh advfirewall firewall add rule name="TCP Port 135" dir=in action=allow protocol=TCP localport=135


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source