Always get "Access Denied" when attempting to chan
Posted: Wed Oct 15, 2008 10:52 pm
Hi all,
I have a routine to change the password on Scheduled Tasks.
Seems to have a problem on Vista.
function TMainForm.ChangeTaskPasswords(const AComputerName, AUserName, APassword: String): Boolean;
// NOTE: Unlike with Services, which uses .\account for local accounts, Scheduled Tasks seem to use
// computername\account.
var
TaskScheduler: TTaskScheduler;
Index: Integer;
LogEntry: String;
ErrorMessage: String;
begin
Result := True;
try
TaskScheduler := TTaskScheduler.Create( nil );
try
TaskScheduler.AutoRefresh := False;
TaskScheduler.Duplicate := tdError;
TaskScheduler.Active := True;
TaskScheduler.TargetComputer := '\\' + AComputerName;
TaskScheduler.UseTaskScheduler1Only := True;
TaskScheduler.Refresh;
for Index := 0 to TaskScheduler.Count - 1 do begin
// Apparently the following call lets us access task properties:
try
TaskScheduler.Items[ Index ].Activate;
if AnsiSameText( TaskScheduler.Items[ Index ].AccountName, AComputerName + '\' + AUserName ) then
begin
// Log the action
LogEntry := Format( 'Changing Password for Task: %0:s', [ TaskScheduler.Items[ Index ].Name ] );
LogAndAddProgress( LogEntry );
TaskScheduler.Items[ Index ].SetAccountInformation( TaskScheduler.TargetComputer {AComputerName} + '\' + AUserName, APassword );
TaskScheduler.Items[ Index ].Save; // when I get here, I always get the error "Access Denied" (I do have the proper remote firewall settings on the Vista machine I am trying to change)
// Log the success
LogEntry := Format( 'Task Password Change (Success) | Task: %0:s',
[ TaskScheduler.Items[ Index ].Name ]);
LogAndAddProgress( LogEntry );
end;
except
on E: Exception do begin
// Log the failure
ErrorMessage := SysErrorMessage( GetLastError );
LogEntry := Format( 'Task Password Change (Failed) | Task: %0:s | Error: %1:s',
[ TaskScheduler.Items[ Index ].Name, ErrorMessage ]);
LogAndAddProgress( LogEntry );
end;
end;
end;
finally
TaskScheduler.Free;
end;
except
Result := False;
end;
end;
Any thoughts/suggestions?
I have a routine to change the password on Scheduled Tasks.
Seems to have a problem on Vista.
function TMainForm.ChangeTaskPasswords(const AComputerName, AUserName, APassword: String): Boolean;
// NOTE: Unlike with Services, which uses .\account for local accounts, Scheduled Tasks seem to use
// computername\account.
var
TaskScheduler: TTaskScheduler;
Index: Integer;
LogEntry: String;
ErrorMessage: String;
begin
Result := True;
try
TaskScheduler := TTaskScheduler.Create( nil );
try
TaskScheduler.AutoRefresh := False;
TaskScheduler.Duplicate := tdError;
TaskScheduler.Active := True;
TaskScheduler.TargetComputer := '\\' + AComputerName;
TaskScheduler.UseTaskScheduler1Only := True;
TaskScheduler.Refresh;
for Index := 0 to TaskScheduler.Count - 1 do begin
// Apparently the following call lets us access task properties:
try
TaskScheduler.Items[ Index ].Activate;
if AnsiSameText( TaskScheduler.Items[ Index ].AccountName, AComputerName + '\' + AUserName ) then
begin
// Log the action
LogEntry := Format( 'Changing Password for Task: %0:s', [ TaskScheduler.Items[ Index ].Name ] );
LogAndAddProgress( LogEntry );
TaskScheduler.Items[ Index ].SetAccountInformation( TaskScheduler.TargetComputer {AComputerName} + '\' + AUserName, APassword );
TaskScheduler.Items[ Index ].Save; // when I get here, I always get the error "Access Denied" (I do have the proper remote firewall settings on the Vista machine I am trying to change)
// Log the success
LogEntry := Format( 'Task Password Change (Success) | Task: %0:s',
[ TaskScheduler.Items[ Index ].Name ]);
LogAndAddProgress( LogEntry );
end;
except
on E: Exception do begin
// Log the failure
ErrorMessage := SysErrorMessage( GetLastError );
LogEntry := Format( 'Task Password Change (Failed) | Task: %0:s | Error: %1:s',
[ TaskScheduler.Items[ Index ].Name, ErrorMessage ]);
LogAndAddProgress( LogEntry );
end;
end;
end;
finally
TaskScheduler.Free;
end;
except
Result := False;
end;
end;
Any thoughts/suggestions?