1 Reply Latest reply on Jun 22, 2009 1:42 AM by LucD

    Issues with script not runnung unless logged on..

    steve31783 Hot Shot

       

      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"

      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">
      -->[sfortuna74|http://communities.vmware.com//people/sfortuna74|Click to view sfortuna74's profile]  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&gt;&1 &gt; 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 -"

       

       

        • 1. Re: Find my VM within a cluster script
          LucD Guru
          Community WarriorsvExpertUser Moderators

          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)