Using the library from a command line

Jan 21, 2009 at 9: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
[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,
Jan 22, 2009 at 2:41 PM
I think I have found a work-around on this:

I clicked the unblock button the file properties.  Neato.

Jan 22, 2009 at 3: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?

Jan 23, 2009 at 3: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.

Windows PowerShell MVP
Jan 26, 2009 at 4: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.

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

Jan 26, 2009 at 5: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 ....
Jan 26, 2009 at 5:48 PM
Marco I've picked this up over at .
Can you check what is coming back in the caption property of the VMs, cheers James.
Jan 26, 2009 at 7:57 PM

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:

If you call it like:
powershell.exe "& {. ./test.ps1}"
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...

Jan 30, 2009 at 12:51 AM
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