VMware Cloud Community
nicholas1982
Hot Shot
Hot Shot
Jump to solution

Run Batch in Guest Remotely via PowerCLI

Hi All,

I'm sure I've come across a script that can execute a batch file within Windows guests remotely using PowerCLI, but for the life of me I can't find it.

I have 4 Windows VMs, which have the same login credentials, they have a test.bat file in the same location, I need a way of executing the batch remotely via PowerCLI simultaneously, is this possible?

Nicholas
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

It looks as if the sqlio command is not found. Could be that the directory is not in the Path variable in that OS.

Try with the full path to the sqlio executable.


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

View solution in original post

0 Kudos
9 Replies
LucD
Leadership
Leadership
Jump to solution

You can do that with the Invoke-VMScript cmdlet


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

nicholas1982
Hot Shot
Hot Shot
Jump to solution

Thanks Luc,

It seems to be almost working but I get the following output (see image)

I should also point out I connected PowerCLI directly to the host not vCenter, the batch file I'm trying to run is an SQLIO.exe with the following:

@ECHO OFF

sqlio -kR -t8 -s120 -frandom -o32 -b4 -BH -LS -Fparam.txt

bat.JPG

Nicholas
0 Kudos
LucD
Leadership
Leadership
Jump to solution

It looks as if the sqlio command is not found. Could be that the directory is not in the Path variable in that OS.

Try with the full path to the sqlio executable.


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

0 Kudos
nicholas1982
Hot Shot
Hot Shot
Jump to solution

Hi Luc,

Rather than running the batch this seems to work.

Invoke-VMScript -VM VM1 -ScriptText "C:\SQLIO\SQLIO.EXE -s120 -kR -frandom -b4 -t4 -o32 -LS -BH e:\Testfile.dat" -GuestUser administrator -GuestPassword password

Can you help me with having this executed simultaneously on multiple guest VMs ?

Nicholas
0 Kudos
nicholas1982
Hot Shot
Hot Shot
Jump to solution

Hi Luc,

I think I acutually almost have it, I'm just having trouble specifying the GuestCredential

$guestvms = "flash1,flash2,flash3"

$guestCredential = ?????

$myscript = "C:\SQLIO\SQLIO.EXE -s120 -kR -frandom -b4 -t4 -o32 -LS -BH e:\Testfile.dat"

Invoke-VMScript -VM $guestvms -ScriptText $myscript -GuestCredential $guestCredential

Nicholas
0 Kudos
LucD
Leadership
Leadership
Jump to solution

If you want to be prompted for the credentials, you can use the Get-Credential cmdlet,

Otherwise you can do something like this

$username = "MyUserName"

$password = ConvertTo-SecureString "MyPassword" -AsPlainText -Force

$cred = New-Object -TypeName System.Management.Automation.PSCredential -Argumentlist $username,$password


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

0 Kudos
nicholas1982
Hot Shot
Hot Shot
Jump to solution

Hi Luc,

Any chance you might be able to see where I'm going wrong here, I need to overwrite a txt file within a guest with this value "e:\testfile.dat 8 0x0 11000"

$guestvms = "flash3"

$myscript = "e:\testfile.dat 8 0x0 11000 | Out-File C:\SQLIO\param.txt"

Invoke-VMScript -VM $guestvms -ScriptText $myscript -GuestUser administrator -GuestPassword Password

Nicholas
0 Kudos
LucD
Leadership
Leadership
Jump to solution

The default ScriptType for Windows guest OS is PowerShell.

So you probably have an error saying that there is an "unexpected token" or something along that line.

You will have to make sure that PowerShell sees that first part as a [string], so place it between quotes.

Something like this

$guestvms = "flash3"
$myscript = '"e:\testfile.dat 8 0x0 11000" | Out-File C:\SQLIO\param.txt'
Invoke-VMScript -VM $guestvms -ScriptText $myscript -GuestUser administrator -GuestPassword Password

Those are single quotes on the outside double quotes around the [string].


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

0 Kudos
pravin_utekar12
Contributor
Contributor
Jump to solution

HI LuC,

I need to maintain the Inventory of the Infrastructure so I need tha data Of  VMs in the  following format in the Excel please suggest the script

This for Virtual Machine Inventory

NAME  |  FQDN  | HDD(Assigned HDD Space)  |  Operating System  |  RAM|  CPU  |  NIC  |  IP ADDRESS   | DNS NAME

This is for Host Inventory

Server Name |FQDN | Operating System| Server IP Address | iDRAC IP Address | Domain| Model | Service Tag | Waranty status End Date

Thanks

PravinU.

0 Kudos