Help using Foreach loop with New-PSSession

Hello,
I am trying to get this working in my lab environment, I am getting access denied messages. Its supposed to traverse the c:\tools\server.txt file, and install minecraftinstaller.msi on them all.

Clear-Host
$domainuser = "Basement\administrator"
$domainpassword = 'Password1'| ConvertTo-SecureString -AsPlainText -Force
$domaincredentials = New-Object System.Management.Automation.PSCredential ($domainuser, $domainpassword)
$computers = Get-content 'C:\tools\server.txt'
$Process = 'C:\tools\minecraftinstaller.msi'

Foreach($computer in $computers)
{
Invoke-Command -ComputerName $computer -ScriptBlock {
$Session = New-PSSession -ComputerName $env:COMPUTERNAME
Copy-Item -Path 'C:\tools\minecraftinstaller.msi' -Destination "\\$($Computer)\C$\tools" -ToSession $Session
Start-Process -FilePath $Process -Wait
write-host -ForegroundColor Magenta $env:COMPUTERNAME
													}
}

I believe I should have access as I am running the PS windows with a Domain Admin account, and they are all domain members.

Any ideas?

Here is the error output, I cut it off after server1:

> [SERVER1] Connecting to remote server SERVER1 failed with the following error message : Access is denied. For more
> information, see the about_Remote_Troubleshooting Help topic.
>     + CategoryInfo          : OpenError: (System.Manageme....RemoteRunspace:RemoteRunspace) [New-PSSession], PSRemotin
>    gTransportException
>     + FullyQualifiedErrorId : AccessDenied,PSSessionOpenFailed
>     + PSComputerName        : server1
> Cannot validate argument on parameter 'ToSession'. The argument is null or empty. Provide an argument that is not null
> or empty, and then try the command again.
>     + CategoryInfo          : InvalidData: (:) [Copy-Item], ParameterBindingValidationException
>     + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.CopyItemCommand
>     + PSComputerName        : server1
> 
> Cannot validate argument on parameter 'FilePath'. The argument is null or empty. Provide an argument that is not null
> or empty, and then try the command again.
>     + CategoryInfo          : InvalidData: (:) [Start-Process], ParameterBindingValidationException
>     + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.StartProcessCommand
>     + PSComputerName        : server1
> 
> SERVER1
> [SERVER2] Connecting to remote server SERVER2 failed with the following error message : Access is denied. For more
> information, see the about_Remote_Troubleshooting Help topic.
>     + CategoryInfo          : OpenError: (System.Manageme....RemoteRunspace:RemoteRunspace) [New-PSSession], PSRemotin
>    gTransportException
>     + FullyQualifiedErrorId : AccessDenied,PSSessionOpenFailed
>     + PSComputerName        : server2
> Cannot validate argument on parameter 'ToSession'. The argument is null or empty. Provide an argument that is not null
> or empty, and then try the command again.
>     + CategoryInfo          : InvalidData: (:) [Copy-Item], ParameterBindingValidationException
>     + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.CopyItemCommand
>     + PSComputerName        : server2
> 
> Cannot validate argument on parameter 'FilePath'. The argument is null or empty. Provide an argument that is not null
> or empty, and then try the command again.
>     + CategoryInfo          : InvalidData: (:) [Start-Process], ParameterBindingValidationException
>     + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.StartProcessCommand
>     + PSComputerName        : server2
> 
> SERVER2

I got this working using 2 scripts. The 1st drops the payload to the remote, and the second executes the payload. Both are called from MinecraftInstallerInput.ps1

#MinecraftInstallerInput.ps1
Clear-Host
$computers = Get-content 'C:\tools\server.txt'
$FilePath = 'C:\tools\Minecraftinstaller.ps1'
Foreach($computer in $computers)
{
	Copy-Item c:\tools\Minecraftinstaller.msi "\\$($Computer)\C$\tools"
	write-host -ForegroundColor Magenta $computer
	Invoke-Command -FilePath $Filepath -ComputerName $computer -ArgumentList Process
}
#MineCraftInstaller.ps1
Start-Process "msiexec" -ArgumentList '/i "C:\Tools\MineCraftInstaller.msi" /qn /quiet' -Wait -NoNewWindow

Why 2 scripts?

This should be all you need

$ComputerList = Get-content 'C:\tools\server.txt'
Foreach ($ComputerName in $ComputerList) {
    Copy-Item -Path 'C:\tools\minecraftinstaller.msi' -Destination "\\$($ComputerName)\C$\tools"
    Invoke-Command -ComputerName $ComputerName -ScriptBlock {
        Start-Process -FilePath 'C:\Windows\System32\msiexec.exe' -ArgumentList '/i C:\tools\minecraftinstaller.msi /qn' -Wait
    }
}

That works great Olaf, thanks for that.

I have to go and rework a bunch of scripts I have that were using the 2 script technique lol.

1 Like