VMware Cloud Community
jfierberg
Contributor
Contributor
Jump to solution

Remote Execution of View PowerCli

I am attempting to create a script that will execute a VDI vm reset when an alarm is triggered in vcenter and remotely tell the View connection Broker to refresh that vm.

While the actual mechanism for getting the vm name and the command to execute a refresh are no big deal, I am hung up on the Add-pssnapin portion of the script.  Each time I run it, the error "Add-PSSnapin : The Windows PowerShell snap-in 'VMware.View.Broker' is not installed on this machine." appears.  the script does connect to the connection broker and if I manually run the Add-pssnapin command at a powershell prompt, it works. How can I get the add-pssnapin to work from the script?

set-executionpolicy RemoteSigned
Enter-PSSession -ComputerName <View Connection Server>
Add-PSSnapin VMware.View.Broker #-ErrorAction SilentlyContinue

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

I'm afraid that is not possible, see Andre's post Remote use of VMware.View.Broker PowerCLI

In that post you find another way of managing View remotely


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

0 Kudos
13 Replies
LucD
Leadership
Leadership
Jump to solution

Does this return anything when you run it from a script ?

Enter-PSSession -ComputerName USPHX-VIEWMGR01
Get-PSSnapin


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
jfierberg
Contributor
Contributor
Jump to solution

Yes, when I run it from a script i get the following.

PS C:\Users\TEMP> C:\scripts\remoterefresh.ps1
Add-PSSnapin : The Windows PowerShell snap-in 'VMware.View.Broker' is not installed on this machine.
At C:\scripts\remoterefresh.ps1:3 char:13
+ Add-PSSnapin <<<<  VMware.View.Broker #-ErrorAction SilentlyContinue
    + CategoryInfo          : InvalidArgument: (VMware.View.Broker:String) [Add-PSSnapin], PSArgumentException
    + FullyQualifiedErrorId : AddPSSnapInRead,Microsoft.PowerShell.Commands.AddPSSnapinCommand

0 Kudos
LucD
Leadership
Leadership
Jump to solution

I meant a Get-PSSnapin, just to check if the remote session sees the snapins.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
jfierberg
Contributor
Contributor
Jump to solution

Sorry, no the Get-pssnapin does not show the wmware.view.broker snap-in

Name        : Microsoft.PowerShell.Diagnostics
PSVersion   : 2.0
Description : This Windows PowerShell snap-in contains Windows Eventing and Performance Counter cmdlets.

Name        : Microsoft.WSMan.Management
PSVersion   : 2.0
Description : This Windows PowerShell snap-in contains cmdlets (such as Get-WSManInstance and Set-WSManInstance) that a
              re used by the Windows PowerShell host to manage WSMan operations.

Name        : Microsoft.PowerShell.Core
PSVersion   : 2.0
Description : This Windows PowerShell snap-in contains cmdlets used to manage components of Windows PowerShell.

Name        : Microsoft.PowerShell.Utility
PSVersion   : 2.0
Description : This Windows PowerShell snap-in contains utility Cmdlets used to manipulate data.

Name        : Microsoft.PowerShell.Host
PSVersion   : 2.0
Description : This Windows PowerShell snap-in contains cmdlets (such as Start-Transcript and Stop-Transcript) that are
              provided for use with the Windows PowerShell console host.

Name        : Microsoft.PowerShell.Management
PSVersion   : 2.0
Description : This Windows PowerShell snap-in contains management cmdlets used to manage Windows components.

Name        : Microsoft.PowerShell.Security
PSVersion   : 2.0
Description : This Windows PowerShell snap-in contains cmdlets to manage Windows PowerShell security.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

That could mean that the remote session was not set up, but then I would have expected an error message.

Are you executing this from the PowerCLI prompt or from something like PowerGui ?

Just to make sure, you are using the correct servername in the script ?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
jfierberg
Contributor
Contributor
Jump to solution

I am connecting through the powershell console on my vcenter server, no PowerGUI.

0 Kudos
jfierberg
Contributor
Contributor
Jump to solution

Could PSEXEC be made to run in a powercli script to execute the VIEW VM refresh?

0 Kudos
LucD
Leadership
Leadership
Jump to solution

That should work. Something like this perhaps

psexec.exe /acceptEula -u user -p pswd \\ViewSrv cmd /c  Echo .^| powershell.exe -file "C:\script.ps1"


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
jfierberg
Contributor
Contributor
Jump to solution

I tried it but the system I ran it from (vcenter) does not have the vmware.view.broker snap in.  If there is a way to install that on the vcenter system, we'd have a winner.

results:

Add-PSSnapin : The Windows PowerShell snap-in 'VMware.View.Broker' is not installed on this machine.
At C:\scripts\refreshvdi.ps1:3 char:13
+ add-pssnapin <<<<  VMware.View.Broker
    + CategoryInfo          : InvalidArgument: (VMware.View.Broker:String) [Add-PSSnapin], PSArgumentException
    + FullyQualifiedErrorId : AddPSSnapInRead,Microsoft.PowerShell.Commands.AddPSSnapinCommand

The term 'get-desktopvm' is not recognized as the name of a cmdlet, function, script file, or operable program. Check t
he spelling of the name, or if a path was included, verify that the path is correct and try again.
At C:\scripts\refreshvdi.ps1:7 char:14
+ get-desktopvm <<<<  -Name $vdi | Send-LinkedCloneRefresh -schedule (Get-Date)
    + CategoryInfo          : ObjectNotFound: (get-desktopvm:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

0 Kudos
LucD
Leadership
Leadership
Jump to solution

I'm afraid that is not possible, see Andre's post Remote use of VMware.View.Broker PowerCLI

In that post you find another way of managing View remotely


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
jfierberg
Contributor
Contributor
Jump to solution

Yeah I tried that, no luck. Thanks though for the ideas.

0 Kudos
jfierberg
Contributor
Contributor
Jump to solution

Do you know if there is powercli way to detect “Agent Unavailable” from a View Connection Broker?

0 Kudos
wframe
Contributor
Contributor
Jump to solution

It looks like you are running commands on the vCenter server and not the Connection server.  I'm not sure if it has changed, but I believe you need to run any View PowerCLI commands directly on the Connection server.

So!  What you need to do is to Enable-PSRemoting on the Connection server (run as administrator).  Once remoting is set up, you can use remoting in your script to run commands against the Connection server.  Google PowerShell remoting if you have trouble.

$server = "ViewConnectionServer"
$session = New-PSSession -Computername $server
#Define a scriptblock to run - $computer is set to the first argument, $variable to the second
$scriptBlock = {
    param($computer,$variable)
    Add-PSSnapin vmware.view.broker
    Verb-Noun -ComputerName $computer -Argument $variable
}
#Run the script block on the connection server.  $argument1 will become $computer in the scriptblock and so forth
$resultsFromCommand = Invoke-Command -Session $session -ScriptBlock $scriptBlock -ArgumentList $argument1, $argument2
Remove-PSSession $session

On a side note, hopefully they do let you access the status!  For View 4.6, the status is stored in the ADAM database, and directly querying this is not supported.  Someone previously submitted a feature request and development is presumably working on adding a cmdlet to expose this information.

0 Kudos