Problem to mapping network drive using snapin with batch file
-
OS: Windows 10 1709
Hello,
i have a problem to mapping a drive.
The batch file contain:net use z: \192.168.1.1
i created a snapin with this .bat file, but the drive not install.
If i cut and paste net use z: \192.168.1.1 in the command prompt ,work!!
with snapin not working, i adding in the snapin argument c:\out.txt (for viewing the output when the snapin is executed) the result is:C:\Program Files (x86)\FOG\tmp>net use z: \192.168.1.1\myshare
the operation is easy, but not working.
-
You have to remember one thing. The fog client service runs at the SYSTEM user not the current logged in user. So the drive is being mapped as SYSTEM and it works for the SYSTEM context, that context doesn’t apply the current logged in user. While I haven’t tried this, but log into a windows computer as a normal user, open a command shell as an administrator (or different user). Then map a drive as that admin user, is that drive visible in the normal user’s shell. Is a drive mapped in the normal user’s shell available to the admin user’s command window. I’m suspecting not.
-
Below is a snippit from one of my projects. This powershell script mounts a remote SMB share and should work as SYSTEM.
#Drive letter for mapped drive to use, you may use whatever letter available. $driveLetter="J" #Your username: $username="wayne" #Your SMB password: $SMBPassword="MyAwesomePassword" #server hostname or IP address: $hostname="10.0.0.12" #Change working directory to the system drive. cd $env:SystemDrive #Here, check if the drive letter is already in use. If so, delete it. if (Get-PSDrive $driveLetter) { Get-PSDrive $driveLetter | Remove-PSDrive } #Gain access to the share using the defined settings above. $pass=$SMBPassword|ConvertTo-SecureString -AsPlainText -Force $Cred = New-Object System.Management.Automation.PsCredential("$username",$pass) New-PSDrive -name $driveLetter -Root "\\$hostname\" -Credential $cred -PSProvider filesystem
-
@george1421
Thank you for the answer.
Ok the problem is due to the batch script, which runs as system user, not current user.
i would like to apply this script:Set csv=\192.168.1.1\myshare\ip.csv
for /f “skip=1 delims=, tokens=1-6” %%a in (%csv%) do (
if “%computername%”==“%%a” (
netsh int ipv4 set address “Ethernet” static %%b %%c %%d
netsh int ipv4 set dnsserver “Ethernet” static %%e
netsh int ipv4 add dnsserver “Ethernet” %%f
)
)It’s a batch script to change ip address to the pcs.
The “csv” variable contains the network path to point at the .csv file,
where are all pcs names and relative ip address.
When i run the snapin with this script the script doesn’t get ip.csv and doesn’t read the ip.csv file.
the output is:C:\Program Files (x86)\FOG\tmp>Set csv=\192.168.1.1\myshare\ip.csv
C:\Program Files (x86)\FOG\tmp>for /F “skip=1 delims=, tokens=1-6” %a in (\192.168.1.1\myshare\ip.csv) do (if “ACF-UNDICI” == “%a” (
netsh int ipv4 set address “Ethernet0” static %b %c %d
netsh int ipv4 set dnsserver “Ethernet0” static %e
netsh int ipv4 add dnsserver “Ethernet0” %f
) )If i copy ip.csv to the the script works “Set csv=c:\ip.csv” !!!
Why doesn’t the script get ip.csv file from network path?
I tried to map a drive with net use command to copy ip.csv tonet use z: \192.168.1.1\myshare
copy z:\ip.csv c:\but doesn’t work.
Have you any idea to solve this problem?
Sorry if it is off-topic.
thanks you -
@fog-user said in Problem to mapping network drive using snapin with batch file:
Ok the problem is due to the batch script, which runs as system user, not current user.
i would like to apply this script:
Set csv=\192.168.1.1\myshare\ip.csvThis is your problem, still as I mentioned. The batch script runs as the SYSTEM user, which is a local administrative account. The SYSTEM user has zero domain rights, or rights on another computer. Your script will not work as it is currently designed.
So how to make it work? You have 2 options.
- As part of your snapin, also include the csv file with your batch file. So where ever your batch script is run then you .csv file will be there.
- Map a network drive to the location of where the .csv file is and supply the required user credentials. for example.
net use w: "\\192.168.1.1\myshare" /user:domain\user user-password for /F “skip=1 delims=, tokens=1-6” %a in (w:\ip.csv) do (if “ACF-UNDICI” == “%a” ( ... net use w: /delete
-
Hi,
no way to do this if you need to do this with credentials, would be a security leak!
two possible solutions if you don’t give a * whos loggin in to this computer, place a file on the system with the desired net use line and execute it by registry hklm run or place the script in all users autostart.Isn’t this a ADS env with the possibility of logon scripts?
Regards X23
-
@wayne-workman Did you ever have any luck with this? I copied it into my own powershell script, and changed the entries to reflect my own SMB share. I added a line at the end to test that it is actually connected, which doesn’t work. It is just a basic Copy-Item command to copy over a file from the share to the local drive. I added a second copy line after that to make sure that the script is executing completely, which copied a file in one folder on the local system to the desktop. This line worked.
-
@hancocza Try the script with bad credentials or bad hostname and see if you get an error. Also, PS-Drives created by powershell are only available to the shell that created them. Meaning you won’t be able to see it thorough explorer.exe, but will be able to use it inside of the script.
-
@wayne-workman It works if i run it as an administrator on the test computer. It just doesn’t work when i run it as a snapin. Same code and everything. I assume it has something to do with running on the SYSTEM user?
-
@wayne-workman Hey Wayne, you can forget this. I tinkered with it a bit (reentered in the credentials, changed the path a few times, etc) and for some reason, it started working on the SYSTEM user using psexec. After that, I tested as a snapin, and with the powershell template (powershell.exe -ExecutionPolicy Bypass -NoProfile -File xxxxx.ps1) it worked correctly.