locked
copying file between remote machines RRS feed

  • Question

  • Hello

    I need to copy install files from remote network mapped drive (source) to remote servers(target),install software on target servers and restart windows services that are running on those machines.I am having trouble with copying file.I tested the following script by replacing copy-item command with get-content.My credentials pass through just fine and gets me content but it has issue with copying files.

    foreach ($Target in Get-Content -path "\\$source\servers.txt")
    #{
    $source = "//mapped drive/"
    $target = "computer B"
    $secpasswd = ConvertTo-SecureString $test_passwd -AsPlainText -Force
    $mycreds = New-Object System.Management.Automation.PSCredential ($test_account, $secpasswd)
    Invoke-Command -Computername $source -Credential $mycreds -scriptblock{
        Copy-Item -path "$source\scripts\.exe file"  -Destination "c:\$target\temp" -force        
        Invoke-Command -Computername $target -Credential $Using:mycreds -ScriptBlock {stop service -"abc"}  

    Sunday, October 27, 2019 7:54 PM

Answers

  • Here is an example that may help you understand what you don't know.

    Get-Content $source\servers.txt |
         ForEach-Object{
             Get-CimInstance Win32_service -Filter "Name='SAPHostControl'" -ComputerName $_
        }


    \_(ツ)_/

    • Marked as answer by mbijja Tuesday, November 12, 2019 12:25 AM
    Tuesday, November 5, 2019 2:02 AM

All replies

  • You have to copy the file from the current session. Also you do not need to remote to start a service.

    Copy cannot remotely access a network share form a remote session.  It is restricted.

    Get-CimInstance Win32_service -Filter "Name='spooler'" -ComputerName $computer -Credential $cred |
        Invoke-CimMethod -MethodName StopService


    \_(ツ)_/


    Sunday, October 27, 2019 8:16 PM
  • Please read and fix your original post: How to post code in Technet Forums

    \_(ツ)_/

    Sunday, October 27, 2019 8:17 PM
  • Here is how to do this correctly.

    $source = '\\computer\share'
    $secpasswd = ConvertTo-SecureString $test_passwd -AsPlainText -Force
    $mycreds = [System.Management.Automation.PSCredential]::New($test_account, $secpasswd)
            
    Get-Content -path $source\servers.txt |
        ForEach-Object{
            $s = New-PSSession $_ -Credential $cred
            Get-CimInstance Win32_service -Filter "Name='spooler'" -CimSession $s -Credential $cred |
                Invoke-CimMethod -MethodName StopService
            Copy-Item -path "$source\scripts\file.exe" -Destination c:\exe\temp -ToSession $s
            # install file
            Get-CimInstance Win32_service -Filter "Name='spooler'" -CimSession $s -Credential $cred |
                Invoke-CimMethod -MethodName StartService
        }
    

    Before going any further you will need to learn basic PowerShell.


    \_(ツ)_/

    Sunday, October 27, 2019 8:33 PM
  • Appreciate your replies-I have just started to code.

    I am seeing  errors with this line both for starting and stopping. Also i need to add a command to close the session right,at the end of the code?

    Get-CimInstance Win32_service -Filter "Name='spooler'" -CimSession $s -Credential $cred |
                Invoke-CimMethod -MethodName StopService

    Thanks

    mbijja

    Tuesday, October 29, 2019 7:32 AM
  • Without the error message there is no way to know what your issue is.

    You need to take some time to learn basic PowerShell and the basics of coding.  Without the minimums you will be forever lost.

    The code I posted was just an example to point you in the right direction.  The code has been tested.  You still need to understand Windows technology. PS and coding are not for end users without any technical training in Windows and Windows networking. 

    Start by carefully reading the error message as most errors tell you exactly what is wrong.

     


    \_(ツ)_/

    Tuesday, October 29, 2019 8:01 AM
  • Just to be clear - I am not trying to put you off. Read the error and. If you don't understand it then post the complete error and we will try to help you understand it.

    I noted the difficulties of working with PS without sufficient background to let you know that helping you will be difficult if you have no basic understanding of PowerShell.  That is because you won't know what information is needed and, as with not posting the error, you will not know what information to give us.

    Here is a good post that may be of some help: How to ask questions in a technical forum


    \_(ツ)_/

    Tuesday, October 29, 2019 9:12 AM
  • OK!!Appreciate your help but please know that I wouldn't be posting on forums if i was a pro!!I need help as i am new and learning so relying on forums.

    Here is what i have modified to stop/start services but I get error when its copying.

    We were calling PS session in the script you sent above--Cimsession was failing so i had to change it New-CimSession and assign to our vaiable $s.That worked for starting and stopping but copying fails.I think copy-item syntax recognizes either -computer or -session params not -Cimsession.I am stuck here :(

    Get-CimInstance Win32_service -Filter "Name='spooler'" -CimSession $s -Credential $cred |
                Invoke-CimMethod -MethodName StopService
            

    $source = '\\ussddevlv01\c$\scripts'
    $secpasswd = ConvertTo-SecureString $test_passwd -AsPlainText -Force
    $mycreds = [System.Management.Automation.PSCredential]::New($test_account, $secpasswd)

    Get-Content -path $source\servers.txt |
        ForEach-Object{
            $s = New-CimSession $_ -Credential $mycreds
            Get-CimInstance -Classname Win32_service -Filter "Name='SAPHostControl'" -CimSession $s  |
                Invoke-CimMethod -MethodName StopService
            Copy-Item -path "$source\SAPHOSTAGENT40_40-20009417.SAR" -Destination c:\temp -ToSession $s
            #Write-Host "File Copied"
            # install file
            #Get-CimInstance -Classname Win32_service -Filter "Name='SAPHostControl'" -CimSession $s  |
                #Invoke-CimMethod -Name StartService
        }

    Here is the ERROR

     Copy-Item : A parameter cannot be found that matches parameter name 'CimSession'.

    At C:\ILMN-LAMA\HostAgentUpgrade.ps1:16 char:87
    + ... e\SAPHOSTAGENT40_40-20009417.SAR" -Destination c:\temp -CimSession $s
    +                                                            ~~~~~~~~~~~
        + CategoryInfo          : InvalidArgument: (:) [Copy-Item], ParameterBindingException
        + FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.Commands.CopyItemCommand

    Tuesday, November 5, 2019 1:51 AM
  • Without taking time to learn basic PowerShell y9ou will not be able to ask a clear question. YOu will also not be abkle to understand the answers. Stop trying to guess and reword you issue and spend this time learning PowerShell.

    Also take time to read all of the help for each CmdLet you want to use.  You will need to learn basic PS to understand the help and you will need good basic technical knowledge of Windows.

    Example:

    help foreach-object -online

    You will have to learn the fundamentals on your own.  Technical forums are for technicians and are not a place to get personal instruction.  The forums can answer specific questions that you have about a script but we cannot rewrite your scripts fo you.


    \_(ツ)_/

    Tuesday, November 5, 2019 1:58 AM
  • Here is an example that may help you understand what you don't know.

    Get-Content $source\servers.txt |
         ForEach-Object{
             Get-CimInstance Win32_service -Filter "Name='SAPHostControl'" -ComputerName $_
        }


    \_(ツ)_/

    • Marked as answer by mbijja Tuesday, November 12, 2019 12:25 AM
    Tuesday, November 5, 2019 2:02 AM
  • Did you forget to make this like a comment?

        "Name='SAPHostControl'" -CimSession $s  |

    If so, put a "#" at the beginning of the line.


    --- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)

    Tuesday, November 5, 2019 3:20 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

    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    Friday, November 8, 2019 1:07 PM