0

I have a simple PS script that counts files in 2 folders on remote machines and then exports to a CSV.

These are the lines that make it happen (note: all of the variables like $server, $folder1path, $folder2path are defined and legit)

invoke-command -computername $servers -scriptblock {
$servername = hostname
$infoObject = New-Object PSObject

$folder1count=(Get-ChildItem -recurse  -path $folder1path).count
$folder2count =(Get-ChildItem -recurse  -path $folder2path).count

if ($folder1count -ne 0 -or $folder2count -ne 0) {

Add-Member -inputObject $infoObject -memberType NoteProperty -name "ServerName" -value $Servername
Add-Member -inputObject $infoObject -memberType NoteProperty -name "Folder1" -value $folder1count
Add-Member -inputObject $infoObject -memberType NoteProperty -name "Folder2" -value $folder2count

$infoObject}} | Select-Object * -ExcludeProperty PSComputerName, RunspaceId, PSShowComputerName | Export-Csv -path "C:\temp\Test_count_folder.csv" -NoTypeInformation 

If I run the (Get-ChildItem -recurse -path $folder1path).count cmd on the local machine it returns the correct value.

The problem is that the scriptblock section consistently returns incorrect values ( 28 & 38?) even when the folders are empty.

What am I missing? It doesn't throw an error but it does output incorrect values.

dbutts
  • 17
  • 7
  • 1
    variables in an `Invoke-Command` scriptblock MUST be either passed in OR defined in the scriptblock. i don't see you passing in some of the $Vars in your scriptblock. – Lee_Dailey Apr 19 '21 at 15:35
  • @Lee_Dailey don't you think this question worths an answer on why and how to correct the script ? I agree that this one is somehow trivial, and was probably already asked, however, it seems to me that understanding why the remote script can't have the local scripts variable has some value. – Zilog80 Apr 19 '21 at 15:55
  • @Lee_Dailey Maybe the question is a little too case specific. Title should be corrected to something as "Why remote PS script doesn't work as expected". – Zilog80 Apr 19 '21 at 16:19
  • @Lee_Dailey For reference, i haven't found yet past question about variables context on remote session, even if there is past questions about how to properly pass parameters to remote PS session [1](https://stackoverflow.com/questions/38505414/execute-remote-ps-command-properly), [2](https://stackoverflow.com/questions/51934047/execute-a-remote-generic-powershell-script-with-generic-parameters), [3](https://stackoverflow.com/questions/28603718/how-to-pass-parameters-to-powershell-script-and-run-it-on-a-remote-computer). – Zilog80 Apr 19 '21 at 16:31
  • @Lee_Dailey Find ones that match this one i think : [1](https://stackoverflow.com/questions/63015482/powershell-remote-execution-of-console-application-fails) and [2](https://stackoverflow.com/questions/28837383/problems-using-local-variables-in-a-remote-commands). Let you flag it as duplicate if it's fit you. – Zilog80 Apr 19 '21 at 16:38
  • @Zilog80 - it looks like you posted an excellent Answer to the actual problem. _kool!_ [*grin*] – Lee_Dailey Apr 19 '21 at 19:42
  • @Lee_Daily It's more because pointing code corrections would have been tedious in the comments ;) Flag the question as duplicate and it will be deleted soon. – Zilog80 Apr 19 '21 at 19:56

1 Answers1

0

$folder1path and $folder2path aren't defined on the remote PowerShell session executed on the server $server. The remote session started on the server doesn't inherit variables nor environment from the local session, the local session can only get remote session output. Actually, your script is counting the files from the current directory of the remote PowerShell session as the variable $folder1path and $folder2path are here undefined thus "empty". You have to pass required local variables as command arguments to the script block of Ìnvoke-Command. You can do that by passing your local variables as arguments of the Invoke-Command with -ArgumentList and the param instruction :

$folder1path = "<path of folder 1>"
$folder2path = "<path of folder 2>"
invoke-command -computername $server -ArgumentList @($folder1path,$folder2path) -scriptblock {param($folder1path,$folder2path)
$servername = hostname
$infoObject = New-Object PSObject

$folder1count=(Get-ChildItem -recurse  -path $folder1path).count
$folder2count =(Get-ChildItem -recurse  -path $folder2path).count

if ($folder1count -ne 0 -or $folder2count -ne 0) {

Add-Member -inputObject $infoObject -memberType NoteProperty -name "ServerName" -value $Servername
Add-Member -inputObject $infoObject -memberType NoteProperty -name "Folder1" -value $folder1count
Add-Member -inputObject $infoObject -memberType NoteProperty -name "Folder2" -value $folder2count

$infoObject
}} | Select-Object * -ExcludeProperty PSComputerName, RunspaceId, PSShowComputerName | Export-Csv -path "C:\temp\Test_count_folder.csv" -NoTypeInformation 
Zilog80
  • 2,534
  • 2
  • 15
  • 20
  • aha! thanks so much! that solved it indeed! I did not think about passing those to the REMOTE nodes. – dbutts Apr 19 '21 at 16:59
  • @dbutts I suggest you to correct your title question with something more canonical like "Why remote PS script doesn't work as expected". Your question was probably already answered with the ones you can find in the comments. Searching SO with your question in canonical/generic form will give you better results on SO in the future. – Zilog80 Apr 19 '21 at 17:01