WinBatch® Technical Support Forum

Archived Boards => COM Automation and dotNet => Topic started by: seckner on January 28, 2016, 05:09:48 AM

Title: Automation using Powershell
Post by: seckner on January 28, 2016, 05:09:48 AM
Trying to get a script to run to export VM's for backup. Using lots of help from here and the database I have most, but not all, working, What does work:
This is run on the appropriate server. The script is named ExportVM.ps1 and this is the only line in the PS script:   Export-VM -Name Gateway -Path Z:\
This completes exporting the VM:

cRun=FileGet(cScript)
ObjectClrOption("use", "System.Management.Automation,version=1.0.0.0,publicKeyToken=31bf3856ad364e35,culture=neutral")
objAutoPs = ObjectClrNew("System.Management.Automation.PowerShell")
oPshell = objAutoPs.Create()
oScope = ObjectType("BOOL",@TRUE)
oPshell.AddScript(cRun,oScope)
objAsync = oPshell.BeginInvoke()
oPShell.EndInvoke(objAsync)     
oPshell.Dispose()
oPshell=0

The problem happens when changing the .ps1 script to be:  Get-VM | Export-VM ââ,¬â€œPath N:\
If I save the PS1 file and then call it as above I get this error:
COM/CLR Exception:    System.Management.Automation    The string is missing the terminator: ".
If I run Get-VM | Export-VM ââ,¬â€œPath N:\ directly inside powershell it runs to completion.

What am I missing? Why will it complete if I do one machine, but fail on get-vm where there is more than one machine to get?
Title: Re: Automation using Powershell
Post by: td on January 28, 2016, 07:27:52 AM
Just a guess but since you are using a file instead of building your PS commands on the fly using 'System.Management.Automation.PowerShell" methods, you may need and be missing a line-feed at the end of the line in your file.
Title: Re: Automation using Powershell
Post by: seckner on January 28, 2016, 11:45:41 AM
td - thank you. I've added the line feed but can't run the script during the day so I'll see what happens tonight. So that I don't appear to be a total dimwit, I went back to the database and searched for Powershell - I didn't see an example of what you referred to. I am using a file only because that's what I found in Stan and Deana's examples. If there's a better way please put on your teacher hat!
Title: Re: Automation using Powershell
Post by: td on January 28, 2016, 01:27:50 PM
You can find out about any FCL assembly and class on MSFT's site.  You can eve look at the source code, if you are so inclined. Anyway, here is a simple example that may help some:

Code (winbatch) Select
; Example script.
strScript = 'get-process |where {$_.Name -like $proc}'

; Boiler plate.
ObjectClrOption("use", "System.Management.Automation,version=1.0.0.0,publicKeyToken=31bf3856ad364e35,culture=neutral")
objAutoPs = ObjectClrNew("System.Management.Automation.PowerShell")
objPs     = objAutoPs.Create()

; Set the script varible.
objPs.AddCommand("Set-Variable");
objPs.AddParameter("name",  "proc");
objPs.AddParameter("value", "WinBatch*");
objPs.AddScript(strScript,ObjectType("BOOL",@TRUE))

; Invoke the script.
objAsync = objPs.BeginInvoke()
objPsCol = objPs.EndInvoke(objAsync)

; Display results.
nCount = objPsCol.Count - 1
for x = 0 to nCount
   objItem = objPsCol.Item(x)
    objCast = objItem.BaseObject()
   Message("Process Info", "Process name: ":objCast.ProcessName:@CRLF:"id: ":objCast.Id)
next
objPs.Dispose()
objPs=0

exit