10 responses to “A guide to PowerShell’s Advanced Functions”

  1. Klaus Schulte

    Dear Joel,

    firstOfAll: thank you very much for your powershell guides!!!
    I love to read through them … (if I’ve got the time) ...

    Just one question and a short comment:

    1) You specified: DefaultParameterSetName=“NoCreds”
    I’m no PS-expert … but shouldn’t there be a ParameterSet with the tag “NoCred” defined somewhere in the param-section?

    2) If I try to start a process with:

    [Diagnostics.Process]::Start("notepad.exe", $c.GetNetworkCredential().UserName, $c.Password, $c.GetNetworkCredential().Domain)

    ... that’s what your adv.function will issue, if I add a parameter ( $c = get-Credential ) to the call:
    “Start-Process “notepad.exe” -Credential $c”
    I’m receiving an exception

    PS C:\Dokumente und Einstellungen\Schulte> Start-Process “notepad.exe” -Credential $c
    Exception calling “Start” with “4” argument(s): “Der Verzeichnisname ist ungültig”
    At H:\Powershell\Start-Process.ps1:38 char:35
    + [Diagnostics.Process]::Start <<<< (
    + CategoryInfo : NotSpecified: ( :) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException

    That exception ( in english: “invalid directory name” ) occurs only, if I add the Credentials to the call. This works:
    Start-Process “notepad.exe”

    As I figured out, that’s a problem with the “working directory”, that you can set explicitly using the “StartProcessInfo” parameter as an overload to the Start method. Using C# this works:

    System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo();
    psi.Arguments = null;
    psi.Domain = "MyDomain";
    psi.FileName = @"c:\windows\System32\Notepad.exe";
    psi.Password = pwd;
    psi.UserName = "UserName";
    psi.UseShellExecute = false;
    psi.WorkingDirectory="C:\";
    System.Diagnostics.Process.Start(psi);

    Commenting out the psi.WorkingDirectory line results in the “invalid directory” exception!

    Strange but true :-(

    king regards, Klaus

  2. David Mohundro

    Nice guide – thanks for posting it! I really haven’t been taking advantage of what’s possible with advanced functions. So far, I’ve primarily been using it to require parameters (with [Parameter(Mandatory=$true)]). I need to try some more features out.

  3. David Mohundro

    Apparently I don’t even know my openid URL. Sorry about messing up that last comment.

  4. Jason Archer

    Thank you for writing this up, it was sorely needed.

  5. Chad Miller

    Modules and Advanced functions seem a lot clearer to me now —Thanks for the guide.

    I’ve also heard the term “library” used to describe function(s) which you source. Should “processutility.ps1” be “processutility.psm1” in 4a?

  6. Klaus Schulte

    Hello Joel,

    thank you very much for your reply!
    I won’t argue any more … but I’d like to add these lines and finish the discussion anyway. Things are sometimes not the way they should be and I can live with that :-)

    # A credential object: $c was created
    PS H:\Powershell> $c

    UserName                 Password
    DOMS\schulte            System.Security.SecureString

    # My CurrentDirectory is writeable (and readable) by my account
    PS H:\Powershell> out-file -inp "I can write to $([Environment]::CurrentDirectory)!" "$([Environment]::CurrentDirectory)\WriteToMe.txt"

    # I CAN'T start a process with my Credentials:
    PS H:\Powershell> [System.Diagnostics.Process]::Start("Notepad", $c.GetNetworkCredential().UserName, $c.Password, $c.GetNetworkCredential().Domain)

    Exception calling "Start" with "4" argument(s): "Der Verzeichnisname ist ungültig"
    At line:1 char:36
    + [System.Diagnostics.Process]::Start &lt;&lt;&lt; [System.Diagnostics.Process]::Start("Notepad")

    Handles  NPM(K)    PM(K)      WS(K) VM(M)   CPU(s)     Id ProcessName                                                            
    -------  ------    -----      ----- -----   ------     -- -----------                                                            
          0       0      140         84     1     0,02   4304 notepad                                                                
     

    • Life is NOT ALWAYS dair ******

    kind regards, Klaus

  7. Stuart Henderson

    Hi Jaykul,

    I think it was I who posted the message in the NG asking for clarification.

    As usual you come through with something far exceeding what I could have hoped for. I can’t thank you enough for the help you have provided me both in the NG and the IRC.

    I’m gonna email Jeffrey Snover every week till he gives you the MVP and backdates it a year Haha!!

    Thanks again,
    Stuart