PowerShell provides creative ways to simplify and automate tedious and repetitive tasks by creating scripts using sigle 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.
One of the benefits that JAMS provides is the power to script the creation of new Jobs, Setups, 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-Item to 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 Code
|
---|---|
Import-Module JAMS New-PSDrive JD JAMS localhost |
Or to modify an existing Job . . .
Modify an Existing JAMS Job |
Copy Code
|
---|---|
Import-Module JAMS New-PSDrive JD JAMS localhost |
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.
Before a connection can be made, users must first define the credentials for the user account in order to connect to the file server.
Getting Credentials from a JAMS User |
Copy Code
|
---|---|
Import-Module JAMS $userCredentials = Get-JAMSCredential -UserName JAMSUserName -Server JAMSServerName |
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 Code
|
---|---|
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 Retrieving Files |
Copy Code
|
---|---|
Send-JFSItem -Name C:\MyFile.txt -Destination C:\ServerDirectory\MyFile.txt |
To view files in a directory, use the cmdlet "Get-JFSChildItem".
Viewing Files in a Directory |
Copy Code
|
---|---|
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 Code
|
---|---|
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 Code
|
---|---|
$fileList = Get-JFSChildItem *.txt foreach($file in $fileList) { if (($file.IsFile) -and ($file.Modified -gt $checkDate)) { Receive-JFSItem $file } } |
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 Code
|
---|---|
$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 Code
|
---|---|
Set-JFSLocation -Location C:\NewDirectory |
Renaming and removing files using PowerShell is also straightforward. An example of renaming a file is shown below:
Renaming a File |
Copy Code
|
---|---|
Rename-JFSItem -Name OriginalName.txt -NewName NewName.txt |
An example script for removing a file is:
Removing a File |
Copy Code
|
---|---|
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. |
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.
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 Code
|
---|---|
# # 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 |