How to create a scheduled task using PowerShell

February 21, 2008

I found this insanely easy, thanks to some well written Microsoft documentation (shocking I know). If you need to create a scheduled task of an application in your PowerShell script you can call the Schtasks.exe application. Below is an example I used to create a scheduled task to run my exe every day at 1:00a.m. using the System account:

Write-Host “Creating Account Collector Scheduled Task”

Schtasks /create /sc DAILY /tn AccountCollector /tr “`”`”`”`”C:\Program Files\AMF\AMFCollector.exe`”`”`”`”” /s devsbvm01 /ru System /st 01:00:00

Write-Host “Scheduled Task Created”

That’s it, works painlessly on a local or remote server, which in my example, devsbvm01 is my remote server. Now, you might be wondering “whats with all the quotes?”. Well, I struggled for a long time trying to figure out why the scheduled task would be created but would never run. Turns out that the qoutes were being stripped out, thus causing the path to break. I know its sleazy, but turns out you need four sets of quotes in order to keep them from being stripped out! If anyone ever finds a better way, let me know but this seems to be it for now! So make sure you put those quotes in there!

Here is the link to the Microsoft tech net article that explains in great detail the Schtasks.exe application.

-Flea#


How to check if an object exists in a SQL 2005 Database

February 13, 2008

When executing some SQL code on an object in database such as a table, stored procedure, or view, it is important to check to see if these objects currently exist in the database. It is important because it makes your code more reusable, especially if multiple developers will be running it on their local databases. Here is a simple line of SQL you can run to find various objects in a SQL 2005 database.

How to check if an object exists in a SQL 2005 Database

if exists (select * from sys.objects where [object_id] = object_id(‘the_name_of_your_object’))
begin
print ‘Hello World’
end


How to check if a stored procedure exists in a SQL 2005 Database

Say your Database contained several hundred stored procedures and you wanted to know if the stored procedure usp_GetNames existed. Simply run the following example from above:

if exists (select * from sys.objects where [object_id] = object_id(‘dbo.usp_GetNames’) )
begin
print ‘Hello World’
end

How to check if a view exists in a SQL 2005 Database

What if you wanted to create a new view called dbo.vw_OldCustomers, but wanted to see if that view existed before you created it? Again, run the same SQL code above.

if exists (select * from sys.objects where [object_id] = object_id(‘dbo.vw_OldCustomers’) )
begin
— Logic to create the view and populate it
end


-Flea#


How to Install an Application on a Remote Machine Using PowerShell

January 16, 2008

Although this seems as it would be a common tasks for administrators and developers alike, I found very little documentation on the Internet on how to install an msi or exe on a remote machine using PowerShell. I even found comments saying it couldn’t be done at all! Bull Hugly, it can be done! I have provided two pieces of code that you can use to install either an msi package or exe on a remote server using PowerShell.

Installing an MSI

Using a short hand of WMI in PowerShell , you can execute the following code to install your msi package:

$box=”deviis01” #this is the name of your server

$product= [WMICLASS]”\\$box\ROOT\CIMV2:win32_Product”

Write-Host “Installing software on $box”


$product.Install(“c:\Setup \somesoftwarepackage.msi”)

The best part is it will install it silently so you don’t have to worry about command arguments to do that.

Installing an EXE

The exe was more of a pain and I wasn’t able to get it to work with the Install method. If someone else is able to get it to work, please post a comment to share with the rest of us because I would like to know if it is possible, however, here is what you can do to install your exe.

$box=”deviis01” #this is the name of your server

Write-Host “Installing software on $box”

([WMICLASS]”\\$box\ROOT\CIMV2:win32_process”).Create(“cmd.exe /c c:\Setup\somesoftware.exe /s /v`” /qn”)

This actually works by creating a new process on the remote server, calling the cmd.exe and passing it the arguments for installing your program. One thing to make note of is all the command switches I have at the end. You will want to check your exe to see what switches it takes; for example mine above is using an exe created with InstallShield so the command line options might be different, so just keep that in mind.

-Flea#