-1

I am trying to read a list of computers from a .txt file, then look for a specific file named engage.* that exists in a specific directory on the pcs, then output the file info for engage.* to a csv file. However, when using the $computer variable in the get-childitem command, no data is outputted. Here is my script:

$computers = Get-Content -Path \scripts\pclist.txt  

foreach ($computer in $computers){

Get-ChildItem  -path "\\$computer\C$\Pfx Engagement\WM\Workpapers\*\engage.*" |
export-csv "c:\scripts\engage3.csv" -notypeinformation -encoding utf8
}

If I test by running the script for a specific pc name, it works great and outputs the file info to the .csv file:

Get-ChildItem  -path "\\0543834Laptop\C$\Pfx Engagement\WM\Workpapers\*\engage.*" |
export-csv "c:\scripts\engage2.csv" -notypeinformation -encoding utf8

The get-childitem cmd does not like using the $computer variable in the foreach loop. I cannot find anything after hrs of research that says you cannot use a variable in the get-childitem cmd. Is there something obvious I'm overlooking here? thanks for any suggestions.

hrs of googling and editing my code.

Theo
  • 57,719
  • 8
  • 24
  • 41
EBoone
  • 9
  • 1
  • A UNC path starts with two backslashes: `"\\$computer\C$...`. If you `echo $computer`, does it show you the names? – Tim Roberts May 10 '23 at 00:04
  • You're definitely overlooking showing us how the `.txt` file looks. – Destroy666 May 10 '23 at 00:29
  • Please [format your post properly](https://stackoverflow.com/help/formatting). – mklement0 May 10 '23 at 01:35
  • @TimRoberts, the actual code _does_ contain ``\\``, but the lack of proper formatting of the post hides that fact. – mklement0 May 10 '23 at 01:38
  • The problem is _not_ one of string interpolation. Try `$computer = 'server1'; Write-Output "\\$computer\C$\Pfx Engagement\WM\Workpapers\*\engage.*"`, for instance. The implication of your symptom is that the value of `$computer` isn't the name of an existing server (that has the administrative `C$` share enabled). – mklement0 May 10 '23 at 01:41

1 Answers1

-1

\ - escapes the character $, therefore, they use join-path

$disk = "c:\"
$folder = "temp"

$path = "$disk\$folder"  #dont work
$path = join-path $disk $folder #it works

You can use a workaround to avoid escaping:

$path = "\\" + $computer + "\C$\Pfx Engagement\WM\Workpapers*\engage.*"

Get-ChildItem -path $path | export-csv "c:\scripts\engage3.csv" -notypeinformation -encoding utf8 }
rinat gadeev
  • 114
  • 4
  • No, ``\`` has no special meaning in PowerShell (its escape character is `\``). Try `"\$HOME"` to see that `$HOME still expands. – mklement0 May 10 '23 at 11:53
  • this is true, although I remember for sure that it caused problems before – rinat gadeev May 11 '23 at 05:38
  • I'm not aware of any such issues. The only issue with ``\`` - which is unrelated to string expansion (interpolation) - is that Windows PowerShell neglects to escape a trailing ``\`` when passing arguments with spaces to external programs (fixed in PowerShell (Core) 7+). – mklement0 May 11 '23 at 07:21