Pages: [1]
Print
Author Topic: PowerShellASP  (Read 6742 times)
fixitchris
Guest
« on: April 07, 2009, 01:25:56 PM »

http://www.powershelltoys.com/ allows you to run PS commands over the Web...

Here is an example for connecting to a host, listing the datastores, guests and VMDKs:
Code:
<html>
    <head>
       
    </head>
    <body>

        <%Add-PSSnapin VMware.VimAutomation.Core%>
        <%$host1 = Connect-VIServer -server 192.168.0.1 -user myuser -password mypass%>
        <%$hostinfo = get-vmhost%>
        <b>VMware.VimAutomation.Types.VMHost</b></br>
Host: <% $hostinfo.Name %></br>
Host Id: <% $hostinfo.Id %></br>
Host State: <% $hostinfo.State %></br></br>

<b>VMware.VimAutomation.Client20.DatastoreImpl</b></br>
<% $ds = Get-Datastore -VMHost $hostinfo %>
<% $ds | % { %>
<% $_.Name %></br>
<% } %>
</br>

<b>VMware.VimAutomation.Types.VirtualMachine</b></br>
Guests:</br>
<table>
<% Get-VM | % { %>
<tr>
<td><% ($_.Name) %></td>
<td><% ($_.Description) %></td>
<td><% ($_.PowerState) %></td>
<td><% ($_.Guest.State) %></td>
<td><% ($_.Guest.IPAddress) %></td>
<td>
<% $diskInfo = $_.Guest.Disks %>
<% $diskInfo | % { %>
<td><% $_.Path %></td>
<% } %>
</td>

<td>
<% $harddisks = Get-HardDisk -VM $_ %>
<% $harddisks | % { %>
<td><% $_.Filename %></td>
<% } %>
</td>

</tr>
<% } %>
</table>

</br></br>
               
    </body>
</html>

This disconnect line will fail (it attempts to change the window title per c_shanklin):
Code:
<% Disconnect-VIServer -server $host1 -whatif:$true -confirm:$false %>

Alternate Disconnect Code:
Code:
<% (get-view serviceinstance).Client.Logout() %>

And Here is the Output:
Code:
VMware.VimAutomation.Types.VMHost
Host: 192.168.0.1
Host Id: HostSystem-ha-host
Host State: Connected

VMware.VimAutomation.Client20.DatastoreImpl
datastore1

VMware.VimAutomation.Types.VirtualMachine
Guests:
VM1 Windows 2003 x86 PoweredOn Running 192.168.0.1 C:\ [datastore1] VM1/VM1.vmdk
VM2 Windows 2003 x86 PoweredOn Running 192.168.0.3 C:\ [datastore1] VM2/VM2.vmdk
VM3 Windows 2003 x86 PoweredOff NotRunning [datastore1] VM3/VM4.vmdk
VM4 Windows 2003 x86 PoweredOff NotRunning


And here is how to catch the response to a file:
Code:
$xmlhttp = New-Object -ComObject Msxml2.XMLHTTP.3.0
$xmlhttp.open("GET","http://somewhere.com/myhoststate.ps1x", $false);
$xmlhttp.send();
$s = $xmlhttp.responseText
Add-Content "c:\temp\resp.txt" -Value $S
« Last Edit: April 07, 2009, 11:13:15 PM by fixitchris » Logged
tteller
Newbie
*
Posts: 3


View Profile
« Reply #1 on: July 03, 2009, 12:30:06 AM »

Fixitchris,

I like your code.  And it works great!!  Once your code ran.. then it opened the world of possibilities for me.  But... I am stuck on a simple thing - credentials.

I created a credential store using the New-VICredentialStoreItem, copied the .xml file to the systemprofile appdata directory.  But I am unable to get the script to work.  It bombs at the connect-viserver cmdlet.  The error message is : The method or operation is not implemented. 

Here's what I am using:


                                             
<html>
    <head>
       
    </head>
    <body>

        <% Add-PSSnapin VMware.VimAutomation.Core %>
        <% $credentials=Get-VICredentialStoreItem -Host vcenter %>
        <% Connect-VIServer -Server vcenter -User $credentials.User -Password $credentials.Password %>
      
        <table>
        <tr>
       
        <% Get-VM | where {%>
        <td><% ($_ | get-networkadapter).NetworkName -eq "Virtual Machine Network"%></td>
        <td><% } | ft name, powerstate %></td>
       
        </tr>
        </table>
       

       
     
    </body>
</html>


Have you successfully used the Get-VICredentialStoreItem with your ps1x pages?

Thank you in advance,
Tim T

Logged
fixitchris
Newbie
*
Posts: 7


View Profile Email
« Reply #2 on: July 06, 2009, 10:22:57 PM »

I haven't tried, but I ran into the same issues with other cmdlets... That's the reason for the alternate disconnect code... You probably get the ui.host.write something or other error, right?

Try this instead: http://halr9000.com/article/531
Logged
tteller
Newbie
*
Posts: 3


View Profile
« Reply #3 on: July 13, 2009, 11:48:21 AM »

Sorry it took me a while to get back with you.  Yes, I have found out that the credentials must be called from the user that made them.  I used the XML file and encrypted text file, and both failed with the PowershellASP plugin.  I can, however, use these methods in PowerCLI without issues.  Your script works perfect for me, I just didn't want to put credentials in a ps1x file.  If you get it to work, please share. 

Thanks,
Tim T
Logged
fixitchris
Newbie
*
Posts: 7


View Profile Email
« Reply #4 on: July 13, 2009, 01:36:02 PM »

Oh, I see.  So the issue is that ASP.NET is using NetworkService log in to run your code.  Try running the web services as the user.  There are documented ways around this issue.  Take a look at my blog post: http://sync-io.net/go/blog/2008/03/27/CreateProcessAsUserFromSystemServiceVBNET.aspx

Logged
ptburkis
Newbie
*
Posts: 2


View Profile Email
« Reply #5 on: November 24, 2009, 08:01:13 AM »

Hi Guys,

I've tried to set this up on two servers, however, whenever I invoke the "connect-viserver" commandlet I get the following error:

Error while executing PowerShell Script:
C:\Program Files\PowerShellToys\PowerShellASP V1\demos\getmksticket.ps1x (6,37): The current configuration system does not support user-scoped settings.

Source Code:
$host.ui.write(""); Connect-VIServer -server <MYVCC> -user <MYUSER> -password <mypassword>

The stack trace is:

Stack Trace:


[a (0x80004005): Error while executing PowerShell Script:
C:\Program Files\PowerShellToys\PowerShellASP V1\demos\getmksticket.ps1x (6,37): The current configuration system does not support user-scoped settings.


 


Any help would be much appreciated!

Cheers

Peter

Logged
fixitchris
Newbie
*
Posts: 7


View Profile Email
« Reply #6 on: November 24, 2009, 08:35:05 AM »

"The current configuration system does not support user-scoped settings." is an odd exception in your context.  The error id is 'access denied'.  Are you supplying a server name, user and password on the vi-connect line?  What IIS version?  What is the application pool running as, Network Service?
Logged
ptburkis
Newbie
*
Posts: 2


View Profile Email
« Reply #7 on: November 24, 2009, 11:00:58 AM »

Hi, thanks for getting back to me!

I'm supplying the correct servername, username and password in the script.

This seems to happen on my XP IIS 5.0 machine and Windows 2003 IIS 6.0 machine. On the IIS 6.0 setup I have put the app into it's own application pool and set the identity to an administrative user, but still no joy :-(
Logged
Pages: [1]
Print
Jump to: