Page 1 of 1
Write to registry
Posted: 28 Sep 2012 05:01
by jvuz
Hello,
I'm having a csv file (for instance)
Code: Select all
192.168.3.132,LC03-132,User A,11-04-2011
192.168.3.133,LC03-133,User B,02-05-2011
192.168.4.109,LC04-109,User C,26-10-2011
192.168.5.143,LC05-143,User D,02-05-2010
This file contains IP-address, PC name, User and start date of the warranty. The goal is to create a batch file (being launched from my pc) thakes the first line, tries to ping that machine, if it responds, write the date into the registry and then goes to pinging the following and so on.
If there isn't any response to the ping, it should continue to the following line. Is there a way to doe this by a batch file?
Jvuz
Re: Write to registry
Posted: 28 Sep 2012 06:42
by abc0502
where you want the date to be written in the registry?
Re: Write to registry
Posted: 28 Sep 2012 06:47
by jvuz
I want it to be written to HKLM\SYSTEM\CurrentControlSet\Policies StartWarrantyDate (reg_sz). this key doesn't exist yet.
I know how to write to the registry, I don't know how to start, how to check the first line (1st and 4the field).
Re: Write to registry
Posted: 28 Sep 2012 06:52
by abc0502
To read a file and check for it's content, in your case it's a csv and that make it easy.
The comma is seprating IP,username,pc name and date
so
Code: Select all
For /f "tokens=1,2,3,4 delims=," %%A in ('Type "C:\File.csv"') Do (
:: All Your Commands here
)
the %%A will hold the IP, and %%B will hold the PC names and %%C will have the user names and %%D will have the date
Re: Write to registry
Posted: 28 Sep 2012 06:56
by jvuz
OK, thanks for the info. I knew it would be easier with a csv file, I just didn't know how
Now I can play with it. I'll keep you posted.
Thanks again,
have a nice weekend,
jvuz
Re: Write to registry
Posted: 28 Sep 2012 06:57
by abc0502
Try this batch , but it's not tested
Code: Select all
@Echo Off & Cls & Color 0E
Setlocal EnableDelayedExpansion
:: Reading Input From CSV File
For /F "tokens=1,2,3,4 delims=," %%A in ('Type "C:\File.csv"') Do (
set "IP=%%A" & set "PC=%%B" & set "User=%%C" & set "SDate=%%D"
ping !IP! -n 4 >nul
IF !errorlevel! == 0 (
Reg Add "\\!PC!\HKLM\SYSTEM\CurrentControlSet\Policies" /v "StartWarrantyDate" /t "REG_SZ" /d "!SDate!" /f
)
)
Re: Write to registry
Posted: 01 Oct 2012 02:12
by jvuz
Thanks,
but what if I have more lines. It's meant for our whole computer park (between 300 and 400 pc's). Is there a way to use something like EOF (end of file)?
Jvuz
Re: Write to registry
Posted: 01 Oct 2012 03:07
by abc0502
jvuz wrote:Thanks,
but what if I have more lines. It's meant for our whole computer park (between 300 and 400 pc's). Is there a way to use something like EOF (end of file)?
Jvuz
@Jvuz,
The code i posted will apply to all your 300 or 400 PC's, Just place all your PC's in the same format you did in your first post and then run that batch and it will process all PC's.
This For Loop is limited Only to your IPs in your CSV file whether there was only one line:
Code: Select all
192.168.3.132,LC03-132,User A,11-04-2011
or 1000 line like that line above it will do the same job to all
Re: Write to registry
Posted: 05 Oct 2012 01:31
by jvuz
Thanks guys, it seems to be working, but is there a way to show which line has been done and which couldn't (because there weren't up and running)? Is it possible maybe to write it to another txt file?
Re: Write to registry
Posted: 05 Oct 2012 02:35
by foxidrive
Try this:
Code: Select all
@Echo Off & Cls & Color 0E
Setlocal EnableDelayedExpansion
:: Reading Input From CSV File
For /F "tokens=1,2,3,4 delims=," %%A in ('Type "C:\File.csv"') Do (
set "IP=%%A" & set "PC=%%B" & set "User=%%C" & set "SDate=%%D"
ping !IP! -n 4 >nul
IF !errorlevel! == 0 (
Reg Add "\\!PC!\HKLM\SYSTEM\CurrentControlSet\Policies" /v "StartWarrantyDate" /t "REG_SZ" /d "!SDate!" /f
) else (
>>file.log echo IP %%A was not responding - "PC=%%B" "User=%%C" "SDate=%%D"
)
)
Re: Write to registry
Posted: 05 Oct 2012 02:47
by jvuz
With this I have all:
Code: Select all
@Echo Off & Cls & Color 0E
Setlocal EnableDelayedExpansion
:: Reading Input From CSV File
For /F "tokens=1,2,3,4 delims=," %%A in ('Type "C:\Users\USERNAME\Documents\Support\PC-Dates.csv"') Do (
set "IP=%%A" & set "PC=%%B" & set "User=%%C" & set "SDate=%%D"
ping !IP! -n 4 >nul
IF !errorlevel! == 0 (
Reg Add "\\!PC!\HKLM\SYSTEM\CurrentControlSet\Policies" /v "StartWarrantyDate" /t "REG_SZ" /d "!SDate!" /f
>> c:\StartWarrantyDate.log echo IP %%A OK: PC: %%B - User=%%C - SDate=%%D
) else (
>>c:\StartWarrantyDate.log echo IP %%A was not responding - PC: %%B - User=%%C - SDate=%%D
)
)
Thanks a lot!