Still trying to figure out how to automate a build of an azure server, and install software on it after the build.

The template referenced is our own version of the one from used is based off of the one from this one: https://github.com/Azure/azure-quickstart-templates/tree/master/201-vm-winrm-windows

We only added 2016-Datacenter-smalldisk to the WindowsOSversion, to get the latest server.

From building this server, I wait to make sure it is connected to the server before connecting to the server via PSSession. I am trying to use this PSSession to install IIS, JAVA, other needed software. But anything I run after the PSSession, it still installs on my local PC.

I am coding/running this powershell script from my own pc. How do I get these items to install on the server itself?

# 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("test@here.org", $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 

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

$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
$text | Set-Content $Options
Start-Process -FilePath $Destination -ArgumentList "INSTALLCFG=$Options /s /L $InstallDir\jre-install.log" -Wait -Verbose -PassThru

Took me awhile to find the answer. I got the answer from this page. https://stackoverflow.com/questions/34949442/running-azure-commands-over-a-pssession . Enter-PSSession is intended for interactive mode only and not meant for scripting. A way around this is to create a New-PSSession and then use this with Invoke-Command. So I didn't know this....so basically wrapped everything in Invoke-Command

| improve this answer | |
  • Yes, you should use New-PSSession, you could refer my answerabout this. – Shui shengbao Jun 7 '17 at 1:11
  • Glad to hear that your issue has been resolved, please accept it as an answer so that other community members will be benefited. – Shui shengbao Jun 7 '17 at 5:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.