In a previous article we demonstrated a number of script packager solutions. In this post we show other ways to provide information to a script executable.
The sample script is used to map a network drive to an existing shared folder. We’re going to use both the “New-PSDrive” cmdlet and the “net use .. /delete” DOS command to make it work.
In this script, we’ll be using both the Read-Host and the Get-Credential cmdlets to pass some information to a script executable.
First we will prompt to enter the drive letter to be used to map to a network shared folder.
In this scenario the network shared folder is already provided within the code. The next step is to provide the user credentials for connecting to the network shared folder.
In the logic we check to see if the drive is already in use. If the drive letter is already in use, then it will proceed to remove the existing one attached to the network shared folder connection.
This solution will work on Windows 7, Windows 10, and Windows Server 2012 (or greater).
Now, lets create a couple of package solutions.
In the packager, we are going to use the following script engines:
- SAPIEN PowerShell V5 Host (Command Line)
- SAPIEN PowerShell V5 Host (Windows Application)
In the packager “Output Settings” section, in the “Icon file” field we can take advantage of the free SAPIEN Icon library and add an icon from the list. The “Folder.ico” icon was selected.
This way the icon will be included when building the executable.
Creating a Command Line executable
The packager script engine option “SAPIEN PowerShell V5 Host (Command Line)” gives you the ability to run the executable from the command line, or from File Explorer.
For the command line example, we created the executable “mapNetDriveCLI.exe“. When running it from a shell console it will prompt to enter for both the drive letter and user credentials.
When running the executable again with an already used drive letter, it will proceed to remove the connection to the shared folder.
Creating the Windows Application executable
Although the script is not a Windows application, using the packager script engine “SAPIEN PowerShell V5 Host (Windows Application)” will give it the look and feel of a windows application. A pop-up window will prompt to first to enter the drive letter and then the user credentials.
For the Windows Application example, we created the executable “mapNetDrive.exe“. When running it from a shell console or the file browser, a windows pop-up form will prompt to enter for both the drive letter and then the user credentials.
And again, testing running the script executable will remove the existing drive letter.
Another mapping option
In Windows 10, and Windows Server 2016 (or greater), you’ll find the SmbShare module. You could use both the New-SmbMapping and the Remove-SmbMapping instead of the PSDrive cmdlets.
As you can see, both the script and the executables created will behave the same. This script has a lot of room for enhancements.
Download the sample script code: SampleMapDrive.zip
- About Managing Windows PowerShell Drives.
- About SmbShare.
- Four ways to package a non-GUI PowerShell script as an executable file.
As always, if you have any ideas, comments, or feedback, please visit our feedback forum and reference this post.
Max Trinidad is a Technology Evangelist at SAPIEN Technologies Inc., and a Microsoft PowerShell MVP. You can reach him at firstname.lastname@example.org