Powershell的所有新功能,并尝试编写一个脚本,该脚本可以使用Invoke-Command及其-ScriptBlock参数在具有自定义安装路径和自定义组件的远程服务器上执行静默安装Visual Studio 2017。 能够以静默方式安装Visual Studio,但不提供安装路径和自定义组件列表,因为我不确定如何为thsoe传递参数。尝试过各种方法,但在任何地方都看不到帮助。
param(
$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 \',甚至想到了通过变量传递参数,但不知道在这种情况下它将如何工作。 因此,在这里登陆并没有成功,甚至在任何地方都没有看到针对这种情况的帮助。
答案 0 :(得分:0)
尝试类似以下内容(未经测试)-注意与您尝试过的内容有关的评论:
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.