levertm
asked on
Batch file get server name from text file
Hi I have the following command:
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).Doma in 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.
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
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).Doma
Thank you.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
save the following code in "c:\temp\get_IP.ps1" file
#------------------------- ----------
$file = Import-Csv C:\temp\fqdn.csv
$domainToFind=(gwmi WIN32_ComputerSystem).Doma in
$IP=($file | ? { $_.fqdn -eq $domainToFind } | select file_server).file_server
[environment]::SetEnvironm entVariabl e('fileser ver',"$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.
#-------------------------
$file = Import-Csv C:\temp\fqdn.csv
$domainToFind=(gwmi WIN32_ComputerSystem).Doma
$IP=($file | ? { $_.fqdn -eq $domainToFind } | select file_server).file_server
[environment]::SetEnvironm
#-------------------------
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.
$file = Import-Csv C:\temp\fqdn.csv
$domainToFind="tor.acme.do
$IP=($file | ? { $_.fqdn -eq $domainToFind } | select file_server).file_server
OR
$file = Import-Csv C:\temp\fqdn.csv
$domainToFind=(gwmi WIN32_ComputerSystem).Doma
$IP=($file | ? { $_.fqdn -eq $domainToFind } | select file_server).file_server
$IP has the corresponding IP address of the domain.