locked
Copy files from files share mount to local drive. RRS feed

  • Question

  • Hello,

    I have crated .bat scrip to pull the files from share mount to local drive. how to convert .bat to power shell for below script ?

    .Bat file 

    xcopy /y /f "\\11.123.14.69\gsetdata\logic\*.xls" "F:\test\"

    I want to implement same code in Powershell. Can someone help on this ?

    Thanks,

    R

    Friday, September 13, 2019 4:38 AM

All replies

  • That will run as it is in PowerShell. There is no need to convert it.  Just copy and paste into PowerShell and see it work.


    \_(ツ)_/

    Friday, September 13, 2019 6:08 AM
  • If you open PowerShell and type the following you will see that xcopy is a PowerShell command.

    Get-Command xcopy


    \_(ツ)_/

    Friday, September 13, 2019 6:09 AM
  • Hi,

    Was your issue resolved?

    If you resolved it using our solution, please "mark it as answer" to help other community members find the helpful reply quickly.

    If you resolve it using your own solution, please share your experience and solution here. It will be very beneficial for other community members who have similar questions.

    If no, please reply and tell us the current situation in order to provide further help.

    Best Regards,

    Lee


    Just do it.

    Friday, September 13, 2019 7:05 AM
  • HI 

    i tired this earlier but it was succeed but files not coped from file share to local drive. It seems some permission issue with my user. we have to provide login credentials while connecting to file share server. Something like below. 

    Note: Pull the files form 11.123.14.69 this file server (All *.xls files) to C:\test . Below script was not working. Can someone point me what was the issue ?

    #pre 5.0 powershell copy-item to remote computer
    Write-Host "Remote copy a file"
    $servers = @("11.123.14.69")
    foreach($server in $servers) {
    $username = 'test\USERFILESHARE'
    $password = 'xxxxx'
    $pw   = ConvertTo-SecureString $password -AsPlainText -Force
    $cred = New-Object Management.Automation.PSCredential ($username, $pw)
    $myfile = [System.IO.File]::ReadAllBytes("\11.123.14.69\gsetdata\logic\*.xls")
    $s = New-PSSession -computerName $server -credential $cred
    Enter-PSSession $s
    Invoke-Command -Session $s -ArgumentList $myfile -Scriptblock {[System.IO.File]::WriteAllBytes("C:\test", $args)}   
    Write-Host "Completed"
    Remove-PSSession $s
    }

    THanks

    R


    • Edited by satya RR Friday, September 13, 2019 4:10 PM
    Friday, September 13, 2019 4:10 PM
  • HI 

    i tired this earlier but it was succeed but files not coped from file share to local drive. It seems some permission issue with my user. we have to provide login credentials while connecting to file share server. Something like below. 

    Note: Pull the files form 11.123.14.69 this file server (All *.xls files) to C:\test . Below script was not working. Can someone point me what was the issue ?


    This has nothing to do with your original question.

    help New-PsDrive -online

    Read how to use the drive command.  Never place a password ina file.

    To run a script as another users start the script with "RunAs" and have the user provide the required credentials.

    Users should NEVER share credentials.  



    \_(ツ)_/

    Friday, September 13, 2019 5:26 PM
  • I have tried with run as an option as well but no luck still files are not copied from file share mount to local drive. I have used the following command in power shell.

     

    @ECHO OFF
    PowerShell.exe -NoProfile -Command "& {Start-Process PowerShell.exe -ArgumentList '-NoProfile -ExecutionPolicy Bypass -File ""%~dpn0.ps1""' -Verb RunAs}"
    PAUSE

    Note: Now i decided to pass username and password and check for testing whether it is working or not? Please suggest me whats wrong in below code.

    #pre 5.0 powershell copy-item to remote computer
    Write-Host "Remote copy a file"
    $servers = @("11.123.14.69")
    foreach($server in $servers) {
    $username = 'test\USERFILESHARE'
    $password = 'xxxxx'
    $pw   = ConvertTo-SecureString $password -AsPlainText -Force
    $cred = New-Object Management.Automation.PSCredential ($username, $pw)
    $myfile = [System.IO.File]::ReadAllBytes("\11.123.14.69\gsetdata\logic\*.xls")
    $s = New-PSSession -computerName $server -credential $cred
    Enter-PSSession $s
    Invoke-Command -Session $s -ArgumentList $myfile -Scriptblock {[System.IO.File]::WriteAllBytes("C:\test", $args)}   
    Write-Host "Completed"
    Remove-PSSession $s
    }

    Thanks,

    RR

    Saturday, September 14, 2019 10:53 AM
  • Sorry but there is np way t understand what you are trying to do.  YOu are just adding arbitrary lines of code over and over with no idea how any of it works.

    I suggest hat you take time to learn how to use basic Windows and PowerShell.  

    To copy file using xcopy just  start xcopy as a RunAs process.

     runas /user:testuser "xcopy /y /f \\11.123.14.69\gsetdata\logic\*.xls F:\test"

    Just enter the password when prompted.

    With PowerShell:

    New-PSDrive -Name remote -Root \\11.123.14.69\gsetdata\logic -PSProvider FileSystem -Credential domain\userid
    Copy-Item -Path remote:*.xls -Destination F:\test
    

    Open PowerShell and run that at the prompt.



    \_(ツ)_/

    Saturday, September 14, 2019 11:27 AM
  • New-PSDrive -Name remote -Root \\11.123.14.69\gsetdata\logic -PSProvider FileSystem -Credential domain\userid Copy-Item -Path remote:*.xls -Destination F:\test

    I believe above code will work for me. How to add password parameter int this code.

    I have tried to add below options in code but didn't work . Can you please help me on this ?

    $User = "test\USERFILESHARE"
    $File = "C:\test\Password.txt"
    $MyCredential=New-Object -TypeName System.Management.Automation.PSCredential `
     -ArgumentList $User, (Get-Content $File | ConvertTo-SecureString)


    Saturday, September 14, 2019 11:58 AM
  • New-PSDrive-Name remote -Root \\11.123.14.69\gsetdata\logic -PSProviderFileSystemCopy-Item-Path  remote:*.xls -Destination F:\test
    -Credential  $User = "test\USERFILESHARE" -Crendential.Passcode : $File = "C:\test\Password.txt" $MyCredential=New-Object -TypeName System.Management.Automation.PSCredential ` -ArgumentList $User, (Get-Content $File | ConvertTo-SecureString)
    • Edited by satya RR Saturday, September 14, 2019 5:21 PM
    Saturday, September 14, 2019 2:06 PM
  • Any one can help on below request . Below code is not working. Can some one help on this ?


    New-PSDrive-Name remote -Root \\11.123.14.69\gsetdata\logic -PSProviderFileSystemCopy-Item-Path  remote:*.xls -Destination F:\test
    -Credential  $User = "test\USERFILESHARE" -Crendential.Passcode : $File = "C:\test\Password.txt" $MyCredential=New-Object -TypeName System.Management.Automation.PSCredential ` -ArgumentList $User, (Get-Content $File | ConvertTo-SecureString)

    • Edited by satya RR Saturday, September 14, 2019 5:21 PM
    Saturday, September 14, 2019 5:19 PM