Значение «this» зависит от «контекста», в котором выполняется функция. Контекстом может быть любой объект или глобальный объект, т. Е. Окно.
Таким образом, семантика «этого» отличается от традиционных языков ООП. И это вызывает проблемы: 1. когда функция передается другой переменной (скорее всего, обратный вызов); и 2. когда замыкание вызывается из метода-члена класса.
В обоих случаях это устанавливается в окно.
Попробуйте что-то вроде следующего (непроверено) - обратите внимание на комментарии, которые относятся к тому, что вы пробовали:
param (
# Use $(...) to use a command's output as a parameter default value.
# By contrast assigning "..." only assigns a *string*.
$session = $(New-PSSession -ComputerName $server -Credential $mycredentials),
# To be safe, better to `-escape $ inside "..." if it's meant to be a literal.
$SourceFile = "\\server\D`$\Somefolder\vs.exe",
$Destination = "D:\Somefolder"
)
Write-Host "Installing Visual Studio"
Copy-Item -FromSession $session -Path $SourceFile -Destination $destination -Force
# You don't need the session anymore now that you've copied the file.
# Normally you'd call
# Remove-PSSession $session
# However, given that the session may have been passed as an argument, the caller
# may still need it.
# With the installer present on the local machine, you can invoke
# Start-Process locally - no need for a session.
Start-Process -Wait $Destination\vs.exe -ArgumentList '--quiet', '--installPath "C:\VS"'
# No need for Exit-PSSession (which is a no-op here), given that
# you haven't used Enter-PSSession.