2007-07-16

Accessing Windows PowerShell from SAS

Windows PowerShell, previously Microsoft Shell or MSH (codenamed Monad) is an extensible command line interface (CLI) shell and scripting language product developed by Microsoft.—Wikipedia

PowerShell provides ‘easy’ access to Windows via a command line, and can be used for many things. I wanted to find the total disk space and amount free which we can do like this:
get-wmiobject -class win32_logicaldisk
Outputs:


DeviceID : A:
DriveType : 2
ProviderName :
FreeSpace :
Size :
VolumeName :

DeviceID : C:
DeviceID : C:
DriveType : 3
ProviderName :
FreeSpace : 221132865536
Size : 293552017408
VolumeName :
PowerShell uses STDIN and STDOUT, which is how we can pipe it into SAS. Note adding the filter to restrict to local, non-removable, drives:
filename diskinfo pipe "powershell get-wmiobject -class win32_logicaldisk -filter 'DriveType=3'";

data _null_;
infile diskinfo;
input;
put _infile_;
run;
Which yields (note the 2 leading blank lines):


DeviceID : C:
DriveType : 3
ProviderName :
FreeSpace : 221132881920
Size : 293552017408
VolumeName :
PowerShell can reformat output, but I coded SAS to read it as-is:
data _null_;
infile diskinfo firstobs=3 truncover;
input
@':' +1 DeviceID $1.
/
/
/ @':' +1 FreeSpace 32.
/ @':' +1 Size 32.
/ @':' +1 VolumeName $32.
/;

put _all_;
run;
Which gives us variables holding the total size of the C drive (in bytes) and free space:
DeviceID=C FreeSpace=221132861440 Size=293552017408 VolumeName=  _ERROR_=0 _N_=1

No comments: