Jump to content


Photo

Script to Update XenServer with Powershell

Started by Maikel Gädker , 20 July 2012 - 09:35 PM
10 replies to this topic

Maikel Gädker Members

Maikel Gädker
  • 309 posts

Posted 20 July 2012 - 09:35 PM

Hi there,

Using the Xencenter to apply more then one patch is a timespending Work while every patch must be applied seperat.
So i wrote a few lines of code-snippets to apply multiple XenServer-Patches to my StandAlone XenServers.

I started with xe-commands only and tried to convert the script to powershell. Now i got a running version (but not realy a v1.0) and i would like to share it.
Maybe someone can use it and hopefuly someone will leave a commend, critic or usefull tipps.

While this editor has some problems with formating Code you only download the Zipfile if you thing following code could be useful ;)

<code>
# Xenserver-UpdateScript
# V 0.9
# 20.07.2012
# written by Maikel Gaedker (maikel@gaedker.de)

< <em># Updates XenServer with all patches available within a defined path+
# Requirements: PowerShell v2.0; XenServerPSSnapIn
Import-Module XenServerPSSnapIn

$server = read-host "Enter XenServer to patch" # Enter XenServer Hostname or IP-Address when promted
+$password = read-host "Enter root-password" # Attention: Password is displayed!
# Path to folder with downloaded XenServerpatches
# Only have required patches within these folder
$updatepath = "\\Fileserver\Shared_Folder\xs_updates" # Change to whatever your folder is

< <em>#Start Script+

< <em>Connect-XenServer -server $server -UserName root -Password $password+
write-host "$server will be patched; running VMs will be shut down" -foregroundcolor "green"
write-host "======================================================="
Get-XenServer:VM | Where-Object { $_.is_a_template-eq $false-and $_.is_control_domain-eq $false} | foreach-object {Invoke-XenServer:VM.CleanShutdown -vm $_.name_label}

foreach ($update in Get-ChildItem $updatepath)

< <em>write-host "Patch: $update will be prepared" -foregroundcolor "green"+
Invoke-XenServer:Host.Disable -Host $server
Invoke-XenServer:Host.Reboot -Host $server
write-host "Server will be rebooted" -foregroundcolor "green"
start-sleep -s 300 # test if this time could be shorten for yor Xen Host
Connect-XenServer -server $server -UserName root -Password $password
write-host "$update wird hochgeladen" -foregroundcolor "green"
xe.exe -s $server -u root -pw $password patch-upload file-name=$updatepath\$update | out-file $server"_uuid.txt"
$uuid_patch = get-content .\$server"_uuid.txt"
write-host "$update wird installiert" -foregroundcolor "green"
xe.exe -s $server -u root -pw $password patch-pool-apply uuid=$uuid_patch
write-host "$server was updated with patch: $update" -foregroundcolor "green"
write-host "======================================================="

< <em>write-host "$server was updated with all required patches; one last reboot" -foregroundcolor "yellow"+
Invoke-XenServer:Host.Disable -Host $server
Invoke-XenServer:Host.Reboot -Host $server
start-sleep -s 300 # test if this time could be shorten for yor Xen Host
Connect-XenServer -server $server -UserName root -Password $password
Remove-Item .\$server"_uuid.txt"
write-host "Die VMs werden wieder gestartet" -foregroundcolor "green"
Get-XenServer:VM | Where-Object { $_.is_a_template-eq $false-and $_.is_control_domain-eq $false} | foreach-object {Invoke-XenServer:VM.Start -vm $_.name_label}
Disconnect-XenServer
</code>

Edited by: Maikel Gädker on 20.07.2012 17:38

Fixed Password SecureString Error

Edited by: Maikel Gädker on 22.07.2012 19:14

Attached Files



Alan Lantz Members

Alan Lantz
  • 7,300 posts

Posted 22 July 2012 - 03:19 PM

I applaude your effort to attempt such a thing. But quite frankly the automatic rolling pool upgrade for myself has been flaky at best. I don't mind scripting, but I think in the upgrade world I will be shy toward the cautious side and continue to do them the slow painful way.

Alan Lantz
SysAdmin
City of Rogers, AR



Maikel Gädker Members

Maikel Gädker
  • 309 posts

Posted 22 July 2012 - 03:50 PM

I will agree while working with a XenServer Pool.
But i don`t realy like the Xencenter update Tool for Updates on a number of Standalone Server.



Maikel Gädker Members

Maikel Gädker
  • 309 posts

Posted 22 July 2012 - 05:57 PM

Sadly there is no easy "quick n quirty" way secure the Passwordstring.
Maybe i will rewrite the whole credential part ...



Tobias Kreidl CTP Member

Tobias Kreidl
  • 18,737 posts

Posted 22 July 2012 - 10:14 PM

You could simply prompt the user to supply the password and store it in a variable just while the script is running.
Alternatively, there are a number of possibilities that come to mind, but none particularly elegant.
--Tobias



Maikel Gädker Members

Maikel Gädker
  • 309 posts

Posted 23 July 2012 - 07:52 AM

Thats what i do. Problem is that the password is displayed and not secured. If you use asSecureString as parameter the XS will not exept the password cause it is encrypted.



Maikel Gädker Members

Maikel Gädker
  • 309 posts

Posted 23 July 2012 - 02:59 PM

Found a little hack at http://www.leeholmes.com/blog/2007/01/25/powershell-credentials-and-getnetworkcredential/

So you can "secure" the password the most elegant way, i found, so far.

$SecureString = read-host "Enter root-password" -asSecureString
$password = [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($secureString))



Tobias Kreidl CTP Member

Tobias Kreidl
  • 18,737 posts

Posted 23 July 2012 - 03:36 PM

Nice!
--T.



Maikel Gädker Members

Maikel Gädker
  • 309 posts

Posted 23 July 2012 - 03:52 PM

Sadly most of the code will be formated even if you use plain text, so i added the part for the securestring password as attachment.

Thanks Thomas. Your tip will make it even a bit faster ;)

Attached Files



Maikel Gädker Members
  • #10

Maikel Gädker
  • 309 posts

Posted 29 July 2012 - 07:20 PM

Updated the securesting for Password and run an async shutdown to speed up the shutdown process.

Now i will try to change the installation part from xe-command to powershell.

Attached Files



luis yax Members
  • #11

luis yax
  • 2 posts

Posted 21 May 2014 - 02:32 AM

Here is my solution for the password issue:

 

# This is only done once, after that you have an encrypted file that can only be decrypted by you

Read-Host -AsSecureString | ConvertFrom-SecureString | Out-File C:\DevOps\MyString.txt

 

$SecString = Get-Content C:\DevOps\MyString.txt | ConvertTo-SecureString
$MyCreds = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList "YourDomain\YourAccount",$SecString
 
Connect-XenServer -Server 'MyXenCluster.YourDomain.local' -Creds $MyCreds -SetDefaultSession -NoWarnNewCertificates

 

### Make sure this line is above your script, in my case I have it on $PROFILE

 

if ( (Get-PSSnapIn -Name "XenServerPSSnapIn" -ErrorAction SilentlyContinue) -eq $Null ) {Add-PSSnapIn XenServerPSSnapIn}