<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Powershell to SAS EG in SAS Enterprise Guide</title>
    <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Powershell-to-SAS-EG/m-p/295227#M20165</link>
    <description>Hi guys,&lt;BR /&gt;     I'm really struggling here. My file looks fine in UNIX but when I open it on my network drive it just contains "System.Byte[ ]". If someone could shed any light on why this may be that would be much appreciated. This is what I have so far  &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/4"&gt;@ChrisHemedinger&lt;/a&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;# Example of how to use PowerShell to script the&lt;BR /&gt;# SAS Integration Technologies client&lt;BR /&gt;# You can connect to a remote SAS Workspace&lt;BR /&gt;# and run a program, retrieve the SAS log and download a file&lt;BR /&gt;&lt;BR /&gt;# To use: change this script to reference your SAS Workspace&lt;BR /&gt;# node name, port (if different), and user credentials&lt;BR /&gt;&lt;BR /&gt;# create the Integration Technologies objects&lt;BR /&gt;$objFactory   = New-Object -ComObject SASObjectManager.ObjectFactoryMulti2&lt;BR /&gt;$objServerDef = New-Object -ComObject SASObjectManager.ServerDef&lt;BR /&gt;$objServerDef.MachineDNSName = "yournode.company.com" # SAS Workspace node&lt;BR /&gt;$objServerDef.Port           = 8591  # workspace server port&lt;BR /&gt;$objServerDef.Protocol       = 2     # 2 = IOM protocol&lt;BR /&gt;# Class Identifier for SAS Workspace&lt;BR /&gt;$objServerDef.ClassIdentifier = "440196d4-90f0-11d0-9f41-00a024bb830c"&lt;BR /&gt;&lt;BR /&gt;try&lt;BR /&gt;{&lt;BR /&gt;    # create and connect to the SAS session &lt;BR /&gt;    $objSAS = $objFactory.CreateObjectByServer(&lt;BR /&gt;                    "SASApp",      # server name&lt;BR /&gt;                    $true, &lt;BR /&gt;                    $objServerDef, # built server definition&lt;BR /&gt;                    "sasdemo",     # user ID&lt;BR /&gt;                    "Password1"    # password&lt;BR /&gt;                    )&lt;BR /&gt;}&lt;BR /&gt;catch [system.exception]&lt;BR /&gt;{&lt;BR /&gt;  Write-Host "Could not connect to SAS session: " $_.Exception.Message&lt;BR /&gt;}&lt;BR /&gt;&lt;BR /&gt;# change these to your own SAS-session-based&lt;BR /&gt;# file path and file name&lt;BR /&gt;# Note that $destImg can't be &amp;gt; 7 chars&lt;BR /&gt;$destPath = "/servedir/aves"&lt;BR /&gt;$destFile= "Test"&lt;BR /&gt;&lt;BR /&gt;# local directory for downloaded file&lt;BR /&gt;$localPath = "\network\drive"&lt;BR /&gt;&lt;BR /&gt;# program to run&lt;BR /&gt;# could be read from external file&lt;BR /&gt;$program =  &lt;BR /&gt;       "Libname aves '/serverdir/aves';&lt;BR /&gt;           Filename csv '$destPath/$destFile.csv';&lt;BR /&gt;              Data _null_;&lt;BR /&gt;               Set aves.test;&lt;BR /&gt;               File csv dlm=',';&lt;BR /&gt;               Put (_all_) (+0);&lt;BR /&gt;               Run;&lt;BR /&gt;&lt;BR /&gt;# run the program&lt;BR /&gt;$objSAS.LanguageService.Submit($program);&lt;BR /&gt;&lt;BR /&gt;# flush the log - could redirect to external file&lt;BR /&gt;Write-Output "SAS LOG:"&lt;BR /&gt;$log = ""&lt;BR /&gt;do&lt;BR /&gt;{&lt;BR /&gt;  $log = $objSAS.LanguageService.FlushLog(1000)&lt;BR /&gt;  Write-Output $log&lt;BR /&gt;} while ($log.Length -gt 0)&lt;BR /&gt;&lt;BR /&gt;# now download the image file&lt;BR /&gt;$fileref = ""&lt;BR /&gt;&lt;BR /&gt;# assign a Fileref so we can use FileService from IOM&lt;BR /&gt;$objFile = $objSAS.FileService.AssignFileref(&lt;BR /&gt;     "img", "DISK", "$destPath/$destFile.csv", &lt;BR /&gt;     "", [ref] $fileref);&lt;BR /&gt;&lt;BR /&gt;$StreamOpenModeForReading = 1&lt;BR /&gt;$objStream = $objFile.OpenBinaryStream($StreamOpenModeForReading)&lt;BR /&gt;&lt;BR /&gt;# define an array of bytes&lt;BR /&gt;[Byte[]] $bytes = 0x0&lt;BR /&gt;&lt;BR /&gt;$endOfFile = $false&lt;BR /&gt;$byteCount = 0&lt;BR /&gt;$outStream = [System.IO.StreamWriter] "$localPath\$destFile.csv"&lt;BR /&gt;do&lt;BR /&gt;{&lt;BR /&gt;  # read bytes from source file, 1K at a time&lt;BR /&gt;  $objStream.Read(1024, [ref]$bytes)&lt;BR /&gt;  &lt;BR /&gt;  # write bytes to destination file&lt;BR /&gt;  $outStream.Write($bytes)&lt;BR /&gt;  # if less than requested bytes, we're at EOF&lt;BR /&gt;  $endOfFile = $bytes.Length -lt 1024&lt;BR /&gt;  &lt;BR /&gt;  # add to byte count for tally&lt;BR /&gt;  $byteCount = $byteCount + $bytes.Length&lt;BR /&gt;  &lt;BR /&gt;} while (-not $endOfFile)&lt;BR /&gt;&lt;BR /&gt;# close input and output files&lt;BR /&gt;$objStream.Close()&lt;BR /&gt;$outStream.Close()&lt;BR /&gt;&lt;BR /&gt;# free the SAS fileref&lt;BR /&gt;$objSAS.FileService.DeassignFileref($objFile.FilerefName)&lt;BR /&gt;&lt;BR /&gt;Write-Output "Downloaded $localPath\$destFile.csv:SIZE = $byteCount bytes" &lt;BR /&gt;&lt;BR /&gt;$objSAS.Close()</description>
    <pubDate>Tue, 30 Aug 2016 15:44:04 GMT</pubDate>
    <dc:creator>Aves9019</dc:creator>
    <dc:date>2016-08-30T15:44:04Z</dc:date>
    <item>
      <title>Powershell to SAS EG</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Powershell-to-SAS-EG/m-p/294217#M20088</link>
      <description>&lt;P&gt;Hi all,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; I have succesfully used the following program to invoke&amp;nbsp;remote based SAS EG in windows using powershell and retrieve the log and listing to the powershell console. However I would like to take this a step further and&amp;nbsp;create a delimited txt file to&amp;nbsp;save on my local or a network drive. Can I alter the following program to do so? I would like to alter the powershell comands as I am unable to simply include a proc export due to restrictions on my local and network drives.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;DIV&gt;$objFactory = New-Object -ComObject SASObjectManager.ObjectFactoryMulti2&lt;BR /&gt;$objServerDef = New-Object -ComObject SASObjectManager.ServerDef &lt;BR /&gt;$objServerDef.MachineDNSName = "server.mycompany.com" # SAS Workspace node&lt;BR /&gt;$objServerDef.Port&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; = 8591&amp;nbsp; # workspace server port&lt;BR /&gt;$objServerDef.Protocol&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; = 2&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # 2 = IOM protocol&lt;BR /&gt;# Class Identifier for SAS Workspace&lt;BR /&gt;$objServerDef.ClassIdentifier = "440196d4-90f0-11d0-9f41-00a024bb830c"&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;# create and connect to the SAS session &lt;BR /&gt;$objSAS = $objFactory.CreateObjectByServer(&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; "SASApp",&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # server name&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; $true, &lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; $objServerDef, # used server definition for Workspace&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; "sasdemo",&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # user ID&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; "Password1"&amp;nbsp;&amp;nbsp;&amp;nbsp; # password&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; )&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;$program = "proc sql; create table work.test as select* from perm.data; proc print data=work.test;run;"&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;# run the program&lt;BR /&gt;$objSAS.LanguageService.Submit($program);&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;# flush the log - could redirect to external file&lt;BR /&gt;Write-Output "LOG:"&lt;BR /&gt;$log = ""&lt;BR /&gt;do&lt;BR /&gt;{&lt;BR /&gt;&amp;nbsp; $log = $objSAS.LanguageService.FlushLog(1000)&lt;BR /&gt;&amp;nbsp; Write-Output $log&lt;BR /&gt;} while ($log.Length -gt 0)&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;# flush the output - could redirect to external file&lt;BR /&gt;Write-Output "Output:"&lt;BR /&gt;$list = ""&lt;BR /&gt;do&lt;BR /&gt;{&lt;BR /&gt;&amp;nbsp;$list = $objSAS.LanguageService.FlushList(1000)&lt;BR /&gt;&amp;nbsp;Write-Output $list&lt;BR /&gt;} while ($list.Length -gt 0)&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;# end the SAS session&lt;BR /&gt;$objSAS.Close()&lt;/DIV&gt;</description>
      <pubDate>Thu, 25 Aug 2016 22:53:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Powershell-to-SAS-EG/m-p/294217#M20088</guid>
      <dc:creator>Aves9019</dc:creator>
      <dc:date>2016-08-25T22:53:53Z</dc:date>
    </item>
    <item>
      <title>Re: Powershell to SAS EG</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Powershell-to-SAS-EG/m-p/294270#M20098</link>
      <description>I'm not really following you, especially try to clarify the big picture and what the end product would be.</description>
      <pubDate>Fri, 26 Aug 2016 06:22:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Powershell-to-SAS-EG/m-p/294270#M20098</guid>
      <dc:creator>LinusH</dc:creator>
      <dc:date>2016-08-26T06:22:06Z</dc:date>
    </item>
    <item>
      <title>Re: Powershell to SAS EG</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Powershell-to-SAS-EG/m-p/294347#M20109</link>
      <description>The powershell logic connects my local based Windows to my remote SAS EG. I am then able to run the small sas program you see in the logic in Windows and powershell produces the listing output and log. By using proc print I am able to see the resulting data of the SQL query in powershell. Instead I would like to create a txt file of this resulting SQL quey using powershell. I am unable to simply include a proc export or any additional SAS logic as my SAS EG cannot access my local drive. I need to add to the powershell logic to create a text file of the resulting data from the SQL query. My main goal here is to move data from SAS EG to a local path through invoking SAS EG in powershell.</description>
      <pubDate>Fri, 26 Aug 2016 11:21:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Powershell-to-SAS-EG/m-p/294347#M20109</guid>
      <dc:creator>Aves9019</dc:creator>
      <dc:date>2016-08-26T11:21:56Z</dc:date>
    </item>
    <item>
      <title>Re: Powershell to SAS EG</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Powershell-to-SAS-EG/m-p/294349#M20110</link>
      <description>&lt;P&gt;As far as I can read your code, you are not using a remote Enterprise Guide, but connecting directly to a Workspace Server on a remote SAS host.&lt;/P&gt;
