Author Posts

January 29, 2014 at 7:33 pm

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"}
}
}
}

January 30, 2014 at 4:00 am

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
#>

January 31, 2014 at 7:49 am

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