Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Batch file get server name from text file

Posted on 2014-04-10
5
Medium Priority
?
843 Views
Last Modified: 2014-10-25
Hi I have the following command:

for /F "tokens=*" %%i in (userslist.txt) do takeown /f "\\%fileserver%\d$\Profiles\%%i" /r /d y & icacls.exe \\%fileserver%\d$\Profiles\%%* /resize & icacls.exe \\%fileserver%\d$\Profiles\%%i /grant %%i:(OI)(CI)F builtin\Administrators:(OI)(CI)F /inheritance:r >> error.log

Open in new window


I'm running this batch file from a 2008 server, I would like to import the file server IP from the following input.csv  file that has the following:

fqdn  file_server
van.acme.dom.com 10.10.10.10
tor.acme.dom.com 20.20.20.20
new.acme.dom.com 30.30.30.30

I want to be able to query the current computer FQDN and match it to the file_server ip address in the input.csv file. Then take that IP and make it my %fileserver% variable in the batch file.

The idea is to be able to run this batch file from any network and it'll pick up the corresponding file server Ip from the input.csv file

I have access to PowerShell and can run (gwmi WIN32_ComputerSystem).Domain but I dont know how to then take that FQDN and match it to the IP adress found in input.csv and then feed that IP to the batch file.

Thank you.
0
Comment
Question by:levertm
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
5 Comments
 
LVL 19

Expert Comment

by:Raheman M. Abdul
ID: 39992467
To get the matching IP of the fqdn from the input .csv file  (say  fqdn.csv)

$file = Import-Csv C:\temp\fqdn.csv  
$domainToFind="tor.acme.dom.com"
$IP=($file | ? { $_.fqdn -eq  $domainToFind } | select file_server).file_server

OR

$file = Import-Csv C:\temp\fqdn.csv  
$domainToFind=(gwmi WIN32_ComputerSystem).Domain
$IP=($file | ? { $_.fqdn -eq  $domainToFind } | select file_server).file_server


$IP has the corresponding IP address of the domain.
0
 
LVL 85

Accepted Solution

by:
oBdA earned 2000 total points
ID: 39992515
Plain batch; I REMed out the permission change part so that you can test if the IP gets resolved like you want:
@echo off
setlocal enabledelayedexpansion
set ServerFile=input.csv
for /f "skip=1" %%a in ('wmic.exe Path WIN32_ComputerSystem Get Domain') do set ComputerDomain=%%a&goto Break
:Break
echo Computer domain: '%ComputerDomain%'
for /f "tokens=1,2" %%a in ('type "%ServerFile%" ^| find /i "%ComputerDomain%"') do set Fileserver=%%b
echo File server IP:  '%Fileserver%'
for /F "tokens=*" %%i in (userslist.txt) do (
	REM takeown /f "\\%fileserver%\d$\Profiles\%%i" /r /d y >>error.log 2>&1
	REM icacls.exe \\%fileserver%\d$\Profiles\%%* /resize >>error.log 2>&1
	REM icacls.exe \\%fileserver%\d$\Profiles\%%i /grant %%i:(OI)(CI)F builtin\Administrators:(OI)(CI)F /inheritance:r >>error.log 2>&1
)

Open in new window

0
 
LVL 19

Expert Comment

by:Raheman M. Abdul
ID: 39992540
save the following code in "c:\temp\get_IP.ps1" file

#-----------------------------------
$file = Import-Csv C:\temp\fqdn.csv  
$domainToFind=(gwmi WIN32_ComputerSystem).Domain
$IP=($file | ? { $_.fqdn -eq  $domainToFind } | select file_server).file_server
[environment]::SetEnvironmentVariable('fileserver',"$IP",'USER')
#-----------------------------------------------------------------------------------

In your batch file at the top of it add the following line:
#-------------------------------------------
powershell.exe -file "c:\temp\getIP.ps1"

then follows your batch file code.
#-----------------------------------------------------------
which executes the file and sets the environment variable %fileserver%
%fileserver%  is the environment variable now and have the value of the corresponding IP address and can be accessed from the batch file.
0

Featured Post

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This script can help you clean up your user profile database by comparing profiles to Active Directory users in a particular OU, and removing the profiles that don't match.
This is a fine trick which I've found useful many times, when you just don't want to accidentally run a batch script or the commands needs administrator rights.
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question