locked
Copy an xml files RRS feed

  • Question

  • Hello,

    This script is driving me crazy, I cant unsesrtanad why this is happening, but I cannot copy an xml file to a path

    Line 134,

    Copy-Item"$RemoteTemplatePath\$Provider\XML\$RemoteXMLFile""$CustomUserTemplatePath\XML\$Provider"-Force

    so it supposed to copy a file from a share folder to a "AppData\Roaming\Microsoft\Templates\Template\XML\"

    But for some reason is not copying anything

    aby ideas?

    ###################################################
    ##                                               ##
    ## Custom Office Templates User Discovery Script ##
    ##                                               ##
    ###################################################
    
                                                                                     
    
    ########################
    ## USER-SET VARIABLES ##
    ########################
    
    # Set the remote template path.  This is a common location that is accessible to everyone. 
    $RemoteTemplatePath = "\\<FileServer>\remotefiles\OfficeTemplates\"
    
    # This is the root folder in the local template path where all the template providers and files will be created (eg, company name)
    $RootFolderName = "Template"
    
    # Office versions we will work with. This is used to determine the Office path in the HKCU registry
    $OfficeVersionKeys = @(
        "16.0", # Office 2016
        "15.0", # Office 2013
        "14.0" # Office 2010
    )
    
    
    
    
    ##########################
    ## SCRIPT-SET VARIABLES ##
    ##########################
    
    # Determine OS Architecture
    $OSArch = Get-WmiObject -Class win32_operatingsystem | select -ExpandProperty OSArchitecture
    
    # Get the provider names that exist in the remote location
    [array]$Providers = (Get-ChildItem $RemoteTemplatePath -Directory -ErrorAction Stop).Name
    
    # This is the path to the Templates directory in the user's profile
    $UserTemplatePath = "$env:APPDATA\Microsoft\Templates"
    
    # This is the path to the custom folder we are using to store our XML files in the user's profile
    $CustomUserTemplatePath = "$UserTemplatePath\$RootFolderName"
    
    # Determine which Office version/s we have installed
    [array]$InstalledOfficeKeys = $null
    $OfficeVersionKeys | Foreach {
        If (Test-Path "HKCU:\Software\Microsoft\Office\$_\Common\LanguageResources")
        {
            $InstalledOfficeKeys += $_
        }
    }
    
    # Set the program files directory by architecture
    If ($OSArch -eq "32-bit")
    {
        $Architecture = "x86"
    }
    If ($OSArch -eq "64-bit")
    {
        $Architecture = "x64"
    }
    
    
    
    
    ##################
    ## SCRIPT START ##
    ##################
    
    
    ##################################
    ## DIRECTORY CREATION / CLEANUP ##
    ##################################
    
    # Check that the custom user template path exists
    If (!(Test-Path "$CustomUserTemplatePath"))
    {
        New-Item -Path $UserTemplatePath -Name $RootFolderName -ItemType container -Force
    }
    
    # Check that the XML subfolder exists
    If (!(Test-Path "$CustomUserTemplatePath\XML"))
    {
        New-Item -Path $CustomUserTemplatePath -Name "XML" -ItemType container -Force
    }
    
    # Check that all providers exist locally under the XML directory
    [array]$LocalProviders = (Get-ChildItem "$CustomUserTemplatePath\XML" -Directory -ErrorAction Stop).Name
    $Providers | foreach {
        
        $Provider = $_
        If ($LocalProviders -notcontains $Provider)
        {
            New-Item -Path "$CustomUserTemplatePath\XML" -Name $Provider -ItemType container -Force
        }
    
    }
    
    # Check that no providers exist locally that do not exist remotely (cleanup)
    If ($LocalProviders.Count -ne 0)
    {
        $LocalProviders | foreach {
        
            $Provider = $_
            If ($Providers -notcontains $Provider)
            {
                Remove-Item -Path "$CustomUserTemplatePath\XML\$Provider" -Recurse -Force -Confirm:$false
            }
        
        }
    }
    
    
    
    #################################
    ## XML FILE CREATION / CLEANUP ##
    #################################
    
    # Check that XML files exist for each provider
    $Providers | foreach {
    
        $Provider = $_
        [array]$RemoteXMLFiles = (Get-ChildItem "$RemoteTemplatePath\$Provider\XML" -File -ErrorAction Stop).Name | where {$_ -match $Architecture} # create an array even though only 1 file should be returned as array is used for the cleanup
        [array]$LocalXMLFiles = (Get-ChildItem "$CustomUserTemplatePath\XML\$Provider" -File -ErrorAction SilentlyContinue).Name
    
        # Check that the XML files that exist remotely also exist locally
        $RemoteXMLFiles | foreach {
            
            $RemoteXMLFile = $_
            
            If ($LocalXMLFiles -notcontains $RemoteXMLFile)
            {
                Copy-Item "$RemoteTemplatePath\$Provider\XML\$RemoteXMLFile" "$CustomUserTemplatePath\XML\$Provider" -Force
            }
    
            # Regenerate the content branch as the XML file has been added
            $InstalledOfficeKeys | foreach {
    
                $OfficeVersionCode = $_
    
                If (Test-Path "HKCU:\Software\Microsoft\Office\$OfficeVersionCode\Common\Spotlight\Content")
                {
                    Remove-Item "HKCU:\Software\Microsoft\Office\$OfficeVersionCode\Common\Spotlight\Content" -Recurse -Force -Confirm:$false
                }
    
            }
        }
    
        # Check that the XML files that exist locally also exist remotely (cleanup)
        If ($LocalXMLFiles.Count -ne 0)
        {
            $LocalXMLFiles | foreach {
        
                $LocalXMLFile = $_
        
                If ($RemoteXMLFiles -notcontains $LocalXMLFile)
                {
                    Remove-Item -Path "$CustomUserTemplatePath\XML\$Provider\$LocalXMLFile" -Force -Confirm:$false
                }
            }
        }
    
        # Check that the hash values match
        $RemoteXMLFiles | foreach {
            $File = $_
            $RemoteHash = (Get-FileHash -Path "$RemoteTemplatePath\$Provider\XML\$File").Hash
            $LocalHash = (Get-FileHash -Path "$CustomUserTemplatePath\XML\$Provider\$File").Hash
    
            If ($RemoteHash -ne $LocalHash)
            {
                Copy-Item "$RemoteTemplatePath\$Provider\XML\$File" "$CustomUserTemplatePath\XML\$Provider" -Force
                
                # Regenerate the content branch as the XML file has changed
                $InstalledOfficeKeys | foreach {
    
                    $OfficeVersionCode = $_
    
                    If (Test-Path "HKCU:\Software\Microsoft\Office\$OfficeVersionCode\Common\Spotlight\Content")
                    {
                        Remove-Item "HKCU:\Software\Microsoft\Office\$OfficeVersionCode\Common\Spotlight\Content" -Recurse -Force -Confirm:$false
                    }
    
                }
            }
        }
    }
    
    
    
    
    #####################################
    ## REGISTRY KEY CREATION / CLEANUP ##
    #####################################
    
    
    # Loop through each installed Office version
    $InstalledOfficeKeys | foreach {
    
        $OfficeVersionCode = $_
        
        # Get the list of providers existing in the local registry
        [array]$LocalRegistryProviders = (Get-ChildItem "HKCU:\Software\Microsoft\Office\$OfficeVersionCode\Common\Spotlight\Providers" -ErrorAction SilentlyContinue).PSChildName
    
        # Loop through the providers
        $Providers | foreach {
            
            $Provider = $_
    
            # Check that each remote provider exists in the current user registry providers and create the ServiceURL key where necessary
            $RemoteXMLFile = (Get-ChildItem "$RemoteTemplatePath\$Provider\XML" -File -ErrorAction Stop).Name | where {$_ -match $Architecture}
            $ServiceURLDefinition = "$CustomUserTemplatePath\XML\$Provider\$RemoteXMLFile"
            If ($LocalRegistryProviders -notcontains $Provider)
            {
                New-Item -Path "HKCU:\Software\Microsoft\Office\$OfficeVersionCode\Common\Spotlight\Providers" -Name $Provider -Force
                New-ItemProperty -Path "HKCU:\Software\Microsoft\Office\$OfficeVersionCode\Common\Spotlight\Providers\$Provider" -Name ServiceURL -Value $ServiceURLDefinition -Force
                
                # Regenerate the content branch as a new XML has been added
                If (Test-Path "HKCU:\Software\Microsoft\Office\$OfficeVersionCode\Common\Spotlight\Content")
                {
                    Remove-Item "HKCU:\Software\Microsoft\Office\$OfficeVersionCode\Common\Spotlight\Content" -Recurse -Force -Confirm:$false
                }
            }
    
            # Check that the ServiceURL key is correct
            $ActualServiceURL = Get-ItemProperty -Path "HKCU:\Software\Microsoft\Office\$OfficeVersionCode\Common\Spotlight\Providers\$Provider" -Name ServiceURL -ErrorAction SilentlyContinue | Select -ExpandProperty ServiceURL
            If ($ActualServiceURL -ne $ServiceURLDefinition)
            {
                New-ItemProperty -Path "HKCU:\Software\Microsoft\Office\$OfficeVersionCode\Common\Spotlight\Providers\$Provider" -Name ServiceURL -Value $ServiceURLDefinition -Force
                
                # Regenerate the content branch as the XML file may be new
                If (Test-Path "HKCU:\Software\Microsoft\Office\$OfficeVersionCode\Common\Spotlight\Content")
                {
                    Remove-Item "HKCU:\Software\Microsoft\Office\$OfficeVersionCode\Common\Spotlight\Content" -Recurse -Force -Confirm:$false
                }
            }
        }
    
        # Build the list of providers existing in the local registry again since changes may just have been made
        [array]$LocalRegistryProviders = (Get-ChildItem "HKCU:\Software\Microsoft\Office\$OfficeVersionCode\Common\Spotlight\Providers" -ErrorAction SilentlyContinue).PSChildName
    
        # Loop through the local providers in the registry
        $LocalRegistryProviders | foreach {
    
            $Provider = $_
            
            # Check that each local registry provider exists in the remote provider list (cleanup)
            If ($Providers -notcontains $Provider)
            {
                Remove-Item -Path "HKCU:\Software\Microsoft\Office\$OfficeVersionCode\Common\Spotlight\Providers\$Provider" -Recurse -Force -Confirm:$false
                
                # Regenerate the content branch as a provider has been removed
                If (Test-Path "HKCU:\Software\Microsoft\Office\$OfficeVersionCode\Common\Spotlight\Content")
                {
                    Remove-Item "HKCU:\Software\Microsoft\Office\$OfficeVersionCode\Common\Spotlight\Content" -Recurse -Force -Confirm:$false
                }
            }
    
        }
        
    }
    


    lmelm

    Saturday, July 22, 2017 8:02 PM

All replies

  • It is clear that you path is wrong.  Some element is likely null so the file is being copied to the wrong place.  If tis was not the case you would get an error.

    Output the path or add "-Verbose" to the command to see what is being copied.

    We cannot debug your code for you.

    When posting code to a technical forum you must post using the code posting tool. You should also reduce the code to the smallest amount of code that demonstrates your issue.  If you cannot do this or don't know how then you will have to contact a consultant or an experienced tech in your company.


    \_(ツ)_/

    Saturday, July 22, 2017 8:10 PM
  • Hi,
    Just checking in to see if the information provided was helpful. Please let us know if you would like further assistance.

    Best Regards,

    Frank

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

    Friday, August 11, 2017 4:43 AM