VMware Cloud Community
Antony13
Contributor
Contributor
Jump to solution

VBScript / PowerShell - Output Power On / Shutdown commands to text file for logging purposes

Hi there,

I'm trying to do what I think is a relatively simple thing that is, use Powershell to run a Power on / Shutdown command on a VM, and then log the output that happens when that Powershell script runs (as it contains information on whether that operation was successful or not). I know more VBscript than I do Powershell, so I'm using VBscript to do all the file type operations (logging, sending emails etc), and then calling Powershell scripts to actually do the power off / power on.

What I can't figure out is how you run the powershell script, and get it to log what the output is. I'm calling the below powershell from the VBscript (for example to power on):

powershell.exe -noexit -file " & constScriptPath & "\start.ps1 strVMName

Where start.ps1 is:

# Uncomment line below when running as a scheduled task

Add-PSSnapin VMware.VimAutomation.Cloud

# Connect to Cloud Provider

Connect-CIServer -server <servername> -User <username> -Password <password> -Org <org id>

# Carry out the PowerOn task

write-host 'Starting... ' $args

Get-CIVM -Name $args | Start-CIVM | Out-File result.txt

Start-Sleep 10

# Exit Console Window when done

if ($Host.Name -eq 'ConsoleHost') {

Stop-Process $PID


}

As you can see, I have the 'Out-File result.txt' line piped in after the command to start the VM, and this does indeed create a text file with the output of the command. If I open this in Notepad, I can see the text fine, but if I try and read it by opening the file within the VBscript and reading it there, I just get a load of really strange characters and white space, and nothing else. Any ideas what's going on? Or any ideas on how I can do this better? This is what I can see if I open the file in Notepad:


WARNING: 2 columns do not fit into the display and were removed.

Name                           Status                      GuestOSFullName    
----                           ------                      ---------------    
<VM Name>                PoweredOn                   Microsoft Windows...


And this is the weird text I get when the VBscript reads the file:

਍圀䄀刀一䤀一䜀㨀 ㈀ 挀漀氀甀洀渀猀 搀漀 渀漀琀 昀椀琀 椀渀琀漀 琀栀攀 搀椀猀瀀氀愀礀 愀渀搀 眀攀爀攀 爀攀洀漀瘀攀搀⸀ഀഀ

਍一愀洀攀                           匀琀愀琀甀猀                      䜀甀攀猀琀伀匀䘀甀氀氀一愀洀攀     ഀഀ

----                           ------                      ---------------  

਍匀䠀匀䤀䰀㈀ⴀ圀匀嘀吀倀  㐀                倀漀眀攀爀攀搀伀渀                   䴀椀挀爀漀猀漀昀琀 圀椀渀搀漀眀猀⸀⸀⸀ഀഀ

਍ഀഀ

I assume this is to do with that initial warning about 2 columns do not fit, but I'm not really too clear on what this means or how to fix it. Has anyone seen anything like this?

BTW - fully appreciate I need to move from VBscript to fully use Powershell, but I'm struggling with finding the time to do this, and really need to get a solution up and running pretty quickly for this particular issue, and one that I am okay to support!

1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Try adding the parameter "-encoding ASCII" on the Out-File cmdlet


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

View solution in original post

2 Replies
LucD
Leadership
Leadership
Jump to solution

Try adding the parameter "-encoding ASCII" on the Out-File cmdlet


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

Antony13
Contributor
Contributor
Jump to solution

Legend! Easy when you know how!

Thanks so much, been wrestling with this for a few days, knew I should have asked for help sooner Smiley Happy

0 Kudos