VMware Cloud Community
paulymo
Contributor
Contributor

Invoke-VMScript

Hi All,

I've been working on a script to get a batch file to run within a VM during an SRM failover. I believe the only way to accomplish this is using the Invoke-VMScript command through PowerCLI. I'm hoping someone can tell me what I am missing as it jut times out after 300 seconds and there is no indication from the VMs event log of the script running.

Here is the PowerShell script

$srv = Connect-VIServer 10.1.2.3 -User domainuser -Password XXXXX

$script = '"%windir%\system32\cmd.exe" "%scripts%\iscsi"'

$vm = Get-VM MyVM

$HostUser = "root"

$HostPassword = "XXXXX"

$GuestUser = "VMdomain\User"

$GuestPassword = "XXXXXX"

$ScriptType = "bat"

Connect-VIServer 10.1.2.3 -Session $srv.SessionID

Invoke-VMScript -ScriptText $script -VM $vm -HostUser $HostUser -HostPassword $HostPassword -GuestUser $GuestUser -GuestPassword $GuestPassword -ScriptType $ScriptType

Here is the iSCSI script in the batch file:

@echo off

echo Adding target portals

iscsicli AddTargetPortal 10.4.3.2 3260 "Root\ISCSIPRT\0000_0" 1 * * * * * * * * * *

echo refreshing target portals

iscsicli RefreshTargetPortal 10.4.3.2 3260

echo Adding persistent targets logins

iscsicli PersistentLoginTarget iqn.1992-08.com.netapp:sn.xxxxxxxxx T 10.4.3.2 3260 "Root\ISCSIPRT\0000_0" 1 0x0 0x2 * * * * * * * 0 * 0

echo Login to Target

iscsicli logintarget iqn.1992-08.com.netapp:sn.xxxxxxx T * * Root\ISCSIPRT\0000_0 10.16.1.22 3260 * * * * * * * * * * 0

Here is the command syntax in SRM:

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

The redirect script reads:

@echo off

C:\WINDOWS\system32\windowspowershell\v1.0\powershell.exe -psc "D:\VMware\Infrastructure\vSphere PowerCLI\vim.psc1" "& '%1'"

Any thoughts, suggestions or comments????

0 Kudos
6 Replies
LucD
Leadership
Leadership

Can you try running powershell.exe with the -NonInteractive parameter ?

Btw, you can do 'powershell /?' from a command prompt to see all the parameters.

____________

Blog: LucD notes

Twitter: lucd22


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

0 Kudos
paulymo
Contributor
Contributor

@LucD - I can try that once I get the script working. For right now I am running the script manually through PowerCLI on the SRM server and it times out. Doesn't even seem to run on the VM like it is supposed to. Is there anything in the Powershell script that I need to add or change?

0 Kudos
LucD
Leadership
Leadership

I don't immediately see anything wrong with the PS1 script.

Perhaps you could try running a very simple script first ? Perhaps just a Get-Service cmdlet in the script ?

Another option is to use the Start-Transcript and Stop-Transcript in the script, that way you log what happens in the script in a log.

____________

Blog: LucD notes

Twitter: lucd22


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

0 Kudos
paulymo
Contributor
Contributor

Still not working but at least I'm getting something different now. I changed the $script Syntax to:

$script = '"%windir%\system32\cmd.exe /c c:\scripts\iscsi.bat"'

Now I get the following output...

C:\scripts> .\powercli.ps1

Name Port User

-


-


-


10.x.x.x 443 user

%windir%\system32\cmd.exe /c c:\scripts\iscsi.bat

So it looks like it works but nothing in the VM itself. I took your suggestion of trying a different script, change IP, and it worked like a charm. Any thoughts on this output?

0 Kudos
LucD
Leadership
Leadership

Why do you have the 2nd Connect-VIServer in there ?

When you're PowerCLI is configured for multi-mode, you will now have 2 connections and the cmdlets will be executed against each connection.

____________

Blog: LucD notes

Twitter: lucd22


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

0 Kudos
nnedev
VMware Employee
VMware Employee

Hi,

The call to "Invoke-VMScript" cmdlet seems correct.

I guess there is something wrong with the bat script. You can easily verify its behavior by manually running it on the target vm.

Regards,

Nedko Nedev

PowerCLI Development Team

Regards, Nedko Nedev PowerCLI Development Team
0 Kudos