首页 > 解决方案 > 通过 Powershell 使用自定义安装路径和组件静默安装 Visual Studio 的参数

问题描述

Powershell 的新手,并尝试编写一个脚本,该脚本可以使用 Invoke-Command 及其 -ScriptBlock 参数使用自定义安装路径和自定义组件在远程服务器上进行 Visual Studio 2017 的静默安装。能够静默安装 Visual Studio,但不提供安装路径和自定义组件列表,因为我不确定如何为 thsoe 传递参数。尝试了各种 没有任何帮助,具体到这个任何地方。

参数 (

    $session = "New-PSSession -ComputerName $server -Credential $mycredentials",
    $SourceFile = "\\server\D$\Somefolder\vs.exe",
    $Destination = "D:\Somefolder\"
    )


Write-Host "Installing Visual Studio"

Copy-Item -FromSession $session -Path $SourceFile -Destination $destination -Force

Invoke-Command -Session $session -ScriptBlock { Start-Process $Destination\vs.exe -ArgumentList '--quiet', '--installPath "C\VS\"' -Wait

    }

      Exit-PSSession

尝试了安装路径的不同变体,例如 --installPath "D:\Somefolder\", -installPath "D:\Somefolder\", -install 'D:\Somefolder\' ,甚至考虑过通过变量传递参数但不确定如何它会在这种情况下工作吗?此后降落在这里没有成功,甚至在任何地方都没有看到针对这种情况的帮助。

标签: powershellvisual-studio-2017powershell-remoting

解决方案


尝试类似以下(未经测试) - 请注意与您尝试的内容相关的评论:

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.

推荐阅读