5 Replies Latest reply on Sep 18, 2015 12:55 PM by NeoIsTaken

    Copy file from a Network share

    Rookie

      trying to copy a file from a network share using a powershell script but when I schedules the task everything seems to worked but the file doesn't gets copy. Here is the code, any idea why? However, the script works when it run on my local computer (not using LANDesk)

       

      Function Copy-ItemUNC($SourcePath, $TargetPath, $FileName)

      {

        $fielexist = "C:\Windows\System32\myfile"

       

         New-PSDrive -Name source -PSProvider FileSystem -Root $SourcePath | Out-Null

         New-PSDrive -Name target -PSProvider FileSystem -Root $TargetPath | Out-Null

         if (Test-Path ($fielexist)) {

       

       

         remove-item -path $fielexist -force

       

       

         }

       

       

         Copy-Item -Path source:\$FileName -Destination target: -Force

         Remove-PSDrive source

         Remove-PSDrive target

      }

       

       

       

       

       

       

      $SourcePath = "\\myserver\Packages\DKL_Systems"

      $FileName = "myfile"

      $TargetPaht = "C:\Windows\System32"

       

       

       

       

       

       

      Copy-ItemUNC $SourcePath $TargetPaht $FileName

        • 1. Re: Copy file from a Network share
          Tom Farrugia SupportEmployee

          The problem here is the LANDESK agent runs under the Local System Account, which by default doesn't have access to the network share.  You might try to copy the files down as part of the package as "Additional Files" and then run the script locally on the device.  Alternatively, it might work if you were to give Domain Computers read access to the share.

          • 2. Re: Copy file from a Network share
            Rookie

            I added the file as "Additional file" path \\myserver\Packages\filname.ini

             

            so for example my code should be something like:

             

            $source = "\\myserver\Packages\filname.ini"

            $target = "C:\Windows\System32"

             

            copy-Item -path $source -Destination $target

            • 3. Re: Copy file from a Network share
              Rookie

              I tried this but it didn't worked, since I included the file in the distribution package what should my source path be?

               

              Function Copy-ItemUNC($SourcePath, $TargetPath)

              {

                $fielexist = "C:\Windows\System32\GroupPolicy\Machine\Scripts\psscripts.ini"

                

               

                 if (Test-Path ($fielexist)) {

               

               

                 remove-item -path $fielexist -force

               

               

                 }

               

               

                 Copy-Item -Path $SourcePath -Destination $TargetPaht -Force

               

              }

               

               

              $SourcePath = "\\SAURON.ern.nps.edu\packages\psscripts.ini"

               

               

              $TargetPaht = "C:\Windows\System32\GroupPolicy\Machine\Scripts\"

               

              Copy-ItemUNC $SourcePath $TargetPaht

              • 4. Re: Copy file from a Network share
                JeremyG Apprentice

                If you already included it as an "Additional File" when you do not need to specify the UNC path.    LANDesk will download the Powershell script to the SDMCache folder to execute it, but before it executes, it will also download the additional files into the same folder.  So it handles all network functions for you.

                 

                Because of this you should be able to just specify the file name with NO path as the source file! (since it's in the same folder as where the script is running from)

                 

                On your server make your two files like this:

                \\SAURON.ern.nps.edu\packages\PSPackage1\MyPSScript.ps1

                \\SAURON.ern.nps.edu\packages\PSPackage1\psscripts.ini

                 

                 

                Function Copy-ItemUNC($SourcePath, $TargetPath)
                {
                  $fielexist = "C:\Windows\System32\GroupPolicy\Machine\Scripts\psscripts.ini"
                  if (Test-Path ($fielexist))
                {
                  remove-item -path $fielexist -force
                }
                Copy-Item -Path $SourcePath -Destination $TargetPaht -Force
                }
                
                $SourcePath = "psscripts.ini"
                $TargetPaht = "C:\Windows\System32\GroupPolicy\Machine\Scripts\"
                
                Copy-ItemUNC $SourcePath $TargetPaht
                
                • 5. Re: Copy file from a Network share
                  Specialist

                  keep script and additional files in same distribution share (\\server\software\folder)

                   

                  They will both be located locally @ c:\progra~x\landesk\ldclient\sdmcache\software\folder

                   

                  $scriptPath = split-path -parent $MyInvocation.MyCommand.Definition

                   

                  copy-Item -path $scriptPath\filname.ini -Destination $target\filname.ini