VMware

This Question is Possibly Answered

1 "correct" answer available (10 pts) 2 "helpful" answers available (6 pts)
1 Replies Last post: Apr 17, 2009 7:28 AM by LucD  

Issues with script not runnung unless logged on.. posted: Apr 17, 2009 7:03 AM

Click to view steve31783's profile Hot Shot 178 posts since
Nov 8, 2006

I am having the same issue as another user posted here: http://communities.vmware.com/docs/DOC-7430

I have the connect-viserver line in the script, and it runs perfectly as a scheduled task when I am logged into the server. When I am not, the script runs and outputs a new html file, but the fields inside are all blank. Any ideas?

"Mar 23, 2009 8:54 AM Reply <!-- <a href="/people/sfortuna74"
title="Click to view sfortuna74's profile"

<img
src="/people/sfortuna74/avatar/16.png?a=-1"
border="0" height="16" width="16"
title="Click to view sfortuna74's profile"
alt="Click to view sfortuna74's profile"></a>
-->sfortuna74 says:

Hi -

We have recently started using this script and found it to be more helpful than we could have imagined. We are having one problem, however, which I hope you can help us with. We can only get the script to run when we are logged into the server. When the script is set to run as a scheduled task, we get the email report, however all the fields are empty. I then mofified our batch file to write to an error log, via "2>&1 > error.log" and the output I see when the script attempts to run as a scheduled task is as follows -
You are not currently connected to any servers. Please connect first using Connect-V
IServer
Might you be able to help us understand what we need to do to make this error go away?

Thank you very much for sharing the script and for your time -"

Re: Find my VM within a cluster script

1. Jun 22, 2009 1:42 AM in response to: steve31783
Click to view LucD's profile Champion 2,430 posts since
Oct 31, 2005
Getting the ESX hostnames from a text file is easy, you can do this with a Get-Content cmdlet.

To run the script as a scheduled task see Re: Script as a Scheduled Task.
But watch out, the foldername has changed in PowerCLI v4!
C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe -PSConsoleFile "C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\vim.psc1" <full-path-to-the-script.ps1>

If you're using PowerCLI v4 you don't need Rob's fix (scheduled tasks + credentials not passed to script) for scheduled tasks anymore.

Sending the report as an email is straightforward.
# Array $ESXhosts contains the names of all the ESX servers
$ESXhosts = Get-Content "C:\ESXhosts.txt"

$user = <ESX-account>
$pswd = <ESX-password>
$VCname = <VC-hostname>
$SQLname = <SQL-hostname>

$report = @()
foreach($esx in $ESXhosts){
  $srv = Connect-ViServer -Server $esx -User $user -Password $pswd
  Get-Vm | where {$_.Name -eq $VCname -or $_.Name -eq $SQLname} | %{
    $line = ($_.Name + " on ESX server " + $esx) | Out-String
	$report += $line
  }
  Disconnect-ViServer $srv -Confirm:$false
}

# Send as email
$SmtpClient = New-Object system.net.mail.smtpClient
$MailMessage = New-Object system.net.mail.mailmessage
$SmtpClient.host = <smtp-servername>
$MailMessage.from = <from-email-address>
$MailMessage.To.add(<to-email-address>)

$MailMessage.Subject = "Critical guests"
$MailMessage.body = ($report | Out-String -width 60)
$SmtpClient.Send($MailMessage)

VMware Developer

SDKs, APIs, Videos, Learn and much more in the Developer community.

Learn More

Developer Sample Code

Increase your developer productivity with VMware API sample code.

Learn More

VMworld Sessions & Labs

Online access to the latest VMworld Sessions & Labs and online services.

Learn more

Purchase PSO Credits Online

Purchase credits to redeem training and consulting services online.

Buy Now

Community Hardware Software

View reported configurations or report your own.

Learn More

VMware vSphere

Come witness the next giant leap in virtualization.

Register Today

Communities