/build/static/layout/Breadcrumb_cap_w.png

Scripting Question


Powershell Copy Item

09/12/2019 2144 views

$RootDir = $PSScriptRoot




#wmic product where "name like 'Java 7%%' or name like 'Java 8%%'" call uninstall /nointeractive


# Install Java


Write-Host "Installing Java..."

Start-Process -FilePath $(Join-Path -Path $PSScriptRoot -ChildPath "jre1.8.0_161.msi") -ArgumentList "-qn" -Wait

sleep 5


# disable autoupdate

Write-Host "Disabling Java Update..."

reg delete "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run" /v SunJavaUpdateSched /f

reg add "HKEY_LOCAL_MACHINE\Software\JavaSoft\Java Update\Policy" /v EnableJavaUpdate /t REG_DWORD /d 0 /f

sleep 5


# Copy File

cd $RootD

Write-Host "Copying Files.."

Copy-Item -Path $RootDir\Deployment\*.* -Destination "$env:Userprofile\Appdata\LocalLow\Sun\Java\Deployment" -Force -Recurse

My code above works fine if i am running it from the powershell from the folder thats on the desktop. I have packaged it in the SCCM and calling the powershell via batch file. Half of the script works. It installs the Java, however it won't copy the file. If i go to C:\Windows\ccmcache\5 and run just the line from the powershell Copy-Item -Path $RootDir\Deployment\*.* -Destination "$env:Userprofile\Appdata\LocalLow\Sun\Java\Deployment" -Force -Recurse i get an error Cannot find path 'C:\Deployment' because it does not exist.

This leads me to beilve that last part of my script does not work when running as a SCCM package from C:\Windows\ccmcache\(whatever the folder it assigns it to)

How can i make copy portion to work from that folder?


My Folder Structure.




1 Comment   [ + ] Show comment

Comments

  • if your package is running as system the Userprofile\Appdata\LocalLow\Sun\Java\Deployment does not exist


Community Chosen Answer

3

+1 for SMal.tmcc's comment - even if it copy the file, You won't find it in the users profile. It will be copied to the SYSTEM account profile folder (C:\WINDOWS\system32\config\systemprofile\).

Additionally you mentioned:

If i go to C:\Windows\ccmcache\5 and run just the line from the powershell Copy-Item -Path $RootDir\Deployment\*.* -Destination "$env:Userprofile\Appdata\LocalLow\Sun\Java\Deployment" -Force -Recurse i get an error Cannot find path 'C:\Deployment' because it does not exist.

How did you run this line? I'm guessing that from the ISE or PS console as it did not resolve the $PSScriptRoot variable which works only in a script context (you need to start a ps1 script to get it's value resolved properly).


Check the C:\WINDOWS\system32\config\systemprofile\Appdata\LocalLow\Sun\Java\Deploymentfolde to see if file was copied there when deployed throught SCCM.

Answered 09/13/2019 by: rad33k
Fourth Degree Brown Belt

  • I ended up coding to where powershell should copy the file from network share to $env:Userprofile\Appdata\LocalLow\Sun\Java\Deployment.
    again, it works if i ran the command from the sccm cach folder, but it will not copy anything during the application installation from Software Center. I don't understand why its not doing what it should
  • SCCM Agent uses a Local SYSTEM account to execute commands defined in the SCCM console and that's why $env:Userprofile does not work as you are expecting. You should test your script using PSEXEC (https://docs.microsoft.com/en-us/sysinternals/downloads/psexec)

    PSEXEC -si Install.bat

    Then you will see how the PS script works when deployed through SCCM.

    One of the approaches, to handle such issues is to copy the files to each user profiles:
    https://www.pdq.com/blog/copying-files-to-all-user-profiles/

    Another option is to perform the file copy action using Active Setup:
    https://helgeklein.com/blog/2010/04/active-setup-explained/

All Answers

0

I agree with the already mentioned "running as SYSTEM" fact. I have started to use two applications. First  is set to  Install for System and runs all action the user has no rights to.

First application is then used as dependency for second application, that is set to Install for user and runs all user profile, registry and other related stuff.

Answered 01/08/2020 by: Astorian
White Belt

 
This website uses cookies. By continuing to use this site and/or clicking the "Accept" button you are providing consent Quest Software and its affiliates do NOT sell the Personal Data you provide to us either when you register on our websites or when you do business with us. For more information about our Privacy Policy and our data protection efforts, please visit GDPR-HQ