Powershell .NET Set permissions and Preserve times options

Advertisement

jesc516
Joined:
Posts:
5

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.

Reply with quote

Advertisement

jesc516
Joined:
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?


    
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)
        }
        
    }

Reply with quote

martin
Site Admin
martin avatar
Joined:
Posts:
29,266
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

Reply with quote

Advertisement

You can post new topics in this forum