Error when copying file

The Goal

I am trying to create a script that will run either on a single server that I place as an argument, or have it pull from a list of servers via text file. For example:

.Splunk-ForwarderNewInstallFTI.ps1 Server1

.Splunk-ForwarderNewInstallFTI.ps1 .serverlist.txt

The Issue

When running, I get the error below:

PS S:ScriptsSplunkInstalls> .Splunk-ForwarderNewInstallFTI.ps1 .serverlist.txt Installing to SERVER1 PS S:ScriptsSplunkInstalls> .Splunk-ForwarderNewInstallFTI.ps1 server1 Installing to server1 Copy-Item : Cannot find path 'C:SplunkUniversalForwardersplunkforwarder-7.0.2-03bbabbd5c0f-x64-release.msi' because it does not exist. At S:ScriptsSplunkInstallsSplunk-ForwarderNewInstallFTI.ps1:60 char:1 + Copy-Item -Path "splunkforwarder-7.0.2-03bbabbd5c0f-x64-release.msi" ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : WriteError: (C:SplunkUniver...x64-release.msi:String) [Copy-Item], ArgumentException + FullyQualifiedErrorId : RemotePathNotFound,Microsoft.PowerShell.Commands.CopyItemCommand 

The script is in the same directory as the install file. For testing, server1 is in the text file as well.

Edit: The 1st one installs correctly. The 2nd on copies the directories, but not the file.

The Code

#Author: Sansred #New Install of FTI Splunk Forwarder #Version 1.2 R # This version is to give to other groups [CmdletBinding()] Param ([Parameter(Mandatory=$True,Position=0)] [String]$servers) # Determine and set script location for PowerShell $ScriptDir = Split-Path $script:MyInvocation.MyCommand.Path Set-Location $ScriptDir If ($servers -like '*txt'){ $serversList = Get-Content .serverlist.txt #go through each server in list foreach($server in $serversList){ $session = New-PSSession -ComputerName $server -Credential $cred Write-Host "Installing to $server" Copy-Item -Path "splunkforwarder-7.0.2-03bbabbd5c0f-x64-release.msi" -ToSession $session -Destination 'C:SplunkUniversalForwardersplunkforwarder-7.0.2-03bbabbd5c0f-x64-release.msi' -Recurse Copy-Item -Path ".etcapps" -ToSession $session -Destination 'C:SplunkUniversalForwarderetcapps' -Force -Recurse Copy-Item -Path ".etcauth" -ToSession $session -Destination 'C:SplunkUniversalForwarderetcauth' -Force -Recurse Invoke-Command -ScriptBlock { msiexec.exe /i C:SplunkUniversalForwardersplunkforwarder-7.0.2-03bbabbd5c0f-x64-release.msi INSTALLDIR="C:SplunkUniversalForwarder" LAUNCHSPLUNK=1 SERVICESTARTTYPE=auto CLONEPREP=0 AGREETOLICENSE=Yes } -ComputerName $server -Credential $cred } } else { foreach($server in $servers){ $session = New-PSSession -ComputerName $server -Credential $cred Write-Host "Installing to $server" Copy-Item -Path "splunkforwarder-7.0.2-03bbabbd5c0f-x64-release.msi" -ToSession $session -Destination 'C:SplunkUniversalForwardersplunkforwarder-7.0.2-03bbabbd5c0f-x64-release.msi' -Recurse Copy-Item -Path ".etcapps" -ToSession $session -Destination 'C:SplunkUniversalForwarderetcapps' -Force -Recurse Copy-Item -Path ".etcauth" -ToSession $session -Destination 'C:SplunkUniversalForwarderetcauth' -Force -Recurse Invoke-Command -ScriptBlock { msiexec.exe /i C:SplunkUniversalForwardersplunkforwarder-7.0.2-03bbabbd5c0f-x64-release.msi INSTALLDIR="C:SplunkUniversalForwarder" LAUNCHSPLUNK=1 SERVICESTARTTYPE=auto CLONEPREP=0 AGREETOLICENSE=Yes } -ComputerName $server -Credential $cred } } 

What is causing the error? Is there a better way to do this?

submitted by /u/Sansred
[link] [comments]

Leave a Reply