&lt;P&gt;Since &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/4"&gt;@ChrisHemedinger﻿&lt;/a&gt; has created a custom task for Enterprise Guide to transfer any type of file using the IOM connection, I guess he could help you out with how to write that in Powershell.&lt;/P&gt;</description>
      <pubDate>Fri, 26 Aug 2016 11:28:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Powershell-to-SAS-EG/m-p/294349#M20110</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2016-08-26T11:28:55Z</dc:date>
    </item>
    <item>
      <title>Re: Powershell to SAS EG</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Powershell-to-SAS-EG/m-p/294350#M20111</link>
      <description>Thank you Kurt I'm fairly new to EG and didn't fully understand the distinction</description>
      <pubDate>Fri, 26 Aug 2016 11:39:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Powershell-to-SAS-EG/m-p/294350#M20111</guid>
      <dc:creator>Aves9019</dc:creator>
      <dc:date>2016-08-26T11:39:55Z</dc:date>
    </item>
    <item>
      <title>Re: Powershell to SAS EG</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Powershell-to-SAS-EG/m-p/294352#M20112</link>
      <description>&lt;P&gt;I think you're looking for the code &lt;A href="http://blogs.sas.com/content/sasdummy/2013/04/02/using-powershell-to-download-files/" target="_self"&gt;to download a file using PowerShell, via SAS Integration Technologies&lt;/A&gt;. &amp;nbsp;Read that blog post. &lt;A href="https://gist.github.com/cjdinger/5000087" target="_self"&gt;Here's the full code on GitHub&lt;/A&gt;.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Note that even though you want to download a CSV file (text), I recommend using the&amp;nbsp;&lt;STRONG&gt;binary&lt;/STRONG&gt; file stream method. &amp;nbsp;That will avoid transcoding issues that can occur with characters outside of your session encoding range. &amp;nbsp;If the remote system is UNIX, you might need to fix line-endings on the Windows side after the download (as UNIX and Windows use different conventions for line ending characters).&lt;/P&gt;</description>
      <pubDate>Fri, 26 Aug 2016 11:44:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Powershell-to-SAS-EG/m-p/294352#M20112</guid>
      <dc:creator>ChrisHemedinger</dc:creator>
      <dc:date>2016-08-26T11:44:13Z</dc:date>
    </item>
    <item>
      <title>Re: Powershell to SAS EG</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Powershell-to-SAS-EG/m-p/294353#M20113</link>
      <description>&lt;P&gt;Bingo!&lt;/P&gt;</description>
      <pubDate>Fri, 26 Aug 2016 11:48:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Powershell-to-SAS-EG/m-p/294353#M20113</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2016-08-26T11:48:19Z</dc:date>
    </item>
    <item>
      <title>Re: Powershell to SAS EG</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Powershell-to-SAS-EG/m-p/294360#M20114</link>
      <description>Thank you Chris I am working on UNIX. Would you happen to have an example or snippet of the binary file stream method as I am unfamiliar with this</description>
      <pubDate>Fri, 26 Aug 2016 12:15:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Powershell-to-SAS-EG/m-p/294360#M20114</guid>
      <dc:creator>Aves9019</dc:creator>
      <dc:date>2016-08-26T12:15:37Z</dc:date>
    </item>
    <item>
      <title>Re: Powershell to SAS EG</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Powershell-to-SAS-EG/m-p/294363#M20115</link>
      <description>&lt;P&gt;The code in my example should work to retrieve your file -- just download the CSV file instead of the PNG.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;To then convert the line endings in the CSV (which might not be important to you, depending what your next step is), some straight PowerShell code can help. &amp;nbsp;Here's an example I modified&amp;nbsp;from stackoverflow to convert all CSV files in a local directory.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;Get-ChildItem * -Include *.csv | ForEach-Object {
    ## If contains UNIX line endings, replace with Windows line endings
    if (Get-Content $_.FullName -Delimiter "`0" | Select-String "[^`r]`n")
    {
    	$content = Get-Content $_.FullName
    	$content | Set-Content $_.FullName
    }
}&lt;/PRE&gt;
&lt;P&gt;If you're just going to use the CSV file in Excel, I think you can skip this step. &amp;nbsp;But if you think people will open these CSV files in a local text viewer on Windows, it's probably a good idea to convert.&lt;/P&gt;</description>
      <pubDate>Fri, 26 Aug 2016 12:25:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Powershell-to-SAS-EG/m-p/294363#M20115</guid>
      <dc:creator>ChrisHemedinger</dc:creator>
      <dc:date>2016-08-26T12:25:28Z</dc:date>
    </item>
    <item>
      <title>Re: Powershell to SAS EG</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Powershell-to-SAS-EG/m-p/294371#M20118</link>
      <description>Thank you Chris this is great. I was able to create the file however the contents are a repetition of System.Byte[ ]System.Byte[ ] and my date is not present. In addition I can only open the file in read only mode and can seem how to kill the connection between powershell and excel to fix this. I attempted to export as pipe delimited text file which is what I would like to do but the file again only contained a repetition of System.Byte[ ]System.Byte[ ] . Thank you again for all of your help&lt;BR /&gt;</description>
      <pubDate>Fri, 26 Aug 2016 13:05:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Powershell-to-SAS-EG/m-p/294371#M20118</guid>
      <dc:creator>Aves9019</dc:creator>
      <dc:date>2016-08-26T13:05:00Z</dc:date>
    </item>
    <item>
      <title>Re: Powershell to SAS EG</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Powershell-to-SAS-EG/m-p/294405#M20119</link>
      <description>Just to clarify Chris am I replacing the Proc sgplot in the example with a proc export or is another method better suited</description>
      <pubDate>Fri, 26 Aug 2016 14:29:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Powershell-to-SAS-EG/m-p/294405#M20119</guid>
      <dc:creator>Aves9019</dc:creator>
      <dc:date>2016-08-26T14:29:32Z</dc:date>
    </item>
    <item>
      <title>Re: Powershell to SAS EG</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Powershell-to-SAS-EG/m-p/294564#M20128</link>
      <description>&lt;P&gt;If your objective is simply to write logs / text files / output from your SAS server session to a file share then I'd suggest it would be a lot easier just to do this directly from SAS to a server-accessible file share.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 27 Aug 2016 02:29:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Powershell-to-SAS-EG/m-p/294564#M20128</guid>
      <dc:creator>SASKiwi</dc:creator>
      <dc:date>2016-08-27T02:29:22Z</dc:date>
    </item>
    <item>
      <title>Re: Powershell to SAS EG</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Powershell-to-SAS-EG/m-p/295227#M20165</link>
      <description>Hi guys,&lt;BR /&gt;     I'm really struggling here. My file looks fine in UNIX but when I open it on my network drive it just contains "System.Byte[ ]". If someone could shed any light on why this may be that would be much appreciated. This is what I have so far  &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/4"&gt;@ChrisHemedinger&lt;/a&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;# Example of how to use PowerShell to script the&lt;BR /&gt;# SAS Integration Technologies client&lt;BR /&gt;# You can connect to a remote SAS Workspace&lt;BR /&gt;# and run a program, retrieve the SAS log and download a file&lt;BR /&gt;&lt;BR /&gt;# To use: change this script to reference your SAS Workspace&lt;BR /&gt;# node name, port (if different), and user credentials&lt;BR /&gt;&lt;BR /&gt;# create the Integration Technologies objects&lt;BR /&gt;$objFactory   = New-Object -ComObject SASObjectManager.ObjectFactoryMulti2&lt;BR /&gt;$objServerDef = New-Object -ComObject SASObjectManager.ServerDef&lt;BR /&gt;$objServerDef.MachineDNSName = "yournode.company.com" # SAS Workspace node&lt;BR /&gt;$objServerDef.Port           = 8591  # workspace server port&lt;BR /&gt;$objServerDef.Protocol       = 2     # 2 = IOM protocol&lt;BR /&gt;# Class Identifier for SAS Workspace&lt;BR /&gt;$objServerDef.ClassIdentifier = "440196d4-90f0-11d0-9f41-00a024bb830c"&lt;BR /&gt;&lt;BR /&gt;try&lt;BR /&gt;{&lt;BR /&gt;    # create and connect to the SAS session &lt;BR /&gt;    $objSAS = $objFactory.CreateObjectByServer(&lt;BR /&gt;                    "SASApp",      # server name&lt;BR /&gt;                    $true, &lt;BR /&gt;                    $objServerDef, # built server definition&lt;BR /&gt;                    "sasdemo",     # user ID&lt;BR /&gt;                    "Password1"    # password&lt;BR /&gt;                    )&lt;BR /&gt;}&lt;BR /&gt;catch [system.exception]&lt;BR /&gt;{&lt;BR /&gt;  Write-Host "Could not connect to SAS session: " $_.Exception.Message&lt;BR /&gt;}&lt;BR /&gt;&lt;BR /&gt;# change these to your own SAS-session-based&lt;BR /&gt;# file path and file name&lt;BR /&gt;# Note that $destImg can't be &amp;gt; 7 chars&lt;BR /&gt;$destPath = "/servedir/aves"&lt;BR /&gt;$destFile= "Test"&lt;BR /&gt;&lt;BR /&gt;# local directory for downloaded file&lt;BR /&gt;$localPath = "\network\drive"&lt;BR /&gt;&lt;BR /&gt;# program to run&lt;BR /&gt;# could be read from external file&lt;BR /&gt;$program =  &lt;BR /&gt;       "Libname aves '/serverdir/aves';&lt;BR /&gt;           Filename csv '$destPath/$destFile.csv';&lt;BR /&gt;              Data _null_;&lt;BR /&gt;               Set aves.test;&lt;BR /&gt;               File csv dlm=',';&lt;BR /&gt;               Put (_all_) (+0);&lt;BR /&gt;               Run;&lt;BR /&gt;&lt;BR /&gt;# run the program&lt;BR /&gt;$objSAS.LanguageService.Submit($program);&lt;BR /&gt;&lt;BR /&gt;# flush the log - could redirect to external file&lt;BR /&gt;Write-Output "SAS LOG:"&lt;BR /&gt;$log = ""&lt;BR /&gt;do&lt;BR /&gt;{&lt;BR /&gt;  $log = $objSAS.LanguageService.FlushLog(1000)&lt;BR /&gt;  Write-Output $log&lt;BR /&gt;} while ($log.Length -gt 0)&lt;BR /&gt;&lt;BR /&gt;# now download the image file&lt;BR /&gt;$fileref = ""&lt;BR /&gt;&lt;BR /&gt;# assign a Fileref so we can use FileService from IOM&lt;BR /&gt;$objFile = $objSAS.FileService.AssignFileref(&lt;BR /&gt;     "img", "DISK", "$destPath/$destFile.csv", &lt;BR /&gt;     "", [ref] $fileref);&lt;BR /&gt;&lt;BR /&gt;$StreamOpenModeForReading = 1&lt;BR /&gt;$objStream = $objFile.OpenBinaryStream($StreamOpenModeForReading)&lt;BR /&gt;&lt;BR /&gt;# define an array of bytes&lt;BR /&gt;[Byte[]] $bytes = 0x0&lt;BR /&gt;&lt;BR /&gt;$endOfFile = $false&lt;BR /&gt;$byteCount = 0&lt;BR /&gt;$outStream = [System.IO.StreamWriter] "$localPath\$destFile.csv"&lt;BR /&gt;do&lt;BR /&gt;{&lt;BR /&gt;  # read bytes from source file, 1K at a time&lt;BR /&gt;  $objStream.Read(1024, [ref]$bytes)&lt;BR /&gt;  &lt;BR /&gt;  # write bytes to destination file&lt;BR /&gt;  $outStream.Write($bytes)&lt;BR /&gt;  # if less than requested bytes, we're at EOF&lt;BR /&gt;  $endOfFile = $bytes.Length -lt 1024&lt;BR /&gt;  &lt;BR /&gt;  # add to byte count for tally&lt;BR /&gt;  $byteCount = $byteCount + $bytes.Length&lt;BR /&gt;  &lt;BR /&gt;} while (-not $endOfFile)&lt;BR /&gt;&lt;BR /&gt;# close input and output files&lt;BR /&gt;$objStream.Close()&lt;BR /&gt;$outStream.Close()&lt;BR /&gt;&lt;BR /&gt;# free the SAS fileref&lt;BR /&gt;$objSAS.FileService.DeassignFileref($objFile.FilerefName)&lt;BR /&gt;&lt;BR /&gt;Write-Output "Downloaded $localPath\$destFile.csv:SIZE = $byteCount bytes" &lt;BR /&gt;&lt;BR /&gt;$objSAS.Close()</description>
      <pubDate>Tue, 30 Aug 2016 15:44:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Powershell-to-SAS-EG/m-p/295227#M20165</guid>
      <dc:creator>Aves9019</dc:creator>
      <dc:date>2016-08-30T15:44:04Z</dc:date>
    </item>
    <item>
      <title>Re: Powershell to SAS EG</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Powershell-to-SAS-EG/m-p/295300#M20169</link>
      <description>&lt;P&gt;Something was amiss in that script, the way the binary stream was assembled for the target file. &amp;nbsp;Try this example -- instead of writing the destination file in pieces, this one assembles the bytes in memory and uses WriteAllBytes.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;# 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()&lt;/PRE&gt;
&lt;P&gt;Output for me:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;Downloaded c:\temp\class.csv: SIZE = 384 bytes&lt;/PRE&gt;</description>
      <pubDate>Tue, 30 Aug 2016 19:43:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Powershell-to-SAS-EG/m-p/295300#M20169</guid>
      <dc:creator>ChrisHemedinger</dc:creator>
      <dc:date>2016-08-30T19:43:30Z</dc:date>
    </item>
  </channel>
</rss>

