Friday, March 25, 2011

Scheduled Task Not Running a Batch Job

I've recently ran into this several times, so I'm thought I'd document it here.

I've created batch jobs (.bat) that do various things, like copy, move, delete files, download stuff from SFTP servers, etc. In the past, I've just used my credentials to run these jobs, but if I changed my password, the jobs would fail. Now, our IT department created a service account for me to Schedule these tasks, but I've had many issues with Scheduled Tasks not working once I changed the credentials to the service account.

The scheduled task will not show an error code, it just won't work or it will be stuck in a "running" state.

Here is what I have found out:

1. The service account should be a local admin
2. Delete your old Scheduled Task
3. Log onto the server using the service account credentials
4. Re-Create the Scheduled Task while logged on as the service account and use the service account credentials

This applies to Scheduled Tasks that run a .BAT file. All my other scheduled tasks work normally.

Tuesday, February 15, 2011

Migrating users to DotNetNuke

I've been on a 2 month hiatus, but I've come back with some new skills.  I've been working on a DotNetNuke portal to replace our existing MojoPortal.  I really wanted to like MojoPortal because of it's simplicity and ease of setup, but in the end MojoPortal Core development pretty much came to a halt.  There are just too many standard features that are missing, plus DNN has so much out of the box, I couldn't resist.  Plus, the community behind DNN is huge.

While browsing Mitchel Sellers' blog, I found some SQL that allows me to migrate the 700+ user accounts in our MojoPortal to DotNetNuke.  I've put it in a script and parametized it.

After I created this, I created a table from my old MojoPortal, which contains: UserName, FirstName, LastName, DisplayName, Email, and RoleName (Primary Role, which must exist in DNN before running the script).  Then, I ran it through a cursor and added all the users.

** ADDITION NOTE:  If you are using {owner}{prefix} in DNN, you'll need to modify the script.



-- =======================================================================================================
-- Author:        Larry Eisenstein
-- Create date: 2/15/2011
-- Description:    Creates a DNN User by copying an existing user.
-- This can be used to script a single account creation or migrating users from another system.
-- This works by creating the NewUser from an existing user.
-- The script was pulled from Mitchel Sellers website.  He has a great blog, so you should visit it.
-- http://www.mitchelsellers.com/blogs/articletype/articleview/articleid/84/creating-a-standard-dotnetnuke-user-via-sql.aspx
-- 
-- 
-- Req: 
-- 1. Know the Username/Password of an existing user. The password for the user you create will be the password 
-- of this user
-- 2. If you are assigning Roles, the RoleName MUST existing in the DotNetNuke site
-- 
-- Defaults:  I set up some defaults, but these can be changed via params or just change the defaults in the script.
-- 
-- Notes: 
-- Stored Procs used: aspnet_Membership_CreateUser
-- Tables Updated: users, Roles, UserPortals
 
-- The password works by copying the encrypted password, passwordsalt of the ExistingUser to your new user.  Then, you can
-- just login with that user's password.
-- ==========================================================================================================
CREATE PROCEDURE [dbo].[AAA_MigrateUser_sp]
@ApplicationName varchar(255) = 'DotNetNuke',  -- Search for applicationName in your web.config
@ExistingUserName varchar(255) = 'TestUser',   -- This must be an existing DNN user
@FirstName varchar(255) = 'Migrated',            
@LastName varchar(255) = 'UserAccount',
@DisplayName varchar(255) = 'Migrated UserAccount',
@NewUserName varchar(255),
@RoleName varchar(255) = 'Registered Users',
@PortalId int = 0,
@Email nvarchar(256) = 'TestUser@email.org'
AS
BEGIN
 
