The Power of PowerShell

PowerShell provides creative ways to simplify and automate tedious and repetitive tasks by creating scripts using single or multiple commands.

JAMS has fully incorporated PowerShell. The JAMS Client component includes a PowerShell Module that contains dozens of JAMS cmdlets which are listed and detailed in the topic:JAMS PowerShell Cmdlets.

There is so much that PowerShell can do within JAMS that we can only provide a few basic examples. For more PowerShell solutions, please visit the JAMS Technical Support site at: support.jamsscheduler.com and scroll down to access the PowerShell knowledge base articles.

NOTE: The PowerShell cmdlets listed below cannot be used with PowerShell Core.

Example: Creating or Modifying Existing JAMS Jobs

One of the benefits that JAMS provides is the power to script the creation of new Jobs, Sequences, Folders, or any type of JAMS definition. Because the JAMS Module includes a PowerShell provider, you can use native PowerShell commands like New-Item or Get-Itemto define or change any JAMS definition.

For example, to create a new Job in JAMS, use the following PowerShell script.

Creating a New JAMS Job

Copy
Import-Module JAMS -ErrorAction SilentlyContinue

New-PSDrive JD JAMS localhost -ErrorAction SilentlyContinue
$job = New-Item JD:\Samples\MyJob123 -itemtype Job -MethodName Command        
$job.Description = "This job was added programmatically"                        
# Create and Add a Schedule Trigger Element to the Job
#
$ScheduleTrigger = [MVPSI.JAMS.ScheduleTrigger]::New("Monday, Tuesday, Wednesday, Thursday",
[MVPSI.JAMS.TimeOfDay]"23:40"
$ScheduleTrigger.Enabled = $false$job.Elements.Add($ScheduleTrigger)
#                
# Define Jobs Source                    
#            
$job.Source = "Dir C:\"
#                
# Create a new Resource Requirement and add it to the Job
#        
$Resource = [MVPSI.JAMS.ResourceRequirement]::New("new", 12)                
$job.Elements.Add($Resource)
#                    
# Update the Job object for changes to take effect                    
#                
$job.Update()

Modify an Existing JAMS Job

Copy
Import-Module JAMSNew-PSDrive JD JAMS localhost$job = Get-Item JD:\Samples\MyJob                    
$job.Description = "This job was added programmatically"                    
$sched = new-object MVPSI.JAMS.ScheduleTrigger("Monday, Tuesday, Wednesday, Thursday", [MVPSI.JAMS.TimeOfDay] "21:40")$job.Elements.Add($sched)                    
$job.Properties.SetValue("Enabled", $false)                    
$job.Source = "Dir C:\"                    
$requires = new-object MVPSI.JAMS.ResourceRequirement("Widgets", 12)                    
$job.Elements.Add($requires)                    
$requires = new-object MVPSI.JAMS.ResourceRequirement("Gadgets", 5)                    
$job.Elements.Add($requires)                
$job.Update()

Using the JAMS File Transfer PowerShell Cmdlets

In addition to using the "File Transfer" execution method, file transfers can also be performed using the JAMS PowerShell Module. The module supports transfers in the following formats: FTP, FTPS, and SFTP.

Importing the JAMS Module and Getting Credentials

Before a connection can be made, users must verify:
  1. MyJob does not exist in the\Samples Folder
  2. The following resources already exist:
    1. Widgets
    2. Gadgets

Getting Credentials from a JAMS User

Copy
Import-Module JAMS
New-PSDrive JD JAMS localhost
$job = New-Item JD:\Samples\MyJob -ItemType "Job" -MethodName "Command"
$job.Description = "This job was added programmatically"
$sched = new-object MVPSI.JAMS.ScheduleTrigger("Monday, Tuesday, Wednesday, Thursday", [MVPSI.JAMS.TimeOfDay] "23:40")
$job.Elements.Add($sched)
$job.Properties.SetValue("Enabled", $false)
$job.Source = "Dir C:\"
$requires = new-object MVPSI.JAMS.ResourceRequirement("Widgets", 12)
$job.Elements.Add($requires)$requires = new-object MVPSI.JAMS.ResourceRequirement("Gadgets", 5)
$job.Elements.Add($requires)
$job.Update()

Establishing a Connection

Once the credentials have been established, a connection can then be made. The following example shows how to define a FTP connection script.

FTP Connection Script

Copy
Connect-JFTP -Credential $userCredentials -Name YourFileServerName

The other two transfer method follow the same format except that "Connect-JFTP" is replaced with "Connect-JFTPS" or "Connect-JSFTP".

Once a connection is made, users can then retrieve or send files to the server.

Sending and Receiving Files

Sending and Retrieving Files

Copy
Send-JFSItem -Name C:\MyFile.txt -Destination C:\ServerDirectory\MyFile.txt

Viewing File Details

To view files in a directory, use the cmdlet "Get-JFSChildItem".

Viewing Files in a Directory

Copy
Get-JFSChildItem -Path C:\Logs\

Alternatively, you view the details about a specific item using the JAMS cmdlet: "Get-JFSItem".

Getting Details on a Specific Item

Copy
Get-JFSItem -Path C:\Logs\Audit.log

The "Get-JFSChildItem" cmdlet is similar to the "Get-ChildItem" cmdlet, as they both return a collection of objects.

The "Get-JFSChildItem" returns a collection of JAMSFileServerItems. Each item describes a single file or directory located on the file server. You can process these items using all the standard PowerShell commands, for example:

Get-JFSChildItem

Copy
$fileList = Get-JFSChildItem *.txt
foreach($file in $fileList)
{
  if (($file.IsFile) -and ($file.Modified -gt $checkDate))  
  {   
     Receive-JFSItem $file  
  }
 }

Directory Modification

The "Get-JFSLocation" cmdlet allows you to control the current path on the file server. The example below to shows how to store the current directory in a PowerShell variable.

Store the Current Directory in a PowerShell Directory

Copy
$CurrentDirectory = Get-JFSLocation

The "Set-JFSLocation" cmdlet gives users the ability to change the directory on a file server using the format:

Changing the Directory on a File Server

Copy
Set-JFSLocation -Location C:\NewDirectory

Renaming and Removing Files

Renaming and removing files using PowerShell is also straightforward. An example of renaming a file is shown below:

Renaming a File

Copy
Rename-JFSItem -Name OriginalName.txt -NewName NewName.txt

An example script for removing a file is:

Removing a File

Copy
Remove-JFSItem -Path "C:\FTPNewName.txt" -Confirm:$false
NOTE: Setting the "-Confirm switch to false means there should not be a verification prompt before deleting the file.

Disconnecting from the FTP Server

Once the file transfer actions have completed, you must disconnect from the FTP server in order to close the connection using the "Disconnect-JFS" cmdlet.

FTP Transfer Script Example

Any of the aforementioned cmdlets can be issued from a PowerShell console or from a JAMS Job that uses the PowerShell execution method. Below is an example script using these cmdlets to perform a FTP transfer.

FTP Transfer Example

Copy

#
# Get the credentials for our FTP user
$userCredentials = Get-JAMSCredential JAMSFTPUser

#
# Connect to the FTP Server
#
Connect-JFTP -Credential $userCredentials -Name FTPServer7

#
# Send a file
#
Send-JFSItem -Name C:\Logs\Audit_Data.txt -Destination C:\Common\Audit_Data.txt
#
# Retrieve a file
#
Receive-JFSItem -Name C:\Common\Audit_Data.txt -Destination C:\Logs\Audit_Data.txt

#
# Disconnect from the server
#
Disconnect-JFS