Author Posts

March 4, 2017 at 4:33 pm

Trying to broaden my skills and exploring with my first script and running into problems. I've kind of researched and put together the following. My goal is from a .txt list of servers to copy a folder from my local c:\ to a specified folder on the destination server. next install the .msi, copy another folder from a source server to the target and finally restart the new service that was installed.
if I enter the variables manually line by line then run the commands it is successful but if I run the .ps1 all together it gives me a null error indicating it's not carrying the variables to the remote session. I've tried to read about params but for one reason or another it's not clicking so looking for some help. I'm open to any other constructive criticism on the code as well 🙂 Thanks in advance.

#defines the list of destination computers
$computers = Get-Content "c:\scripts\computers.txt"

#defines the ps session to the dest server
$session = New-PSSession -ComputerName $comp

#defines the splunk uf source folder on your computer
$source = "c:\splunkuf"
$sourcespl = "\\server\e$\program files\splunk\etc\deployment-apps\all_deploymentclients"

#defines the destination source where the splunk msi will be copied
$dest = "\\$comp\c$"
$destspl = "\\$comp\c$\program files\SplunkUniversalForwarder\etc\apps"

#defines the deployment command after the msi is copied
$deploy = { & msiexec.exe /I c:\splunkuf\splunkforwarder-6.5.2-67571ef4b87d-x64-release.msi AGREETOLICENSE=Yes /quiet}

foreach ($comp in $computers){
#copies from the splunk msi folder source to the target server c: drive
Copy-Item $source $dest -Recurse -Force
Start-Sleep -s 3

#remote executes the splunk msi on the target server
Invoke-Command -Session $session -ScriptBlock $deploy
Start-Sleep -s 20

#copies the master deployment folder from spl-mstr-prod to the target server
Copy-Item $sourcespl $destspl -Recurse -Force
Start-Sleep -s 10

#restarts the splunk forwarder service on the target server
Invoke-Command -Session $session -ScriptBlock {Restart-Service SplunkForwarder -force}
}

March 4, 2017 at 5:07 pm

Hey Chris,
It looks like you are just setting your variables out of order.

Here you are setting your $dest variables using the $comp variable:

#defines the destination source where the splunk msi will be copied
$dest = "\\$comp\c$"
$destspl = "\\$comp\c$\program files\SplunkUniversalForwarder\etc\apps"

but that is done before you set the $comp variable with a value here later in the script:

foreach ($comp in $computers){

as a result, while you expect $dest and $destspl to contain values such as:

\\server1\c$
\\server1\c$\program files\SplunkUniversalForwarder\etc\apps

they in fact only contain:

\\\c$
\\\c$\program files\SplunkUniversalForwarder\etc\apps

It is likely the reason that this works when you run manually is because you are using the ISE and the $comp variable is already defined from a previous run of the script while you were testing.

To resolve you should move the assignment of your $dest and $destspl variables inside of your foreach loop

foreach ($comp in $computers){
    $dest = "\\$comp\c$"
    $destspl = "\\$comp\c$\program files\SplunkUniversalForwarder\etc\apps"

March 4, 2017 at 5:20 pm

The $session and $dest definitions should go inside the foreach loop. Also add line to dispose of the $session:


#defines the list of destination computers
$computers = Get-Content "c:\scripts\computers.txt"

#defines the splunk uf source folder on your computer
$source = "c:\splunkuf"
$sourcespl = "\\server\e$\program files\splunk\etc\deployment-apps\all_deploymentclients"

#defines the deployment command after the msi is copied
$deploy = { & msiexec.exe /I c:\splunkuf\splunkforwarder-6.5.2-67571ef4b87d-x64-release.msi AGREETOLICENSE=Yes /quiet}

foreach ($comp in $computers){

    #defines the ps session to the dest server
    $session = New-PSSession -ComputerName $comp

    #defines the destination source where the splunk msi will be copied
    $dest = "\\$comp\c$"
    $destspl = "\\$comp\c$\program files\SplunkUniversalForwarder\etc\apps"    
    
    #copies from the splunk msi folder source to the target server c: drive
    Copy-Item $source $dest -Recurse -Force
    Start-Sleep -s 3

    #remote executes the splunk msi on the target server
    Invoke-Command -Session $session -ScriptBlock $deploy
    Start-Sleep -s 20

    #copies the master deployment folder from spl-mstr-prod to the target server
    Copy-Item $sourcespl $destspl -Recurse -Force
    Start-Sleep -s 10

    #restarts the splunk forwarder service on the target server
    Invoke-Command -Session $session -ScriptBlock {Restart-Service SplunkForwarder -force}

    # dispose of the PS session
    $session | Remove-PSSession
}

March 4, 2017 at 5:26 pm

Ah, I missed $session with the same issue. Good catch Sam.

March 6, 2017 at 5:12 pm

awesome! I wasn't too far off then. Thank you both!