At a loss with Get-ChildItem behavior

This topic contains 2 replies, has 2 voices, and was last updated by  Bard 3 years, 7 months ago.

  • Author
    Posts
  • #12801

    Bard
    Participant

    Hi,

    I've been at this for a really long time and I'm at my wits end. My script is below. I'm trying to figure out why I'm not getting any objects returned from line 66 when I specify multiple filters in the -include parameter. The script works as expected if I only provide one item in the include filter. If I run the get-childitem cmdlet via my output from line 64 I get the expected number of objects back. Can someone please let me know why the behavior differs when I use the cmdlet in my script versus run get-childitem cmdlet outside my script?

    Function Copy-WebLogs
    {
    param (
    [validateSet("wsvcctt","unittest")]
    [string]$servertype,
    [string]$logName,
    [validateSet("Tomcat","IIS","AppLogs","all")]
    [string]$logType,
    [string]$target
    )

    Begin
    {
    $UNIT_TEST = @{servers="BGTMNWL-G4BZZL1.PEROOT.com";
    LogTypes="Tomcat";
    Tomcat="c$\testScripts";
    }
    switch ($servertype)
    {
    'wsvcctt' {
    $hash = $WEB_SERVICE_CTT
    }

    'unittest' {
    $hash = $UNIT_TEST
    }

    }
    }

    Process {
    $webs= $hash.servers

    switch ($logType) {
    'Tomcat' {
    $logDirectory = $hash.Tomcat
    }

    'IIS' {
    $logDirectory = $hash.IIS
    }

    'AppLogs' {
    $logDirectory = $hash.AppLogs
    }
    }

    < #Check to see if the $target path exists, if not create it. #>
    If (!(test-path -Path $target)) {
    New-Item -Path $target -ItemType Directory
    }

    foreach($web in $webs)
    {

    If (!(test-path -Path $target\$web))
    {
    New-Item -Path $target\$web -ItemType Directory -ErrorAction Stop
    }

    #below is line 68
    "Get-ChildItem -path \\$web\$logDirectory\* -Include $logName"
    #Below is line 70
    Get-ChildItem -path \\$web\$logDirectory\* -Include $logName | ForEach-Object {"Found stuff"}
    }
    }
    }

  • #12805

    Dave Wyatt
    Moderator

    What's in your $logName variable when it works, an when it does not?

    I suspect that the problem is that you've declared the $logName parameter as a [string], not [string[]]. When you want to pass multiple multiple values to the -Include parameter of Get-ChildItem, you do so by passing it an array of strings. Even if you're passing an array to your Copy-WebLogs function, PowerShell is converting that into a single String object because of how you declared $logName in your param block (which typically means you'll have a space-separate list, as seen here:)

    $variable = [string]('One','Two','Three','Four','Five')
    $variable.GetType().FullName
    
    $variable
    
    < #
    Output:
    
    System.String
    One Two Three Four Five
    #>
    
  • #12846

    Bard
    Participant

    Thanks Dave I really appreciate it. The problem was with my declaration of $logName – as you suspected.

You must be logged in to reply to this topic.