Post a reply

Options
Add an Attachment

If you do not want to add an Attachment to your Post, please leave the Fields blank.

(maximum 10 MB; please compress large files; only common media, archive, text and programming file formats are allowed)

Options

Topic review

jesc516

Re: Powershell .NET Set permissions and Preserve times options

martin wrote:

It's:
$transferOptions.FilePermissions = $Null # This is default

$transferOptions.PreserveTimestamp = $False

I have updated the article with PowerShell example:
https://winscp.net/eng/docs/message_preserve_time_perm


This worked with no errors. thank you prikryl
martin

AFAIK, the $False is not a variable, it's a constant, so no issue. And it would not be a problem even if it were a variable, as you are reading it only.
jesc516

thank you sir. i will do a production test next week. i only access that specific server once a week and the file is immediately moved from when the file is uploaded.

i have a question, will this cause an issue? using the $False variable twice within the same try statement?


    

try
    {
        $session.Open($sessionOptions)
        $transferOptions = New-Object WinSCP.TransferOptions
        $transferOptions.TransferMode = [WinSCP.TransferMode]::Binary
        $transferOptions.FilePermissions = $Null
        $transferOptions.PreserveTimestamp = $False ## First $False variable

        $remote_dir = ("/REMOTE/PATH/")
        $local_dir = ("C:\LOCAL\PATH\")

        $date = ((Get-Date).ToString("MMddyy"))
        $flist = ("FILE" + $date + ".zip"), ("FILE2" + $date + ".zip")

        foreach ($fn in $flist)
        {
            $transferResult = $session.PutFiles(($local_dir + $fn), ($remote_dir), $False, $transferOptions) ## SECOND $False variable
            $transferResult.Check()
            Write-Host ("Uploaded... " + $fn)
        }
       
    }

martin

Re: Powershell .NET Set permissions and Preserve times options

It's:
$transferOptions.FilePermissions = $Null # This is default

$transferOptions.PreserveTimestamp = $False

I have updated the article with PowerShell example:
https://winscp.net/eng/docs/message_preserve_time_perm
jesc516

Powershell .NET Set permissions and Preserve times options

How can i set the following options as per the .NET assembly instructions in powershell?


When using .NET assembly, set TransferOptions as follows:
TransferOptions transferOptions = new TransferOptions();

...
transferOptions.FilePermissions = null; // This is default
transferOptions.PreserveTimestamp = false;


when i attempt to use "null" or "false" in powershell i get the following errors:

The term 'null' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.


The term 'false' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.


i was able to get the script to run using:

$transferOptions.FilePermissions = Out-Null

$transferOptions.PreserveTimestamp = Out-Null


not sure if this is the correct way of doing the above. any help is greatly appreciated.