|
Answer» I'm trying to create a .bat file to use for networking support. The file will run a ipconfig /all, tracert, ping and output the results to a file. The catch is I know some people will run it who don't have access to the c:\ and will still need to create the file. Thus I need to check access and then write the file to the c:\ else write it to other directory. I'm thinking i'll use ver to check the OS afterwards but I need help writing an if or something to get around access denied when writing the file.
On a side note, I'd like to include the public IP address in here but not certain the best way to do that yetwhy not just create a network LOCATION (IE share) on a server that everyone has modify access to. Then all you need to do is map a drive to the share and log the results to the share. you could even use the %computername% var in the log file so you keep track of each computer.
please let me know if this will work or if you need more information / help.users don't all have access to same serverwhat type of network are you working on? are the clients on the same domain or are you working with different networks?completely different users are CUSTOMERS who have their own networksOK well give this a try and let me know. ( I have done limited testing with it.)
Code: [Select]@echo OFF CLS
set admin=N set domain=%USERDOMAIN%\ If /i "%domain%" EQU "%computername%\" set domain= set user=%domain%%username% for /f "Tokens=*" %%a in ('net localgroup administrators^|find /i "%user%"') do set admin=Y
If %admin% == Y goto isAdmin
ECHO NO NOT ADMIN pause GOTO END
:isAdmin ECHO YES ADMIN pause
:END
you can have two different sets of code one for admins and one for non admins.interesting but what if other user groups have admin access? then they would show as non-Administrators, right?this should test for users explicitly defined in admins.
Are you asking will they show up if they are a member of a domain GROUP? the answer is no.
but if you are asking about groups like power users then yes you would just need to change the group the query is looking in to.
Code: [Select]for /f "Tokens=*" %%a in ('net localgroup [powerusers]^|find /i "%user%"') do set admin=Y so there isn't a way to test the command
ping www.google.com > C:\ping.txt
To see if you will return an "Access is denied"? I was hoping to use some sort of if errorlevel statement but couldn't grasp it.Like the attrib command can view attributes for a file...what you could also try and do is create a folder on the root of the system. Then check and see if it exists. if it dose then the user has the rights needed.
also if you are using scheduled tasks then you can run then as a different user. md will work as when I get Access denied, as you said I can continue with the batch, i'll check for the directory and then remove it. this should work, but would like to think there's a more direct way.Quote but would like to think there's a more direct way.
By redirecting STDERR back to the STDOUT data stream, you should be able to trap the message and implement plan B.
Code: [Select]@echo off for /f "tokens=* delims=" %%v in ('ping www.google.com ^> C:\ping.txt 2^>^&1') do ( if /i "%%v" equ "Access is denied." echo Implement Plan B )
The literal message must be spelled exactly as issued by the OS. It is not case sensitive.
Hope this helps.
|