VMware Cloud Community
antoniogemelli
Hot Shot
Hot Shot

scp with powershell - set file permissions

Hello guys,

I have a script for Putty who sending everyday some report from a Win directory to Linux server with scp

Not sure if I can manage directly with this script, I would like to set some permissions on files before I sent,

The Linux directory have some restrictions, directory 775 (drwxrwxr-x)  but file became from Win server with  (640 -rw-r-----)

This is script:

$file = Get-ChildItem -Path C:\temp\smart_backup\*.csv |

  Sort-Object -Property LastWriteTime -Descending |

  select -First 1 -ExpandProperty FullName

$sProc = @{

  FilePath = 'C:\Program Files (x86)\PuTTY\pscp.exe'

  ArgumentList = "-scp -pw ******* $file smart@1.1.1.1:/data/smart/invoicing/"

}

Start-Process @sProc

Any suggestion or I should find a way in Linux server directly?

Thanks

1 Reply
LucD
Leadership
Leadership

I owuld encourage you to have a go with the Posh-Ssh module.
Makes SSH a lot easier.

Changing permissions can be done like this

$esxName = 'myesx.my.domain'

$cred = Get-Credential -Message 'Enter credentials'

$cmdsub = @'

chmod 644 /tmp/test.file

'@


$esx = Get-VMHost -Name $esxName

$session = New-SSHSession -ComputerName $esx.Name -Credential $cred –AcceptKey

$result = Invoke-SSHCommand -SSHSession $session -Command $cmdSub

Remove-SSHSession -SSHSession $session | Out-Null


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference