Category Archives: Technology

Managing network drives in Powershell

It used to be, in Windows, that the way to map network drives was with the net use command, but with Powershell this has become a bit easier. I haven’t, however, been able to find a simple overview of the commands and their usage so hopefully this will be of use to someone other than me.

There are three commands (cmdlets in Powershell speak) that you need to know about.

Get-PSDrive

This lists the currently mapped drives and a bit of information about them, most usefully the network path that the drive is mapped to. You can also enter the drive letter as a parameter so that

Get-PSDrive C

Tells you that drive C is mapped to the Windows C: drive. Obvious, really, but more useful if you want to know about other mapped drives.

New-PSDrive

This is the cmdlet that maps drives to to shared folders. If I want, for example, to map my Y drive to folder \sharedfiles on server Server the command would look like this:

New-PSDrive –Name “Y” –PSProvider FileSystem –Root “\\Server\sharedfiles” –Persist

The PSProvider switch tells Powershell what sort of data this drive is accessing. Filesystem, obviously, is the filesystem but you can also map to environment variables and other oddities.

The Root switch is needed so that Powershell understands the path name.

The Persist switch tells Powershell that this mapping is persistent so the mapped drive is available every time you reboot. This switch also causes Powershell to create a mapped network drive in Windows which can be accessed via all the standard Windows tools such as Windows Explorer.

Remove-PSDrive

When you no longer need the mapped drive, you can remove the mapping with this command.

Remove-PSDrive Y

Does exactly what you would expect.

Flattr this!

Using QShell and CL to work with Stream files in the IFS

It turns out that there is no simple way of generating a list of files currently residing in a folder on the IBM i IFS. Simple, in this case, would be a command like DSPLNK OUTPUT(*FILE). An API does exist, but a combination of not enough time and too much lazy proved to be quite a disincentive for me going down that route.

The issue was that we recieve a number of very big files and, to save a bit of bandwidth, these files were being zipped before being sent to the IBM i. Dropping the file into the IFS and unzipping it was easy enough but then I found myself with an archive folder containing one of more files. While I can set the name of the folder into which the files should be extracted, I have no way of determining beforehand what the file names will be.

Here’s a solution:

/* -------------------------------------------------------------------------- */
/* Program     : EXTRACTZIP                                                   */
/* Description : Retrieve and unpack a zipped archive                         */
/* Written by  : Paul Pritchard                                               */
/* Date        : 27/05/2015                                                   */
/* -------------------------------------------------------------------------- */
pgm
dcl &library *char 10 value('MYLIB')
dcl &fromfile *char 50
dcl &tombr *char 50 value('/qsys.lib/qtemp.lib/IMPORTP.file/IMPORTP.mbr')
dcl &dltfile *char 50
dclf EXTRACTP

/* Retrieve and the zipped file and unzip it.                                 */
/* I won't bore you with the details here, but the the production program is  */
/* retriving the ZIP file from an FTP server and using PKZIP to unzip it.     */
/* The extract directory is /home/EXPATPAUL/EXTRACTFLR which now contains one */
/* or more stream files                                                       */

/* Retrieve a list of extracted files                                         */
/* First, I use QShell to list the files in EXTRACTFLR. The output of this is */
/* redirected to ExtractedFiles.TXT.                                          */
/* In order to use this information, I copy the ExtractedFiles.TXT stream     */
/* to an ad-hoc physical file (QTEMP/EXTRACTP)                                */
qsh cmd('ls /home/EXPATPAUL/EXTRACTFLR/ > /home/EXPATPAUL/ExtractedFiles.TXT')
crtpf file(QTEMP/EXTRACTP) rcdlen(20)
cpyfrmstmf fromstmf('/home/EXPATPAUL/ExtractedFiles.TXT') +
           tombr('/qsys.lib/qtemp.lib/EXTRACTP.file/EXTRACTP.mbr') +
           mbropt(*REPLACE)

/* And now I can use QTEMP/EXTRACTP to drive my way through EXTRACTFLR and    */
/* copy each of the files in the archive into the IMPORTP physical file.      */
dowhile '1'
    rcvf
    monmsg msgid(CPF0864) exec(LEAVE)

    /* Copy the next sream file from the archive                              */
    chgvar &fromfile value('/home/EXPATPAUL/EXTRACTFLR/' *tcat &EXTRACTP)
    cpyfrmstmf fromstmf(&fromfile) tombr(&tombr) mbropt(*add) +
               STMFCCSID(*PCASCII)

    /* and then delete the stream file                                        */
    chgvar &dltfile value('rm /home/EXPATPAUL/EXTRACTFLR/' *tcat &EXTRACTP)
    qsh cmd(&dltfile)

    enddo