DECLARE @PasswordQuestion varchar(256),
@PasswordAnswer varchar(256),
@Pw varchar(255),
@PasswordSalt varchar(255),
@PasswordFormat int,
@IsApproved bit,
@CurrentTimeUtc datetime,
@CreateDate datetime,
@UniqueEmail int,
@UserId uniqueidentifier,
@DNNUserId int,
@NumUsers int
 
 
SELECT    @PasswordQuestion = '',
@PasswordAnswer = '',
@IsApproved = 1,
@CurrentTimeUtc = GETDATE(),
@CreateDate = @CurrentTimeUtc,
@UniqueEmail = 0
 
 
 
 
SELECT @NumUsers = COUNT(*)
FROM aspnet_Users
WHERE UserName = @NewUserName
 
 
IF(@NumUsers != 0)
return -1
 
 
SELECT    @Pw = m.password,
@PasswordSalt = m.passwordsalt,
@PasswordFormat = m.passwordformat
FROM aspnet_users u
INNER JOIN aspnet_membership m  ON (u.userid = m.userid)
WHERE u.UserName = @ExistingUserName
 
 
 
-- Make the stored procedure call
EXEC dbo.aspnet_Membership_CreateUser @ApplicationName, @NewUserName, @Pw,
@PasswordSalt, @email, @passwordquestion, @PasswordAnswer, 
@IsApproved, @CurrentTimeUtc, @CreateDate, @UniqueEmail,
@PasswordFormat, @UserId
 
 
-- Insert the record into the DotNetNuke users table
INSERT INTO users (Username, FirstName, LastName, IsSuperUser, Email,
DisplayName, UpdatePassword)
VALUES(@NewUserName, @FirstName, @LastName, 0, @Email, @DisplayName, 0)
 
 
-- Get the new userid, from the DNN users table
SELECT @dnnuserid = userid
FROM Users
WHERE username = @NewUserName
 
 
-- Now, insert the record into the user portals table
INSERT INTO UserPortals (userId, PortalId, CreatedDate)
VALUES(@dnnuserid, @PortalId, GETDATE()) 
 
 
-- Now Give the user permissions to the User Group you specified
IF(@RoleName != 'Registered Users' and @RoleName IS NOT NULL)
BEGIN
INSERT INTO UserRoles (userId, roleId)
SELECT @dnnuserid,
roleId
FROM Roles
WHERE RoleName = @RoleName
END
 
-- Now Give the user permissions to the REGISTERED Users group
INSERT INTO UserRoles (userId, roleId)
SELECT @dnnuserid,
roleId
FROM Roles
WHERE RoleName = 'Registered Users'
 
 
 
END

Tuesday, December 14, 2010

Outlook 2007 Tip: Color Code Messages

This is something I've always wanted in Outlook.  You can 'Categorize' emails and assign a color, but this does not change the color in the Outlook Inbox view.

The work around for me was to create a 'Custom Flag' with a 'Due Date' in the past.  This will turn the text of the email in the Inbox view to Red.

You can also set up some Advance Formatting Rules to change the Font(color, size, underline, bold) based on the Category, but this takes some time.  Here is the link for that: http://www.howto-outlook.com/howto/coloremailadvanced.htm

Here is a sample of what it looks like:

Monday, November 29, 2010

@schedule_uid is not a parameter for procedure sp_add_jobschedule

I received this error when trying to copy a SQL job from one server to another, by trying to script out the create statement:

Msg 8145, Level 16, State 1, Procedure sp_add_jobschedule, Line 0

@schedule_uid is not a parameter for procedure sp_add_jobschedule



The problem is that the parameters for the stored procedure, sp_add_jobschedule, have changed from SQL2005 to SQL2008, so you need to change the script to match.

- SQL 2005 uses @schedule_id (an integer) and SQL 2008 uses @schedule_uid (a uniqueidentifier)


- All you have to do is change the parameter name and data-type to the correct value
    If it's @schedule_uid, change it to @schedule_id.
    Then, find an un-used integer value in: select * from sysjobschedules (MSDB database)

- If you are going the other way, you can change the parameter to @schedule_uid, then create a new
   uniqueidentifier by running:  select newid()

Friday, November 26, 2010

Grails Bootstrapping Issue

I'm sure this is a newbie issue, but it's interesting to me.

I've created the Race applications from the book, "Getting Started With Grails, 2nd Edition" by Jason Rudolph.  After creating the Runner domain class, I add "package racetrack", then I try to bootstrap some data.



   1:  class BootStrap {
   2:  def init = { servletContext ->
   3:  def jane = new Runner(firstName:'Jane',lastName:'Doe')
   4:  jane.save()
   5:  }
   6:  def destroy = {}
   7:  }

