Topic "Powershell .NET Set permissions and Preserve times options"

Author Message
jesc516
[View user's profile]

Joined: 2014-12-09
Posts: 5
How can i set the following options as per the .NET assembly instructions in powershell?


When using .NET assembly, set TransferOptions as follows:
Code:

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:

Code:
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:

Code:

$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.
Advertisements
martin
[View user's profile]
Site Admin
Joined: 2002-12-10
Posts: 24530
Location: Prague, Czechia
It's:
Code:
$transferOptions.FilePermissions = $Null # This is default
$transferOptions.PreserveTimestamp = $False

I have updated the article with PowerShell example:
http://winscp.net/eng/docs/message_preserve_time_perm
_________________
Martin Prikryl
jesc516
[View user's profile]

Joined: 2014-12-09
Posts: 5
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?


Code:
   
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
[View user's profile]
Site Admin
Joined: 2002-12-10
Posts: 24530
Location: Prague, Czechia
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.
_________________
Martin Prikryl
jesc516
[View user's profile]

Joined: 2014-12-09
Posts: 5
prikryl wrote:
It's:
Code:
$transferOptions.FilePermissions = $Null # This is default
$transferOptions.PreserveTimestamp = $False

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


This worked with no errors. thank you prikryl
Advertisements

You can post new topics in this forum






Search Site

What is WinSCP?

It is award-winning SFTP client, SCP client, FTPS client and FTP client integrated into one software program for file transfer to FTP server or secure SFTP server. [More]

And it's free!

Donate

About donations

$9   $19   $49   $99

About donations

Recommend

WinSCP Privacy Policy

WinSCP License