/* Clean up and exit                                                          */    
qsh cmd('rm /home/EXPATPAUL/ExtractedFiles.TXT')
dltf qtemp/EXTRACTP

endpgm

It should go without saying that some of the names have been changed and that the above program should be treated as a sample only.

Being able to move information between the QShell/IFS and traditional i5/OS environments is both useful and (in my experience) increasingly important. Although it does take a bit of thinking about, it isn’t difficult which is why I find that the oft-seen solution of “buy this tool” is both disappointing and (often) overkill.

Flattr this!

Gnome 3.16: A First Impression

I ran a system update today and Gnome 3.16 turned up. Which was nice.

One thing that I always found mildly annoying in Gnome 3.14 (and I think that this was also true of Gnome 3.12) was that when I clicked the shutdown button it would give me a choice between shutting down or logging out. If I wanted to reboot, I had to log out first and then reboot from the login screen.

So, wondering if anything had been done about the shutdown options, I clicked on the shutdown button.

And my laptop shut itself down.

10/10 for simple behaviour. Minus about 30 for being totally unexpected.

Flattr this!

More consolidation: Squirt

After spending some time cleaning up the mess of repsoitories that I have generated over the past few years, I thought it might be a good time to take a look at Squirt, a slightly larger project. In this case, Squirt still has its own repository but I have removed some branches. Now, instead of creating a seperate branch for every point release (because this is going to become very silly very quickly), I have two branches: development and master.

Obviously, the intention is to use development for any code changes and once I am happy with them, they will be pushed to master. Hopefully this will encourage to be a little less tardy about keeping the master branch up to date.

Flattr this!

It is done

I mentioned last month that I was in the process of consolidating the mess of repositories that I have created over the past few years. This is now done and, hopefully, will be a bit more manageable going forward.

I now have three consolidated repositories: silliness, utilities and utils-on-power. The Configurate repository is already logically enough organised and Squirt is large enough to justify its own repo.

I have also removed the static pages referring to these repos on this site. They were creating overhead without adding any value.

And I now know a lot more about Git that I knew this time last month.

Flattr this!

Squirt 0.2: Now with SMTP

As you may or may not be aware I have been tinkering with a command-line FTP client over the past year or so. And now seems as good a time as any to release version 0.2. The main change here is that the FTP client now supports sending data via SMTP.

In order for this to work, you need to be able to specify an SMTP server and port but, if you have a spare gmail account knocking around, server smtp.gmail.com and port 587 will work quite nicely.

As ever, you can find all of the source on GitHub. Feel free to tinker around with it.

Flattr this!

Fun with Git: Moving a file from one repository to another

This has taken a while for me to come up with an approach, and the approach I took probably isn’t the most efficient. In fact I am sure it isn’t.

But it worked and I thought it would be worth documenting it for the next time I want to do something like this.

So the issue was that I had two files (dicetastic.py and dicelib.py) that needed to be moved. These files has started out in their own (now deleted) Dicetastic repository and then merged into my utilities repository. Now I wanted to move them to silliness, while retaining the git history.

The approach I eventually took was to format a set of patches from the original repositor(y|ies) and then merge them into the new.

So first I created a folder called rollout:

mkdir rollout

Then I formatted the patches I needed from the utilities folder:

cd utilities
git format-patch -o ../rollout --root dice*

This created three patches in the rollout folder. Unfortunately this wasn’t quite what I wanted as none of the history from before I started mucking about with this.

So I went back and formatted the patches from the Dicetastic folder:

cd ../Dicetastic
git format-patch -o ../rollout --root dice*

And I will note here that I should have done this for everything, not the dice* files, in order to include the changes to the README. Oh well.

So now I have a collection of patches in the rollout folder, which I could use to rebuild the history in the silliness folder.

If I had done this properly, I should have been able to use:

git am ~/Code/patches/*.patch

Unfortunately, because I had created two sets of patches in the same folder I had a slightly scary numbering issue. And because there were only seven patches I decided to apply them individually so that I could see what I was doing.

But it worked and all that was left was for me to commit the changes and push them and both the files are in the right place, along with their full history.

I hope.

Flattr this!

Towel was a silly name for a repository

I mentioned about a week ago that I was starting to consolidate some of the short scripts that I have knocked together and pushed to GitHub over the years. This is still in progress but I have decided to consolidate these into two repositories: utilities and silliness. Because A lot of what I do is quite silly.

Going forward, anything short I throw together will end up in one or the other of these repositories. Now I just need to tidy up my current mess of folders.

Flattr this!