Using the library from a command line

Jan 21, 2009 at 8:52 PM
I'd like to invoke this very useful library from a command line:

C:\Hyper-V PowerShell>powershell.exe -command ". ./hyperv.ps1; Suspend-VM blah"

I'm looking to leverage the functions with automation (read: unattended scripts).  I get the following message:

Security Warning
Run only scripts that you trust. While scripts from the Internet can be useful,
 this script can potentially harm your computer. Do you want to run C:\Hyper-V
PowerShell\hyperv.ps1?
[D] Do not run  [R] Run once  [S] Suspend  [?] Help (default is "D"):

If I go ahead and press R, I'm golden.  Otherwise it just hangs.

Is there a way to use this library from a command line without the prompt?

Many thanks,
Blake
Jan 22, 2009 at 1:41 PM
I think I have found a work-around on this:

http://www.hanselman.com/blog/RemovingSecurityFromDownloadedPowerShellScriptsWithAlternativeDataStreams.aspx

I clicked the unblock button the file properties.  Neato.

Blake
Jan 22, 2009 at 2:50 PM
Followup - I want to run this as a batch file or scheduled task, which may not run in the same directory.  How can I load the function library and then run commands in a single command line?

Blake
Jan 23, 2009 at 2:41 PM
I'm looking at this now.  I'm having similar problems to others, and am trying to find the time to look at the source.

New-VM works fine, but other functions/filters like Ping-VM or Get-VM return nothing.

Marco
Windows PowerShell MVP
Jan 26, 2009 at 3:27 PM
After unblocking the downloaded file, I run the command from a CMD (thanks, Marco, for your help with the syntax) and get this:

C:\Users\Administrator>powershell.exe "& {. 'C:\PSHyperv\hyperv.ps1';Start-VM CALMACIL}"
No functions were loaded - you need to invoke with . scriptname
Changing state of CALMACIL: Job Started.
file://\\VALACAR\root\virtualization:Msvm_ConcreteJob.InstanceID=13C-FE8320AB66B7"

This command line works - but the error I get is kind of perplexing.  Just an FYI.


Coordinator
Jan 26, 2009 at 4:25 PM
Marco when Get-VM retursn nothing it is usually a permissions thing - running as a member of admins, but not THE administrator - V1.0 has a trap for that.

At a vanilla PowerShell prompt you can try get-wmiObject -namespace root\virtualization -class msvm_computersystem - if it doesn't return any VMs - just an object for the host then nothing in the library will do much ....
Coordinator
Jan 26, 2009 at 4:48 PM
Marco I've picked this up over at http://www.codeplex.com/PSHyperv/WorkItem/View.aspx?WorkItemId=3677 .
Can you check what is coming back in the caption property of the VMs, cheers James.
Jan 26, 2009 at 6:57 PM
blaked,

Regarding this
C:\Users\Administrator>powershell.exe "& {. 'C:\PSHyperv\hyperv.ps1';Start-VM CALMACIL}"
No functions were loaded - you need to invoke with . scriptname


This is possibly where things get touchy...

If you create a test.ps1 script with only:
$myinvocation.line

If you call it like:
powershell.exe "& {. ./test.ps1}"
or
powershell.exe ". './test.ps1'"

You'll get different results.  So how does one check every possible combination to make sure that the scirpt has been dot-sourced?

There are some less neat examples of testing whether something has been added to the global or current scope...  Something to think about...

Coordinator
Jan 29, 2009 at 11:51 PM
Marco, I'd suggest that someone who plans to load the scripts in that way ignores the error or edits it out - it there to cut down the number of mails I get from people new to powershell who haven't been told about dot sourcing.

That said there is probably a regular expression which will do the trick