Something was amiss in that script, the way the binary stream was assembled for the target file. Try this example -- instead of writing the destination file in pieces, this one assembles the bytes in memory and uses WriteAllBytes.
# Example of how to use PowerShell to script the
# SAS Integration Technologies client
# You can connect to a remote SAS Workspace
# and run a program, retrieve the SAS log and download a file
# To use: change this script to reference your SAS Workspace
# node name, port (if different), and user credentials
# create the Integration Technologies objects
$objFactory = New-Object -ComObject SASObjectManager.ObjectFactoryMulti2
$objServerDef = New-Object -ComObject SASObjectManager.ServerDef
$objServerDef.MachineDNSName = "yourhost.company.com" # SAS Workspace node
$objServerDef.Port = 8591 # workspace server port
$objServerDef.Protocol = 2 # 2 = IOM protocol
# Class Identifier for SAS Workspace
$objServerDef.ClassIdentifier = "440196d4-90f0-11d0-9f41-00a024bb830c"
try
{
# create and connect to the SAS session
$objSAS = $objFactory.CreateObjectByServer(
"SASApp", # server name
$true,
$objServerDef, # built server definition
"sasdemo", # user ID
"Password" # password
)
}
catch [system.exception]
{
Write-Host "Could not connect to SAS session: " $_.Exception.Message
}
# change these to your own SAS-session-based
# file path and file name
$destPath = "/u/userid/temp"
$destFile = "class"
# local directory for downloaded file
$localPath = "c:\temp"
# program to run
# could be read from external file
$program =
"Filename csv '$destPath/$destFile.csv';
Data _null_;
Set sashelp.class;
File csv dlm=',';
Put (_all_) (+0);
Run;"
# run the program
$objSAS.LanguageService.Submit($program);
# flush the log - could redirect to external file
Write-Output "SAS LOG:"
$log = ""
do
{
$log = $objSAS.LanguageService.FlushLog(1000)
Write-Output $log
} while ($log.Length -gt 0)
# now download the image file
$fileref = ""
# assign a Fileref so we can use FileService from IOM
$objFile = $objSAS.FileService.AssignFileref(
"csv", "DISK", "$destPath/$destFile.csv",
"", [ref] $fileref);
$StreamOpenModeForReading = 1
$objStream = $objFile.OpenBinaryStream($StreamOpenModeForReading)
# define an array of bytes
[Byte[]] $bytes = 0x0
$allbytes = @()
$endOfFile = $false
$byteCount = 0
do
{
# read bytes from source file, 1K at a time
$objStream.Read(1024, [ref]$bytes)
$allbytes += $bytes
# if less than requested bytes, we're at EOF
$endOfFile = $bytes.Length -lt 1024
# add to byte count for tally
$byteCount = $byteCount + $bytes.Length
} while (-not $endOfFile)
[io.file]::WriteAllBytes("$localPath\$destFile.csv",$allbytes)
# close input and output files
$objStream.Close()
# free the SAS fileref
$objSAS.FileService.DeassignFileref($objFile.FilerefName)
Write-Output "Downloaded $localPath\$destFile.csv: SIZE = $byteCount bytes"
$objSAS.Close()
Output for me:
Downloaded c:\temp\class.csv: SIZE = 384 bytes
... View more