as900w
Hot Shot
Hot Shot

How use script to Reset Virtual Machine

Jump to solution

I have a Horizon environment.

Because it is test  environment, I need frequently reset virtual machine.

I want to create a script help me to reset virtual machine.

What do I do?

1 Solution

Accepted Solutions
jrodsguitar
Enthusiast
Enthusiast

Make sure that you do not specify the domain in the $username variable. So for example if your admin account is 'Administrator', your password is 'cookies',  and your domain name is ' 'catsanddogs' ,you would do it this way:

$password = ConvertTo-SecureString 'cookies' -AsPlainText -Force

$domain = 'catsanddogs'

$username = 'Administrator'

$domainuser = $domain + '\' + $username

$Cred = New-Object System.Management.Automation.PSCredential ("$domainuser", $password)

$machines = ('B-W10-01', 'B-W10-02')

Refresh-ViewDesktop -machines $machines -viewserver 'vc.abc.com' -cred $cred

Blog: https://powershell.house/

View solution in original post

0 Kudos
18 Replies
jrodsguitar
Enthusiast
Enthusiast

I assume that by "reset" you mean what VMware calls a "refresh" in Horizon? Resetting the machine back to when it was first composed right?

If so then this PowerCLI function  does exactly what you need:

https://get-cj.com/2018/04/02/how-to-refresh-vmware-horizon-desktops-using-powershell-powercli/

Example Usage:

$machines = ('machinename1', 'machinename2')

Refresh-ViewDesktop -machines $machines -viewserver  'Horizon_Connection_Server'

Blog: https://powershell.house/
0 Kudos
as900w
Hot Shot
Hot Shot

How run Refresh-ViewDesktop?

I run it was failed in view PowerCLI.

Error:Refresh-ViewDesktop:the term 'Refresh-ViewDesktop' is not recognized as the name of a cmdlet, function,script file, or operable program.

0 Kudos
jrodsguitar
Enthusiast
Enthusiast

It's a function, so you'll have to run it from within ISE or integrated it into a script.

From within ISE you must first run the function, then you should be able to call it with refresh-ViewDesktop.

Blog: https://powershell.house/
0 Kudos
as900w
Hot Shot
Hot Shot

I'm sorry. I still failed.

I copy this script to Windows PowerShell ISE,

Then changed the following:

#$machines = ('B-W10-01', 'B-W10-02')

#refresh-ViewDesktop -machines $machines -viewserver  'vc.abc.com'

B-W10-01 and  B-W10-02 is machine Name

vc.abc.com is connection server name.

Run this script, It didn't go wrong, but it didn't work.

Where do I need to change?

0 Kudos
jrodsguitar
Enthusiast
Enthusiast

When you call the function, run it without the "#".

I re-arranged some things so copy this one and try it now:

#Script by Jose Rodriguez

#https://get-cj.com

#Uses ONLY the Horizon View API to refresh a set of desktops or a single desktop.

function refresh-ViewDesktop {

  [CmdletBinding()]

  [OutputType([PSCustomObject])]

    param(

              

        [string[]]$machines,

        [string]$viewserver

       

    )

$cred = Get-Credential

$hvServer = Connect-HVServer -server $viewserver -Credential $cred

$hvServices = $hvServer.ExtensionData

$queryService = New-Object VMware.Hv.QueryServiceService

$query = New-Object VMware.Hv.QueryDefinition 

$query.queryEntityType = 'MachineNamesView'

[VMware.Hv.QueryFilter []] $filters = @()

foreach ($machine in $machines){

       

    $filters += new-object VMware.Hv.QueryFilterEquals -property @{'memberName' = 'base.name'; 'value' = $machine}

}

$orFilter = new-object VMware.Hv.QueryFilterOr -property @{'filters' =  $filters}

$query.Filter = $orFilter

$queryResults = $queryService.QueryService_Create($hvServices,$query)

$finalmachine = @()

foreach ($result in $queryResults.Results){

    [VMware.Hv.MachineNamesView]$machineNamesView = $result

    $properties = @{

              

        machineid = $machineNamesView.id

        names = $machineNamesView.base.name

        desktopId = $machineNamesView.base.Desktop

                }

    $finalmachine += New-Object -TypeName pscustomobject -Property $properties

}

$desktopService = New-Object VMware.Hv.DesktopService

$machineService = New-Object VMware.Hv.MachineService

foreach($object in $finalmachine){

    $desktoprefreshSpec = New-Object VMware.Hv.DesktopRefreshSpec

    $desktoprefresheSpecHelper = New-Object VMware.Hv.DesktopService+DesktopRefreshSpecHelper

    $desktoprefreshSpec.Machines = $object.machineid

    $desktoprefreshSpec.LogoffSetting = $desktoprefresheSpecHelper.LOGOFF_SETTING_FORCE_LOGOFF

    $desktopService.Desktop_Refresh($hvServices, $machineNamesView.base.Desktop,$desktoprefreshSpec)

    Write-Output ""

    Write-Output "Refreshing Machines"

    $($object.names)

    }

}

