仍在尝试弄清楚如何自动构建 Azure 服务器,并在构建之后在其上安装软件。
引用的模板是我们自己使用的版本,基于这个模板: https://github.com/Azure/azure-quickstart-templates/tree/master/201-vm-winrm-windows
我们仅将2016-Datacenter-smalldisk添加到WindowsOS版本,以获取最新的服务器。
从构建此服务器开始,我等待确保它已连接到服务器,然后才通过 PSSession 连接到服务器。我尝试使用此 PSSession 安装 IIS、JAVA 和其他所需软件。但是我在 PSSession 之后运行的任何程序仍会安装在我的本地 PC 上。
我正在自己的电脑上编写/运行此 powershell 脚本。如何将这些项目安装在服务器本身上?
# Variables for common values
$location = "East US"
$resourceGroup = "rgTest"
$vmName = "vmTest"
$subscriptionname = "snTest"
$secpasswd = ConvertTo-SecureString "Password1" -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential("[email protected]", $secpasswd)
# Add your Azure account to the local PowerShell environment.
Add-AzureRmAccount -Credential $cred
Login-AzureRmAccount -Credential $cred
Write-Host "Step 0"
Get-AzureRmResourceGroup -Name $resourceGroup -ev notPresent -ea 0
if ($notPresent)
{
New-AzureRmResourceGroup -Name $resourceGroup -Location $location
}
$tempsubscriptionname = Select-AzureRmSubscription -SubscriptionName
$subscriptionname
Set-AzureRmCurrentStorageAccount -ResourceGroupName rgseleniumtest1 -Name saseleniumtemplates
$templateuri = New-AzureStorageBlobSASToken -Container templates -Blob template.json -Permission r `
-ExpiryTime (Get-Date).AddHours(2.0) -FullUri
Get-AzureRmResourceGroup -Name $resourceGroup -Location "East US"
New-AzureRmResourceGroupDeployment -ResourceGroupName $resourceGroup -TemplateUri $templateuri
Do {
$Ping = Get-CimInstance -ClassName Win32_PingStatus -Filter "Address='dnsselenium.eastus.cloudapp.azure.com' AND Timeout=10000";
Start-Sleep -s 30
}
Until($Ping.IPV4Address -ne "")
$hostName= 'testtesttest.eastus.cloudapp.azure.com'
$winrmPort = '5986'
$soptions = New-PSSessionOption -SkipCACheck
$secpasswd = ConvertTo-SecureString "Password1" -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential("LocalAdminUser", $secpasswd)
Enter-PSSession -ComputerName $hostname -Credential $cred -Port $winrmPort -SessionOption $soptions -UseSSL
#try 1
import-module servermanager
Add-WindowsFeature web-server -IncludeAllSubFeature
try 3
# Install iis
Install-WindowsFeature web-server -IncludeManagementTools
#install Java
$InstallDir = "c:\installer"
$Destination = "c:\installer"
New-Item -Path $InstallDir -type directory -Force
#LATEST JAVA 8 JRE
$Source = "http://javadl.oracle.com/webapps/download/AutoDL?BundleId=211996"
$Destination = "$Destination\java.exe"
$Options = "$InstallDir\java_options.txt"
(New-Object System.Net.WebClient).DownloadFile($Source, $Destination)
$text = 'INSTALL_SILENT=Enable
AUTO_UPDATE=Enable
REBOOT=Disable
SPONSORS=Disable
REMOVEOUTOFDATEJRES=Enable
'
$text | Set-Content $Options
Start-Process -FilePath $Destination -ArgumentList "INSTALLCFG=$Options /s /L $InstallDir\jre-install.log" -Wait -Verbose -PassThru
答案1
我花了一段时间才找到答案。我从这个页面找到了答案。 https://stackoverflow.com/questions/34949442/running-azure-commands-over-a-pssession 。Enter-PSSession 仅适用于交互模式,不适用于脚本。解决此问题的方法是创建一个 New-PSSession,然后将其与 Invoke-Command 一起使用。所以我不知道这一点......所以基本上将所有内容都包装在 Invoke-Command 中