I am trying to create a windows C# service which runs on server connected with 10 to 15 target devices via LAN. So here is the what I am doing in service i am reading the data from csv file which is in following format:
TargetComputername1,Date1,Time1,Password1
TargetComputername2,Date2,Time2,Password2
TargetComputername3,Date3,Time3,Password3
.
.
TargetComputernameN,DateN,TimeN,PasswordN
I have to run a vbs inside service but on the specified date & time in csv with Targetname and password as parameter.
So for N device i think i have to create N different instance of timer and pass the parameters how should i do that?
Pleas help thanks in advance...
What I have tried:
using (var parser = new TextFieldParser(AppPath + "\\iPXEScheduler.csv"))
{
parser.TextFieldType = FieldType.Delimited;
parser.SetDelimiters(",");
string[] line;
int count = 1;
while (!parser.EndOfData)
{
count++;
line = parser.ReadFields();
if (bStringContains(line[0], sDeviceNameGet("StoreID"), StringComparison.OrdinalIgnoreCase))
{
if (validate(line, count))
{
SetupTimer();
}
}
}
}
private bool validate(string[] line, int count)
{
bool bISValidate = true;
Double dLOCCountDown = 1.0;
string sLOCTime;
DateTime dtScheduledDateTime = DateTime.Now.AddYears(1000);
string[] DateTimeformat = { "MM/dd/yyyy HH:mm", "M/d/yyyy H:mm", "MM/d/yyyy H:mm", "M/dd/yyyy H:mm", "M/d/yyyy HH:mm", "MM/d/yyyy HH:mm", "M/dd/yyyy HH:mm" };
if (line.Length != 5)
{
WriteLog("There are more or less then 5 parameter specified in csv at line : " + count);
bISValidate = false;
goto Error;
}
if (line[0]=="" && line[1]=="" && line[2]=="" && line[3]==""&&line[4]=="")
{
WriteLog("One of the parameter found blank at : " + count);
bISValidate = false;
goto Error;
}
if (line[0].Length != 12)
{
WriteLog("Please check the device name at line : " + count);
bISValidate = false;
goto Error;
}
if (bStringContains(line[2], "CLOSE", StringComparison.OrdinalIgnoreCase))
{
sLOCTime = "00:00";
}
else
{
sLOCTime = line[2];
}
try
{
dtScheduledDateTime = DateTime.ParseExact(line[1] + " " + line[2], DateTimeformat, null, System.Globalization.DateTimeStyles.None);
}
catch(Exception ex)
{
WriteLog("Error parsing date and time : " + ex.Message);
WriteLog("Correct date format is : MM/dd/yyyy and time is : HH:mm (24 hours)");
bISValidate = false;
goto Error;
}
try
{
dLOCCountDown = Double.Parse(line[3]);
if (dLOCCountDown < 1.0 && dLOCCountDown > 99.0)
{
WriteLog("Countdown can not be less then 1 and greater then 99 " + dLOCCountDown);
bISValidate = false;
goto Error;
}
}
catch(Exception ex)
{
WriteLog("Error parsing CoutDown : " + ex.Message);
}
if (!IsDigitsOnly(line[4]))
{
WriteLog("Password seems to contain character other then digit which is not allowed at line : " + count);
bISValidate = false;
goto Error;
}
Error:
if (bISValidate == false)
{
return false;
}
else
{
DateTime dtGLOScheduleDateTime = dtScheduledDateTime;
Double dGLOCountDown = dLOCCountDown;
string sGLOPassword = DecryptPassword(line[4]);
string sGLODevice = line[0];
return true;
}
}
private void SetupTimer()
{
Timer Countdown = new Timer();
Countdown.AutoReset = false;
WriteLog(dtGLOScheduleDateTime.ToString());
Countdown.Interval = (dtGLOScheduleDateTime.Subtract(DateTime.Now).TotalSeconds * 1000) - (dGLOCountDown * 24 * 60 * 60 * 1000);
Countdown.Elapsed += (sender, e) => Countdown_Elapsed(sender, e, sGLOPassword);
Countdown.Start();
}
private void Countdown_Elapsed(object sender, ElapsedEventArgs e, string sGLOPassword)
{
WriteLog(DateTime.Now.ToString() + " : " + sGLOPassword);
}
But how do i pass the parameter as well