And you would run it like this:

$machines = ('B-W10-01', 'B-W10-02')

Refresh-ViewDesktop -machines $machines -viewserver 'vc.abc.com'

Blog: https://powershell.house/
as900w
Hot Shot
Hot Shot

have a error

1.PNG

0 Kudos
jrodsguitar
Enthusiast
Enthusiast

Might be copy and paste issue.

Try copying from attached text file.

Blog: https://powershell.house/
0 Kudos
as900w
Hot Shot
Hot Shot

1. A new problem has arisen.

1.PNG

2. I want to add User and Password of connection server to this script.

     What should I do?

0 Kudos
jrodsguitar
Enthusiast
Enthusiast

1. Uploaded the modified script for you to try. I think this should work now for your specific environment.

2. So I do not recommend embedding credentials but if it's what you need I included it as an example. In the updated file you just need to update the domain,username,password and then run the script.

Don't forget to mark my answer as correct if all is good  Smiley Happy. Thank you.

Blog: https://powershell.house/
0 Kudos
as900w
Hot Shot
Hot Shot

I'm sorry.

I can't connection to Horizon connection server at weekend.

Today, I try this script.

but it's failed.

1.PNG

I'm sorry.

I forgot to introduce my environment to you.

I have a Horizon environment

Install PowerCLI  6.5 in connection server

Run Windows PowerShell ISE

Change path to “C:\Program Files (x86)\VMware\Infrastructure\PowerCLI\Scripts”

Run .\Initialize-PowerCLIEnvironment.ps1

Then copy the script content to Windows PowerShell ISE

2.PNG

Change the script content

$password = ConvertTo-SecureString 'MyAdministratorPassword' -AsPlainText -Force

$domain = 'MyDomainName'              #abc.com

$username = 'Domain\Administrator'

$domainuser = $domain + '\' + $username

$Cred = New-Object System.Management.Automation.PSCredential ("$domainuser", $password)

$machines = ('B-W10-01', 'B-W10-02')

Refresh-ViewDesktop -machines $machines -viewserver 'vc.abc.com' -cred $cred

0 Kudos
jrodsguitar
Enthusiast
Enthusiast

Make sure that you do not specify the domain in the $username variable. So for example if your admin account is 'Administrator', your password is 'cookies',  and your domain name is ' 'catsanddogs' ,you would do it this way:

$password = ConvertTo-SecureString 'cookies' -AsPlainText -Force

$domain = 'catsanddogs'

$username = 'Administrator'

$domainuser = $domain + '\' + $username

$Cred = New-Object System.Management.Automation.PSCredential ("$domainuser", $password)

$machines = ('B-W10-01', 'B-W10-02')

Refresh-ViewDesktop -machines $machines -viewserver 'vc.abc.com' -cred $cred

Blog: https://powershell.house/
0 Kudos
as900w
Hot Shot
Hot Shot

It's working.

Thanks a lot.

0 Kudos
as900w
Hot Shot
Hot Shot

Hi

I have a new issue.

a part of desktop can't reset.

1.PNG

0 Kudos
jrodsguitar
Enthusiast
Enthusiast

If you delete machine manaully. Does it work after that?

Blog: https://powershell.house/
0 Kudos
as900w
Hot Shot
Hot Shot

But, I don't delete any machine.

0 Kudos
as900w
Hot Shot
Hot Shot

Sometime, it is pass.

Sometime, it is failed.

1.PNG

0 Kudos
jrodsguitar
Enthusiast
Enthusiast

interesting I've never had it fail and I use it to reboot hundred of VM's.

Do you see a pattern or a specific status of the machines that are not able to refresh?

Blog: https://powershell.house/
0 Kudos
as900w
Hot Shot
Hot Shot

It's disconnection.

0 Kudos