Confused about this script - regex split method and new-object error

This topic contains 3 replies, has 2 voices, and was last updated by Profile photo of Herbert Nicholas Herbert Nicholas 11 months, 3 weeks ago.

  • Author
  • #31879
    Profile photo of Herbert Nicholas
    Herbert Nicholas

    Hi there – i am studying workflow basics and looking at training from pluralsight/trainsignal specifically Jeff Hicks ' Powershell v3 New Features Deep Dive in the lesson on workflow and inlinescript i see the following script:

    Workflow Get-Admin{

    $properties=Inlinescript {
    $text= net user $using:name |
    where-object -filterscript {$_ -match "Password |Last Logon"}
    #define a regex of at least 2 spaces
    [regex]$rx="\s{2, }"
    foreach($item in $text) {
    #split each line
    $data = $rx.Split($item)
    } #foreach
    #write $hash as the output of InlineScript
    } #inlinescript

    $result = New-Object -type PSObject -property $properties

    } #endworkflow


    I am not understanding the $data = $rx.Split($item) part – why is he doing that? the output of net user "username" is already split ? not sure what the line he is using actually does . also when i try to run this script in ISE it complains about

    New-Object : Cannot convert 'System.Management.Automation.PSDataCollection`1[Syst
    em.Management.Automation.PSObject]' to the type 'System.Collections.IDictionary'
    required by parameter 'Property'. Specified method is not supported.

  • #31880
    Profile photo of Herbert Nicholas
    Herbert Nicholas

    also getting At line:2 char:1
    + Param=(
    + ~~~~~~~
    Cannot find the 'Param=' command. If this command is defined as a workflow, ensure it is defined before the workflow tha
    + CategoryInfo : ParserError: (:) [], ParseException
    + FullyQualifiedErrorId : CommandNotFound

  • #31882
    Profile photo of Bob McCoy
    Bob McCoy

    There should be no equal sign after Param in line 2.

    $rx is defined (and commented in his code) as "at least two spaces". So the split on $item occurs where it encounters at least two spaces. {2,} is a modifier that says minimum of 2, no maximum.

  • #31885
    Profile photo of Herbert Nicholas
    Herbert Nicholas

    Thank you sir for that – do you know why i am getting an error with the New-Object cmdlet?
    Also was looking at workflow sessions – i cannot understand why someone would run the following like:
    Invoke-Command -session $whatever scriptblock{do something}

    over My-workflow -paramater something -pscomputername $computers
    why do one over the other: you can run both with -asjob and -persist or whatever – and still suspend the job if its long running and get the results from a different session?

You must be logged in to reply to this topic.