title: Executable to install Psexec
body: <p>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):</p>
  <ol>
  <li><code>&quot;Make sure that the default admin$ share is enabled&quot;</code></li>
  <li><code>&quot;Couldn't access xxx.xxx.xx.xx: The user name or password is incorrect.&quot;</code></li>
  </ol>
  <p>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 &lt;3</p>
  <p>Here's the code (after this there's the explanation):</p>
  <pre><code>@echo off
  &gt;nul 2&gt;&amp;1 &quot;%SYSTEMROOT%\system32\cacls.exe&quot; &quot;%SYSTEMROOT%\system32\config\system&quot;
  
  if '%errorlevel%' NEQ '0' (
      echo Requesting administrative privileges...
      goto UACPrompt
  ) else ( goto gotAdmin )
  
  :UACPrompt
      echo Set UAC = CreateObject^(&quot;Shell.Application&quot;^) &gt; &quot;%temp%\getadmin.vbs&quot;
      set params = %*:&quot;=&quot;
      echo UAC.ShellExecute &quot;cmd.exe&quot;, &quot;/c %~s0 %params%&quot;, &quot;&quot;, &quot;runas&quot;, 1 &gt;&gt; &quot;%temp%\getadmin.vbs&quot;
  
      &quot;%temp%\getadmin.vbs&quot;
      del &quot;%temp%\getadmin.vbs&quot;
      exit /B
  
  :gotAdmin
      pushd &quot;%CD%&quot;
      CD /D &quot;%~dp0&quot;
  set &quot;Host-address=&quot;
  set Host-address=xxx.xxx.xx.xx
  set /p NAME=&quot;Name: &quot;
  for /f &quot;delims=[] tokens=2&quot; %%a in ('ping -4 -n 1 %ComputerName% ^| findstr [') do set NetworkIP=%%a
  echo %NetworkIP% &gt;&quot;%~d0\Server installation\IP&quot;\%NAME%.txt
  SETLOCAL
  SET &quot;admins=&quot;
  SET &quot;prev=&quot;
  FOR /f &quot;delims=&quot; %%A IN ('net localgroup administrators') DO (
   CALL SET &quot;admins=%%admins%% %%prev%%&quot;
   SET &quot;prev=%%A&quot;)
  SET admins=%admins:*- Administrator =%
  echo %admins%&gt; &quot;%~d0\Server installation\Username&quot;\%NAME%.txt
  PowerShell.exe -Command &quot;Set-ExecutionPolicy Bypass&quot;
  PowerShell.exe -Command &quot;Set-NetFirewallRule -DisplayGroup 'File and Printer Sharing' -Enabled True -Profile Any&quot;
  PowerShell.exe -Command &quot;Set-NetFirewallRule -DisplayGroup 'File and Printer Sharing over SMBDirect' -Enabled True -Profile Any&quot;
  PowerShell.exe -Command &quot;Enable-PSRemoting -Force&quot;
  powerShell.exe -Command &quot;Set-Item WSMan:\localhost\Client\TrustedHosts -value %Host-address% -Force&quot;
  PowerShell.exe -Command &quot;Restart-Service WInRM&quot;
  reg add HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\system /v LocalAccountTokenFilterPolicy /t REG_DWORD /d 0 /f
  PowerShell.exe -Command &quot;Set-ItemProperty -Name AutoShareWks -Path &quot;HKLM:\SYSTEM\CurrentControlSet\Services\LanmanServer\Parameters&quot; -Value 1&quot;
  xcopy &quot;%~d0\Server installation\PSTools&quot; &quot;C:\Windows\System32\PSTools&quot; /E /H /C /I /Y
  setx /M Path &quot;%path%;C:\Windows\System32\PSTools&quot;
  PowerShell.exe -Command &quot;psexec&quot;
  PowerShell.exe -Command &quot;psexec /accepteula&quot;
  net share admin$ delete
  net share C:\Windows /delete
  net share admin$ /UNLIMITED
  net share admin=&quot;C:\Windows&quot; /GRANT:&quot;%admins%&quot;,Full /UNLIMITED
  netsh advfirewall firewall add rule name=&quot;TCP Port 445&quot; dir=in action=allow protocol=TCP localport=445
  netsh advfirewall firewall add rule name=&quot;TCP Port 135&quot; dir=in action=allow protocol=TCP localport=135
  </code></pre>
  <p>Here's the explanation of what I have writed since now:</p>
  <ol>
  <li>Automatically runs the bat file as Admin:</li>
  </ol>
  <pre><code>&gt;nul 2&gt;&amp;1 &quot;%SYSTEMROOT%\system32\cacls.exe&quot; &quot;%SYSTEMROOT%\system32\config\system&quot;
  
  if '%errorlevel%' NEQ '0' (
     echo Requesting administrative privileges...
     goto UACPrompt
  ) else ( goto gotAdmin )
  
  :UACPrompt
     echo Set UAC = CreateObject^(&quot;Shell.Application&quot;^) &gt; &quot;%temp%\getadmin.vbs&quot;
     set params = %*:&quot;=&quot;
     echo UAC.ShellExecute &quot;cmd.exe&quot;, &quot;/c %~s0 %params%&quot;, &quot;&quot;, &quot;runas&quot;, 1 &gt;&gt; &quot;%temp%\getadmin.vbs&quot;
     &quot;%temp%\getadmin.vbs&quot;
     del &quot;%temp%\getadmin.vbs&quot;
     exit /B
  :gotAdmin
     pushd &quot;%CD%&quot;
     CD /D &quot;%~dp0&quot; 
  </code></pre>
  <ol start="2">
  <li>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:</li>
  </ol>
  <pre><code>set Host-address=xxx.xxx.xx.xx
  </code></pre>
  <ol start="3">
  <li>Creates a variable that askes me the name of the computer to save the following files with Its name:</li>
  </ol>
  <pre><code>set /p NAME=&quot;Name: &quot;
  </code></pre>
  <ol start="4">
  <li>Saves the ip of the computer I'm running the bat on in a folder called
  &quot;IP&quot;, inside the folder &quot;Server Installation&quot;, in my flash drive that has a variable letter assigned &quot;%~d0&quot; (that's because very often I have multiple flash drives connected to this computers):</li>
  </ol>
  <pre><code>for /f &quot;delims=[] tokens=2&quot; %%a in ('ping -4 -n 1 %ComputerName% ^| findstr [') do set NetworkIP=%%a
  echo %NetworkIP% &gt;&quot;%~d0\Server installation\IP&quot;\%NAME%.txt
  </code></pre>
  <ol start="5">
  <li>Saves the adminastrator's name in a folder called &quot;Username&quot; always in the &quot;Server installation&quot; folder in the same flash drive:</li>
  </ol>
  <pre><code>SETLOCAL
  SET &quot;admins=&quot;
  SET &quot;prev=&quot;
  FOR /f &quot;delims=&quot; %%A IN ('net localgroup administrators') DO (
   CALL SET &quot;admins=%%admins%% %%prev%%&quot;
   SET &quot;prev=%%A&quot;)
  SET admins=%admins:*- Administrator =%
  echo %admins%&gt; &quot;%~d0\Server installation\Username&quot;\%NAME%.txt
  </code></pre>
  <ol start="6">
  <li>Lets me execute any command, enables &quot;File and Printer Sharing&quot;, enables &quot;File and Printer Sharing over SMBDirect&quot;, enables &quot;PSRemoting&quot;, adds my host address as a trusted host and finally restarts &quot;WInRM&quot;:</li>
  </ol>
  <pre><code>PowerShell.exe -Command &quot;Set-ExecutionPolicy Bypass&quot;
  PowerShell.exe -Command &quot;Set-NetFirewallRule -DisplayGroup 'File and Printer Sharing' -Enabled True -Profile Any&quot;
  PowerShell.exe -Command &quot;Set-NetFirewallRule -DisplayGroup 'File and Printer Sharing over SMBDirect' -Enabled True -Profile Any&quot;
  PowerShell.exe -Command &quot;Enable-PSRemoting -Force&quot;
  powerShell.exe -Command &quot;Set-Item WSMan:\localhost\Client\TrustedHosts -value %Host-address% -Force&quot;
  PowerShell.exe -Command &quot;Restart-Service WInRM&quot;
  </code></pre>
  <ol start="7">
  <li>Enable C$ sharing and sets the C$ sharing as automatic:</li>
  </ol>
  <pre><code>reg add HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\system /v LocalAccountTokenFilterPolicy /t REG_DWORD /d 0 /f
  PowerShell.exe -Command &quot;Set-ItemProperty -Name AutoShareWks -Path &quot;HKLM:\SYSTEM\CurrentControlSet\Services\LanmanServer\Parameters&quot; -Value 1&quot;
  </code></pre>
  <ol start="8">
  <li>Installs the &quot;pstools&quot; folder containing the &quot;psexec&quot; tool inside my flash drive inside the computer, add its path to the system path variables and accepts psexec eula:</li>
  </ol>
  <pre><code>xcopy &quot;%~d0\Server installation\PSTools&quot; &quot;C:\Windows\System32\PSTools&quot; /E /H /C /I /Y
  setx /M Path &quot;%path%;C:\Windows\System32\PSTools&quot;
  PowerShell.exe -Command &quot;psexec&quot;
  PowerShell.exe -Command &quot;psexec /accepteula&quot;
  </code></pre>
  <ol start="9">
  <li>Erases any admin$ sharing and ant &quot;C:\Windows&quot; sharing, shares admin$ again and then shares &quot;C:\Windows&quot; with the name &quot;admin&quot; to my administrator's name. Then opens the 445 port and the 135 port:</li>
  </ol>
  <pre><code>net share admin$ delete
  net share C:\Windows /delete
  net share admin$ /UNLIMITED
  net share admin=&quot;C:\Windows&quot; /GRANT:&quot;%admins%&quot;,Full /UNLIMITED
  netsh advfirewall firewall add rule name=&quot;TCP Port 445&quot; dir=in action=allow protocol=TCP localport=445
  netsh advfirewall firewall add rule name=&quot;TCP Port 135&quot; dir=in action=allow protocol=TCP localport=135
  </code></pre>