Windows Vista Forums

Functions and how to use them
  1. #1


    OldDog Guest

    Functions and how to use them

    Hi,

    I found this function on Marco Shaw's blog.

    Get-SysInternals

    I copied over and saved it as Get-SysInternals.ps1. Now I don't know
    how to run it.

    If I call it directly, I get this;

    PS C:\Scripts\PS1> Get-SysInternals
    The term 'Get-SysInternals' is not recognized as a cmdlet, function,
    operable p
    rogram, or script file. Verify the term and try again.
    At line:1 char:16
    + Get-SysInternals <<<<
    PS C:\Scripts\PS1> Get-SysInternals.ps1
    The term 'Get-SysInternals.ps1' is not recognized as a cmdlet,
    function, operab
    le program, or script file. Verify the term and try again.
    At line:1 char:20
    + Get-SysInternals.ps1 <<<<

    And here it is;

    function Get-SysInternals {

    param ( $sysIntDir="C:\PSTools\" )

    if( !$sysIntDir.endsWith("\")) { $sysIntDir+="\" }
    $log = join-path $sysIntDir "changes.log"
    add-content -force $log -value "`n`n[$(get-date)]SysInternals sync
    has started"

    dir \\live.sysinternals.com\tools -recurse | foreach {



    $fileName = $_.name
    $localFile = join-path $sysIntDir $_.name
    $exist = test-path $localFile

    $msgNew = "new utility found: $fileName , downloading..."
    $msgUpdate = "file : $fileName is newer, updating..."
    $msgNoChange = "nothing changed for: $fileName"


    if($exist){

    if($_.lastWriteTime -gt (get-item
    $localFile).lastWriteTime){
    copy-item $_.fullname $sysIntDir -force
    write-host $msgUpdate -fore yellow
    add-content -force $log -value $msgUpdate
    } else {
    add-content $log -force -value $msgNoChange
    write-host $msgNoChange
    }

    } else {

    if($_.extension -eq ".exe") {
    write-host $msgNew -fore green
    add-content -force $log -value $msgNew
    }

    copy-item $_.fullname $sysIntDir -force
    }
    }
    }


    I am sure it's simple, but then so am I.

    OldDog

      My System SpecsSystem Spec

  2. #2


    tojo2000 Guest

    Re: Functions and how to use them

    On Sep 18, 3:01*pm, OldDog <mikef2...@xxxxxx> wrote:

    > Hi,
    >
    > I found this function on Marco Shaw's blog.
    >
    > Get-SysInternals
    >
    > I copied over and saved it as Get-SysInternals.ps1. Now I don't know
    > how to run it.
    >
    > If I call it directly, I get this;
    >
    > PS C:\Scripts\PS1> Get-SysInternals
    > The term 'Get-SysInternals' is not recognized as a cmdlet, function,
    > operable p
    > rogram, or script file. Verify the term and try again.
    > At line:1 char:16
    > + Get-SysInternals <<<<
    > PS C:\Scripts\PS1> Get-SysInternals.ps1
    > The term 'Get-SysInternals.ps1' is not recognized as a cmdlet,
    > function, operab
    > le program, or script file. Verify the term and try again.
    > At line:1 char:20
    > + Get-SysInternals.ps1 <<<<
    >
    > And here it is;
    >
    > function Get-SysInternals {
    >
    > * *param ( $sysIntDir="C:\PSTools\" )
    >
    > * *if( !$sysIntDir.endsWith("\")) { $sysIntDir+="\" }
    > * *$log = join-path $sysIntDir "changes.log"
    > * *add-content -force $log -value "`n`n[$(get-date)]SysInternals sync
    > has started"
    >
    > * * * dir \\live.sysinternals.com\tools -recurse | foreach {
    >
    > * * * * *$fileName = $_.name
    > * * * * *$localFile = join-path $sysIntDir $_.name
    > * * * * *$exist = test-path $localFile
    >
    > * * * * *$msgNew = "new utility found: $fileName , downloading..."
    > * * * * *$msgUpdate = "file : $fileName *is newer, updating...."
    > * * * * *$msgNoChange = "nothing changed for: $fileName"
    >
    > * * * * *if($exist){
    >
    > * * * * * * if($_.lastWriteTime -gt (get-item
    > $localFile).lastWriteTime){
    > * * * * * * * *copy-item $_.fullname $sysIntDir -force
    > * * * * * * * *write-host $msgUpdate -fore yellow
    > * * * * * * * *add-content -force $log -value $msgUpdate
    > * * * * * * } else {
    > * * * * * * * *add-content $log -force -value $msgNoChange
    > * * * * * * * *write-host $msgNoChange
    > * * * * * * }
    >
    > * * * * * } else {
    >
    > * * * * * * * *if($_.extension -eq ".exe") {
    > * * * * * * * * * write-host $msgNew -fore green
    > * * * * * * * * * add-content -force $log -value $msgNew
    > * * * * * * * *}
    >
    > * * * * * * * *copy-item $_.fullname $sysIntDir -force
    > * * * * *}
    > * *}
    >
    > }
    >
    > I am sure it's simple, but then so am I.
    >
    > OldDog
    If you want to be able to use the function you'll have to dotsource
    the script. I don't know what they were thinking when they came up
    with dotsourcing, but here's how it works.

    Normally when you call the script, the function is created, but it is
    created in the script's scope, so when the script ends and the scope
    is destroyed, so is the function.

    But if you do this:

    PS C:\> . .\Get-Sysinternals.ps1 # Notice the extra dot

    Then the contents of the script will be executed in your current scope
    instead, so the function and any variables created will still be
    around when the script finishes executing.

    Then you can use the Get-Sysinternals function whenever you want.

      My System SpecsSystem Spec

  3. #3


    OldDog Guest

    Re: Functions and how to use them

    On Sep 19, 6:41*am, OldDog <mikef2...@xxxxxx> wrote:

    > On Sep 18, 5:23*pm, tojo2000 <tojo2...@xxxxxx> wrote:
    >
    >
    >
    >
    >

    > > On Sep 18, 3:01*pm, OldDog <mikef2...@xxxxxx> wrote:
    >

    > > > Hi,
    >

    > > > I found this function on Marco Shaw's blog.
    >

    > > > Get-SysInternals
    >

    > > > I copied over and saved it as Get-SysInternals.ps1. Now I don't know
    > > > how to run it.
    >

    > > > If I call it directly, I get this;
    >

    > > > PS C:\Scripts\PS1> Get-SysInternals
    > > > The term 'Get-SysInternals' is not recognized as a cmdlet, function,
    > > > operable p
    > > > rogram, or script file. Verify the term and try again.
    > > > At line:1 char:16
    > > > + Get-SysInternals <<<<
    > > > PS C:\Scripts\PS1> Get-SysInternals.ps1
    > > > The term 'Get-SysInternals.ps1' is not recognized as a cmdlet,
    > > > function, operab
    > > > le program, or script file. Verify the term and try again.
    > > > At line:1 char:20
    > > > + Get-SysInternals.ps1 <<<<
    >

    > > > And here it is;
    >

    > > > function Get-SysInternals {
    >

    > > > * *param ( $sysIntDir="C:\PSTools\" )
    >

    > > > * *if( !$sysIntDir.endsWith("\")) { $sysIntDir+="\" }
    > > > * *$log = join-path $sysIntDir "changes.log"
    > > > * *add-content -force $log -value "`n`n[$(get-date)]SysInternals sync
    > > > has started"
    >

    > > > * * * dir \\live.sysinternals.com\tools -recurse | foreach {
    >

    > > > * * * * *$fileName = $_.name
    > > > * * * * *$localFile = join-path $sysIntDir $_.name
    > > > * * * * *$exist = test-path $localFile
    >

    > > > * * * * *$msgNew = "new utility found: $fileName , downloading..."
    > > > * * * * *$msgUpdate = "file : $fileName *is newer, updating..."
    > > > * * * * *$msgNoChange = "nothing changed for: $fileName"
    >

    > > > * * * * *if($exist){
    >

    > > > * * * * * * if($_.lastWriteTime -gt (get-item
    > > > $localFile).lastWriteTime){
    > > > * * * * * * * *copy-item $_.fullname $sysIntDir -force
    > > > * * * * * * * *write-host $msgUpdate -fore yellow
    > > > * * * * * * * *add-content -force $log -value $msgUpdate
    > > > * * * * * * } else {
    > > > * * * * * * * *add-content $log -force -value $msgNoChange
    > > > * * * * * * * *write-host $msgNoChange
    > > > * * * * * * }
    >

    > > > * * * * * } else {
    >

    > > > * * * * * * * *if($_.extension -eq ".exe") {
    > > > * * * * * * * * * write-host $msgNew -fore green
    > > > * * * * * * * * * add-content -force $log -value $msgNew
    > > > * * * * * * * *}
    >

    > > > * * * * * * * *copy-item $_.fullname $sysIntDir -force
    > > > * * * * *}
    > > > * *}
    >

    > > > }
    >

    > > > I am sure it's simple, but then so am I.
    >

    > > > OldDog
    >

    > > If you want to be able to use the function you'll have to dotsource
    > > the script. *I don't know what they were thinking when they came up
    > > with dotsourcing, but here's how it works.
    >

    > > Normally when you call the script, the function is created, but it is
    > > created in the script's scope, so when the script ends and the scope
    > > is destroyed, so is the function.
    >

    > > But if you do this:
    >

    > > * PS C:\> . .\Get-Sysinternals.ps1 *# Notice the extra dot
    >

    > > Then the contents of the script will be executed in your current scope
    > > instead, so the function and any variables created will still be
    > > around when the script finishes executing.
    >

    > > Then you can use the Get-Sysinternals function whenever you want.
    >
    > Two dots? I would have never guessed that. I'll give it a try and
    > thanks.
    >
    > OldDog- Hide quoted text -
    >
    > - Show quoted text -
    Nope, still does not work.

    C:\Program Files\PowerGUI> ..\Get-SysInternals
    The term '..\Get-SysInternals' is not recognized as a cmdlet,
    function, operable program, or script file. Verify the term and try
    again.

    C:\Program Files\PowerGUI> ..\Get-SysInternals.ps1
    The term '..\Get-SysInternals.ps1' is not recognized as a cmdlet,
    function, operable program, or script file. Verify the term and try
    again.


      My System SpecsSystem Spec

  4. #4


    Ozone Guest

    Re: Functions and how to use them

    Put a space between the dots. As you have it, the shell is looking in
    the folder one level up, and the script can't be located. To dot
    source your script there should be a space between the two dots...

    HTH
    Ozone

      My System SpecsSystem Spec

  5. #5


    OldDog Guest

    Re: Functions and how to use them

    On Sep 19, 9:11*am, Ozone <rs_dov...@xxxxxx> wrote:

    > Put a space between the dots. *As you have it, the shell is looking in
    > the folder one level up, and the script can't be located. *To dot
    > source your script there should be a space between the two dots...
    >
    > HTH
    > Ozone
    That was fun. Now I get another error;

    C:\Scripts\PS1> . .\Get_SysInternals.ps1
    Exception calling "SetShouldExit" with "1" argument(s): "Object
    reference not set to an instance of an object."

    Notice that "SetShouldExit" is not part of the Function.

      My System SpecsSystem Spec

  6. #6


    OldDog Guest

    Re: Functions and how to use them

    On Sep 19, 9:32*am, OldDog <mikef2...@xxxxxx> wrote:

    > On Sep 19, 9:11*am, Ozone <rs_dov...@xxxxxx> wrote:
    >

    > > Put a space between the dots. *As you have it, the shell is looking in
    > > the folder one level up, and the script can't be located. *To dot
    > > source your script there should be a space between the two dots...
    >

    > > HTH
    > > Ozone
    >
    > That was fun. Now I get another error;
    >
    > C:\Scripts\PS1> . .\Get_SysInternals.ps1
    > Exception calling "SetShouldExit" with "1" argument(s): "Object
    > reference not set to an instance of an object."
    >
    > Notice that "SetShouldExit" is not part of the Function.
    OK, Its the whole function thing that has mu confused.

    If I remove the

    function Get-SysInternals {

    param (

    The trailing ) after $sysIntDir="C:\PSTools\" and the last } and save
    it as Get_SysInternals.ps1 it works just fine.

    So I know its not the code. I just don't know how to call a function
    in PowerShell. So I guess my question is,

    How do you call a Function in PowerShell.

    OldDog

      My System SpecsSystem Spec

  7. #7


    OldDog Guest

    Re: Functions and how to use them

    On Sep 19, 9:56*am, OldDog <mikef2...@xxxxxx> wrote:

    > On Sep 19, 9:32*am, OldDog <mikef2...@xxxxxx> wrote:
    >
    >
    >
    >
    >

    > > On Sep 19, 9:11*am, Ozone <rs_dov...@xxxxxx> wrote:
    >

    > > > Put a space between the dots. *As you have it, the shell is lookingin
    > > > the folder one level up, and the script can't be located. *To dot
    > > > source your script there should be a space between the two dots...
    >

    > > > HTH
    > > > Ozone
    >

    > > That was fun. Now I get another error;
    >

    > > C:\Scripts\PS1> . .\Get_SysInternals.ps1
    > > Exception calling "SetShouldExit" with "1" argument(s): "Object
    > > reference not set to an instance of an object."
    >

    > > Notice that "SetShouldExit" is not part of the Function.
    >
    > OK, Its the whole function thing that has mu confused.
    >
    > If I remove the
    >
    > function Get-SysInternals {
    >
    > * *param (
    >
    > The trailing ) after $sysIntDir="C:\PSTools\" and the last } and save
    > it as Get_SysInternals.ps1 it works just fine.
    >
    > So I know its not the code. I just don't know how to call a function
    > in PowerShell. So I guess my question is,
    >
    > How do you call a Function in PowerShell.
    >
    > OldDog- Hide quoted text -
    >
    > - Show quoted text -
    Correction, I found this Function on Shay Levy's Blog
    http://blogs.microsoft.co.il/blogs/s...c/contact.aspx

      My System SpecsSystem Spec

  8. #8


    Shay Levy [MVP] Guest

    Re: Functions and how to use them

    Hi OldDog,


    You can copy the function as is into your $profile, restart powershell and
    call the function from the console:

    PS > Get_SysInternals

    Or with its parameters:

    PS > Get_SysInternals -sysIntDir="d:\foo"



    You can also paste the function into a ps1 file and dot-sorce:

    # it the script is in the working directory
    PS > . .\Get_SysInternals.ps1

    # from the full path to the file
    PS > . c:\scripts\Get_SysInternals.ps1

    Now you can call it the same way as above.



    Another option is to remove the 'function Get-SysInternals {' line and the
    last '}' (e.g at the bottom of the script), save it to a ps1 file
    and call it like so:


    PS > .\Get_SysInternals.ps1 -sysIntDir="d:\foo"


    ---
    Shay Levy
    Windows PowerShell MVP
    http://blogs.microsoft.co.il/blogs/ScriptFanatic



    O> On Sep 19, 9:32 am, OldDog <mikef2...@xxxxxx> wrote:
    O>

    >> On Sep 19, 9:11 am, Ozone <rs_dov...@xxxxxx> wrote:
    >>

    >>> Put a space between the dots. As you have it, the shell is looking
    >>> in the folder one level up, and the script can't be located. To dot
    >>> source your script there should be a space between the two dots...
    >>>
    >>> HTH
    >>> Ozone
    >> That was fun. Now I get another error;
    >>
    >> C:\Scripts\PS1> . .\Get_SysInternals.ps1
    >> Exception calling "SetShouldExit" with "1" argument(s): "Object
    >> reference not set to an instance of an object."
    >> Notice that "SetShouldExit" is not part of the Function.
    >>
    O> OK, Its the whole function thing that has mu confused.
    O>
    O> If I remove the
    O>
    O> function Get-SysInternals {
    O>
    O> param (
    O>
    O> The trailing ) after $sysIntDir="C:\PSTools\" and the last } and save
    O> it as Get_SysInternals.ps1 it works just fine.
    O>
    O> So I know its not the code. I just don't know how to call a function
    O> in PowerShell. So I guess my question is,
    O>
    O> How do you call a Function in PowerShell.
    O>
    O> OldDog
    O>



      My System SpecsSystem Spec

Functions and how to use them problems?

Similar Threads
Thread Thread Starter Forum Replies Last Post
Functions and Objects ScotsBoy69 PowerShell 1 22 Jan 2009
Fun with functions casey.daniell PowerShell 4 03 Apr 2008
Cut & paste functions Scott Vista General 1 19 Feb 2007
Where are the functions? AiAidevil Vista General 5 03 Feb 2007
Functions Fil PowerShell 4 22 Nov 2006