Archived Boards > COM Automation and dotNet

Automation using Powershell


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:

ObjectClrOption("use", "System.Management.Automation,version=,publicKeyToken=31bf3856ad364e35,culture=neutral")
objAutoPs = ObjectClrNew("System.Management.Automation.PowerShell")
oPshell = objAutoPs.Create()
oScope = ObjectType("BOOL",@TRUE)
objAsync = oPshell.BeginInvoke()

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?

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.

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!

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 ---; Example script.
strScript = 'get-process |where {$_.Name -like $proc}'

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

; Set the script varible.
objPs.AddParameter("name",  "proc");
objPs.AddParameter("value", "WinBatch*");

; 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)



[0] Message Index

Go to full version