VMware Cloud Community
marvinb
Enthusiast
Enthusiast
Jump to solution

getting a function to run in a script

I am a bit of a newbie. I have created a script vi.ps1 with the following content:

function myvi

When i fund the command from the command line it works as expected. a call to myvi brings up the vi editor

When I try to run it as a script, it just says that myvi is not recognized as a cmdlet.

I can run other scripts, but this one will not work correctly. The executionpolicy is unrestricted.

I am running the powercli.

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

In PS v1 you add all the functions you want to have available in one of your profile scripts.

When you start PS or the PowerCLI prompt this profile is executed and all the functions become available.

In PS v2 you can add all your functions to a module and then do a Get-Module in one of your profile scripts.


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

View solution in original post

0 Kudos
10 Replies
halr9000
Commander
Commander
Jump to solution

You are in the current directory where the script is located? You probably just need to prepend the filename with a dot-backslash like so:

.\myscript.ps1

That's a "feature". Since we don't have the concept of 'chmod +x' on Windows, this was seen as a mechanism for preventing the accidental execution of a script that was maliciously inserted into the current folder.

--

Hal Rottenberg / hal@halr9000.com<mailto:hal@halr9000.com> / halr9000.com<http://halr9000.com>

Microsoft MVP (PowerShell) / VMware vExpert

Co-Host, PowerScripting Podcast / Director, PowerShellCommunity.org

"Managing VMware Infrastructure with PowerShell: TFM", now shipping!

Follow me on Twitter: http://twitter.com/halr9000

My signature used to be pretty, but then the forum software broked it. vExpert. Microsoft MVP (Windows PowerShell). Author, Podcaster, Speaker. I'm @halr9000
0 Kudos
marvinb
Enthusiast
Enthusiast
Jump to solution

It has something to do with the function as I've tried the ./myscript.ps1

in fact, i discovered this problem when trying to get you get-parameter script to run from your book.

Then i tried this simple one and it didnt work.

0 Kudos
halr9000
Commander
Commander
Jump to solution

Yeah, I saw your post on my blog. I replied to it, by the way. Smiley Happy

Let's see the full error?

--

Hal Rottenberg / hal@halr9000.com<mailto:hal@halr9000.com> / halr9000.com<http://halr9000.com>

Microsoft MVP (PowerShell) / VMware vExpert

Co-Host, PowerScripting Podcast / Director, PowerShellCommunity.org

"Managing VMware Infrastructure with PowerShell: TFM", now shipping!

Follow me on Twitter: http://twitter.com/halr9000

My signature used to be pretty, but then the forum software broked it. vExpert. Microsoft MVP (Windows PowerShell). Author, Podcaster, Speaker. I'm @halr9000
0 Kudos
marvinb
Enthusiast
Enthusiast
Jump to solution

No error is generated.

0 Kudos
halr9000
Commander
Commander
Jump to solution

You mean you don't see an error that looks like this?

PS C:\Documents and Settings\hxr8765> myvi
The term 'c:\cygwin\bin\vim' is not recognized as a cmdlet, function, operable pro
gram, or script file. Verify the term and try again.
At C:\DOCUME~1\hxr8765\LOCALS~1\Temp\Untitled2.ps1:1 char:33
+ function myvi {c:\cygwin\bin\vim <<<<  $args[0]}
    + CategoryInfo          : ObjectNotFound: (c:\cygwin\bin\vim:String) [], Comm
   andNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException



[vExpert|http://www.vmware.com/communities/vexpert/], PowerShell MVP, VI Toolkit forum moderator

Author of the book: Managing VMware Infrastructure with PowerShell

Co-Host, PowerScripting Podcast (http://powerscripting.net)

Need general, non-VMware-related PowerShell Help? Try the forums at PowerShellCommunity.org

My signature used to be pretty, but then the forum software broked it. vExpert. Microsoft MVP (Windows PowerShell). Author, Podcaster, Speaker. I'm @halr9000
0 Kudos
marvinb
Enthusiast
Enthusiast
Jump to solution

Sorry.

When i execute the script that should define the function, it comes back with no errors.

This is teh sequence (I do get errors when i call myvi.

vSphere PowerCLI C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\myScripts&gt; ./vi.ps1

vSphere PowerCLI C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\myScripts&gt; myvi

The term 'myvi' is not recognized as a cmdlet, function, operable program, or script file. Verify the term and try again.

At line:1 char:4

+ myvi &lt;&lt;&lt;&lt;

vSphere PowerCLI C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\myScripts&gt;

Sorry, I"ve edited this answer a couple of times.

0 Kudos
marvinb
Enthusiast
Enthusiast
Jump to solution

I"m still trying to get functions to work. Could it be some sort of pathing issue. I can do this from the command line, just not from a script.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

I think you are mixing two things here.

For implementing what you want to do you can

1) call a script

In the vi.ps1 file you put

C:\cygwin\bin\vim $args[0]

and then you call the .ps1 file from a the PowerCLI prompt as such

.\vi.ps1 text.txt

2) call a function

In the .ps1 file you can call a function

function myvi{
  param($filename)

  c:\cygwin\bin\vim $filename
}

myvi $args[0]

and now you call the script in the same way

.\vi.ps1 text.txt

The reason why your script is not doing anything is because in the script itself the function is not called.

That also explains why there are no error messages.


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

marvinb
Enthusiast
Enthusiast
Jump to solution

I was under a misconception. When I define the function from the command line, it works as I anticipated. I was under the erroneous impression that the function, when executed from the script, became available to the shell, kinda like an alias defined in a bash profile. How could I set something up so that it would work from the cli.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

In PS v1 you add all the functions you want to have available in one of your profile scripts.

When you start PS or the PowerCLI prompt this profile is executed and all the functions become available.

In PS v2 you can add all your functions to a module and then do a Get-Module in one of your profile scripts.


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

0 Kudos