简体   繁体   中英

Get-ChildItem: Illegal characters in path

I'm trying to login to multiple servers and then get list of files from those servers.

Below is my script:

$ServerName=Get-content "D:\HOMEWARE\BLRMorningCheck\Jerry\servername.txt"
foreach ($server in $ServerName)
{
  $server_host=echo $server | %{$data = $_.split(";"); Write-Output "$($data[0])"}
  $Targetfolder=echo $server | %{$data = $_.split(";"); Write-Output "$($data[1])"}

  $Extension =@("*.log","*.txt*")

  $Files = Get-Childitem $TargetFolder -Include $Extension -Recurse 

  echo $Files
}

When I run I debug mode I see that it really doesnt pick the files.

Error:

Get-ChildItem : Illegal characters in path.
At D:\HOMEWARE\BLRMorningCheck\Jerry\test.ps1:14 char:23
+ $Files = Get-Childitem <<<<  $TargetFolder -Include $Extension -Recurse 
    + CategoryInfo          : InvalidArgument: (D:\HOMEWARE\BLR...ck\Jerry\Check":String) [Get-ChildItem], ArgumentException
    + FullyQualifiedErrorId : ItemExistsArgumentError,Microsoft.PowerShell.Commands.GetChildItemCommand

Get-ChildItem : Cannot find path 'D:\HOMEWARE\BLRMorningCheck\Jerry\"\srvparrtfh01\d$\HOMEWARE\BLRMorningCheck\Jerry\Check"' because it does not exist.
At D:\HOMEWARE\BLRMorningCheck\Jerry\test.ps1:14 char:23
+ $Files = Get-Childitem <<<<  $TargetFolder -Include $Extension -Recurse 
    + CategoryInfo          : ObjectNotFound: (D:\HOMEWARE\BLR...ck\Jerry\Check":String) [Get-ChildItem], ItemNotFoundException
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand

Note: If I run it manually, example

$Files = Get-Childitem \\servername\d$\HOMEWARE\BLRMorningCheck\Jerry\Check -Include "*.log","*.txt*" -Recurse

I get the output.

As @arco444 and @PetSerAl mentioned in the comments: your path string has leading/trailing double quotes, which are invalid characters in a path. You need to remove them to make the path work as intended:

$Files = Get-Childitem ($TargetFolder -replace '^"|"$') -Include $Extension -Recurse

Of course, since your input file seems to be a some sort of CSV, you could use Import-Csv instead of reading the file with Get-Content and manually splitting the fields. That would already take care of the double quotes on file import.

$filename  = 'D:\HOMEWARE\BLRMorningCheck\Jerry\servername.txt'
$extension = '*.log', '*.txt*'

Import-Csv $filename -Delimiter ';' -Header 'ComputerName', 'TargetFolder' | % {
  Get-Childitem $_.TargetFolder -Include $extension -Recurse 
}

This was an absolute nightmare, get-childitem cannot except a string variable if there are multiple paths.

Get-ChildItem -Path "\server1\c$\temp", "\server1\d$\temp" -File -recurse # works

$path = '"\server1\c$\temp", "\server1\d$\temp"'; Get-ChildItem -Path "\server1\c$\temp", "\server1\d$\temp" -File -recurse # fails with cannot find path

$path = "'"\server1\c$\temp", "\server1\d$\temp'""; Get-ChildItem -Path "\server1\c$\temp", "\server1\d$\temp" -File -recurse # fails with illegal character message (the tick)

Any string that has multiple paths fails, however an array with += as shown below will work.

$servers = @("server1", "server2");
$partialPaths = @("\c$\temp\", "\d$\temp\");
foreach ($server in $servers)
    {
        $paths = @();
        foreach ($partialPath in $partialPaths)
        {
            $paths += "\\" + $server + $partialPath;
        }
    }   
Get-ChildItem -Path $paths -File -recurse;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM