VMware Cloud Community
airinaga
Contributor
Contributor

Failed to run script: Failed to invoke command: Stream is not initialized. Calling powerCLI script from "Run a Command" action.

Hey everyone,

      I know there are numerous posts about this topic but I still cannot seem to get my script to work correctly. I am trying to call a batch file from "Run a Command" in vCenter. This batch file is attempting to execute a simple powerCLI script that will send an email once it has completed.

Here are the contents of my batch file:

@echo off

C:\WINDOWS\system32\windowspowershell\v1.0\powershell.exe -psconsolefile "C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\vim.psc1" -command ". 'C:\scripts\snapshotEmail.ps1'"

As I execute this batch file manually, it works and sends an Email.

Here is what I am entering into the vCenter "Run a Command":

c:\windows\system32\cmd.exe /c c:\scripts\redirect.bat

But this fails miserably every time, I have tried many different ways to call this batch file and nothing works.

* And yes, I have been through almost every single post about this topic, so I am not looking for another hyperlink.

Any help is much appreciated.

Reply
0 Kudos
11 Replies
LucD
Leadership
Leadership

What do you have in snapshotEmail.ps1 ?


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

Reply
0 Kudos
airinaga
Contributor
Contributor

Here is the script I have in my Email.ps1, it is just meant to send an email with the .txt attachment which it does perfectly fine when executing the batch file manually.

$file = "C:\scripts\ssmail.txt"

$smtpServer = "smtp.server"

$att = new-object Net.Mail.Attachment($file)

$msg = new-object Net.Mail.MailMessage

$smtp = new-object Net.Mail.SmtpClient($smtpServer)

$msg.From = "sender address"

$msg.To.Add("my email address")

$msg.Subject = "Notification from email server"

$msg.Body = "Attached is the Snapshot report"

$msg.Attachments.Add($att)

$smtp.Send($msg)

$att.Dispose()

What i'm trying to do is, every time this alarm is triggered, I want it to send an email stating the VM, and the name of the snapshot that was created.

Reply
0 Kudos
airinaga
Contributor
Contributor

I just wanted to test that Email.ps1 file to see if the batch file would actually execute it. Here is the actual snapshotReport.ps1 file, currently they are 2 separate .ps1 files.

$myVMs = Get-VM

$VMsWithSnaps = @()

foreach ($vm in $myVMs) {

    $vmView = $vm | Get-View

    if ($vmView.snapshot -ne $null) {

        Write-Host "VM $vm has a snapshot"

        $SnapshotEvents = Get-VIEvent -Entity $vm -type info -MaxSamples 1000 | Where {

            $_.FullFormattedMessage.contains("Create virtual machine snapshot")}

        try {

        $user = $SnapshotEvents[0].UserName

        $time = $SnapshotEvents[0].CreatedTime

        } catch [System.Exception] {

            $user = $SnapshotEvents.UserName

            $time = $SnapshotEvents.CreatedTime

        }

        $VMInfo = “” | Select "VM","CreationDate","User"

        $VMInfo."VM" = $vm.Name

        $VMInfo."CreationDate" = $time

        $VMInfo."User" = $user

        $VMsWithSnaps += $VMInfo

    }

}

$VMsWithSnaps | Sort CreationDate | Format-List | Out-file C:\scripts\ssmail.txt

Thank you in advance for the help!!!

Reply
0 Kudos
LucD
Leadership
Leadership

Just to make sure, you have the BAT and PS1 file on the C-drive of the vCenter server ?


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

airinaga
Contributor
Contributor

The BAT and PS1 file are currently on my local C drive... Would this mean that powerCLI also needs to be on the vCenter server as well? Would I not just be able to point vCenter to the BAT file on my local C drive?

Reply
0 Kudos
LucD
Leadership
Leadership

I'm afraid so.

The script is triggered by the vCenter service, so the service has to be able to find the script.

Afaik, the mechanism doesn't support UNC paths.

There are some older, but still valuable, resources available on the subject.

See How to run PowerCLI scripts from vCenter Alarms

and More fun with vSphere Alarms


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

Reply
0 Kudos
LucD
Leadership
Leadership

I forgot to mention, in theory you could only have the BAT file on the vCenter.

In the BAT file you could start a PS1 script remotely (with psexec for example)

That way you do not need PowerCLI to be installed in the vCenter server.


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

Reply
0 Kudos
airinaga
Contributor
Contributor

Thank you for the heads up about running it on the vCenter server, I hadn't seen a post that mentioned that yet. Hopefully its doable but, do you know if its possible to achieve this if my vCenter appliance is running on SUSE Linux Enterprise 11? I haven't looked into it much but I assume I can install powerCLI on linux?

Reply
0 Kudos
LucD
Leadership
Leadership

Since PowerCLI requires PowerShell and .Net, you can't install that on a Linux box I'm afraid.


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

airinaga
Contributor
Contributor

Darn, time to look for a new solution. Thanks for all your help, I really appreciate it!

Reply
0 Kudos
andrewsardinha
Enthusiast
Enthusiast

This won't help you with your issue and it doesn't really matter anyway because LucD is the man and already helped you out.

I noticed you were sending your e-mail the old, long way. Try using the new cmdlet: Send-MailMessage. This should be available in PowerShell 2.0 and later. It is cleaner and requires less code.

http://technet.microsoft.com/en-us/library/hh849925(v=wps.620).aspx

Send-MailMessage -To 'test@domain.com' -From "Someone <no-reply@domain.com>" -Subject "Test Message" -BodyAsHtml "This is a test" -SmtpServer 'smtp.domain.com'

$email = @{

from='Someone <no-reply@domain.com>'

to='test@domain.com'

subject="Test Message"

smtpserver = 'smtp.domain.com'

body = "This is a test"

bodyashtml = $true

}

Send-MailMessage @email

Just though I'd chime in with hopes of making your scripts a little more pretty. There is an attachment parameter as well. I know you were using that in your script.

Reply
0 Kudos