But, when I run the application, I don't get any data populated and no error.

The answer was in #GRAILS-3842.  In order to create the data, you MUST set the values for "ALL PROPERTIES THAT ARE NOT NULLABLE".

So, when you create a new Runner(), you must supply values for all properties or mark the properties as "nullable:true".  Also, there is a new parameter for the save() method, save(failOnError:true).  This will cause the compilation to fail if the save() method in Bootstrap does not work.

Here is the new code:


   1:  class Runner {
   2:   
   3:  static constraints = {
   4:      firstName(blank:false)
   5:      lastName(blank:false)
   6:      dateOfBirth(nullable:true)
   7:      gender(inList:["M", "F"])
   8:      address(nullable:true)
   9:      city(nullable:true)
  10:      state(nullable:true)
  11:      zipcode(nullable:true)
  12:      email(email:true)
  13:  }
  14:  static hasMany = [registrations:Registration]
  15:      String firstName
  16:      String lastName
  17:      Date dateOfBirth
  18:      String gender
  19:      String address
  20:      String city
  21:      String state
  22:      String zipcode
  23:      String email
  24:      String toString(){
  25:          "${lastName}, ${firstName} (${email})"
  26:      }
  27:   
  28:   
  29:   
  30:   
  31:   
  32:   
  33:  class BootStrap {
  34:   
  35:      def init = { servletContext ->
  36:          def jane = new Runner(firstName:'Jane', lastName:'Doe', gender:'F',city:'Atlanta',state:'GA',email:'jane@toto.com')
  37:          jane.save(failOnError:true)
  38:          def joe = new Runner(firstName:'Joe', lastName:'Blow', gender:'M',city:'Atlanta',state:'GA',email:'joe@toto.com')
  39:          joe.save(failOnError:true)
  40:          def larry = new Runner(firstName:'Larry', lastName:'Eisenstein', gender:'M',city:'Lilburn',state:'GA',email:'larrye@toto.com')
  41:          larry.save(failOnError:true)
  42:   
  43:      }  
  44:   

Tuesday, November 16, 2010

Delete files based on Last Modified Date using FORFILES.exe

Deleting files based on last modified date or running some command on these files has been a real sticking point for Windows users.  Unix/Linux people would always hold some of those cool command line features over our heads to prove why Linux is better than Windows.

Windows Server 2000/2003/2008 has a command called ForFiles.exe (http://technet.microsoft.com/en-us/library/cc753551(WS.10).aspx).  This is a great tool once you get used to the sytnax.  You can find files based on Last Modified Date, then run a command(like del) on each of those files.


FORFILES  /P  E:\MSSQL\Backup  /S  /M  *.full.zip   /D -30   /C  “cmd  /c  del  @path”


/S – look at subdirectories
*.full.zip – that is the file pattern, you could have it be like *.txt for example
/D -30 = files greater than 30 days
/C – thats the command you want to run





Here is what I use in my SQL script:

   1:    declare @full_location varchar(8000)
   2:    SET @full_location = @backup_location + @backup_db_name 
   3:    SET @command = 'forfiles -p "' + @full_location + '" -s -m *.full.zip -d -1 -c "cmd /c move /y @path "' + @archive_destination
   4:    print @command
   5:    EXEC xp_cmdshell @command, NO_OUTPUT
   6:    --print @command
   7:   

I don't know why this isn't in Windows XP.  This is a great tool

Sunday, November 14, 2010

Deactivate Netflix on PS3

This has nothing to do with programming, but I just found a couple great tricks if you are having issues with your Netflix account on your PS3.


Deactivate Netflix from your PS3
1. Click the Netflix application
2. As soon as you do, hold Start & Select at the same time
3. This will allow you to deactivate Netflix from the PS3


Reset your Netflix account on the PS3
1. Start the Netflix account
2. You will be in the movie selections area(don't worry, this will work)
3. With the DPAD, hit these buttons:
UP, UP, DOWN, DOWN, LEFT, RIGHT, LEFT, RIGHT, UP, UP, UP, UP

4. VIOLA!!!  You can now log on as a different user.