1

I have some problems with these lines:

Invoke-Command -ComputerName $computerObject.Name -ScriptBlock {'{0:0.00}' -f ((Get-ChildItem $path  -Recurse  | Measure-Object -Property Length -sum).sum)}

I'm getting nothing. But if I use instead of $path, c:\users\pa27dd7n\documents the output is 21830841828,00

The return of $path.gettype() is:

name:string      basetype: system.object

Current code:

$computers = Get-ADComputer -SearchBase  "OU=......"  -filter "*" -Properties name,description,OperatingSystem,OperatingSystemServicePack,OperatingSystemVersion  | sort-object description 

$ADUsers   = Get-ADUser -Filter * -SearchBase "OU=........" | Where-Object {$PSItem.enabled -eq "True"} |  Sort-Object name 

I add the property to the computers to keep login of the right user for the computer:

$computers | foreach {Add-Member -InputObject $PSItem -NotePropertyName MainUserSamAccountName -NotePropertyValue ""  -Force}

From $ADUsers I'm getting login of the user I need.

When I got everything:

$computerObject = $computers.Get($a) - in the loop

So, When I got that login:

$path = "c:\users\$computerObject.MainUserSamAccountName\documents"

This way I got the path of the directory users's documents.

GileBrt
  • 1,830
  • 3
  • 20
  • 28
mocarnik
  • 11
  • 3
  • 3
    `-ScriptBlock` is distinct, so if you defined `$path` somewhere else in your script it won't be in scope there. You need to pass `$path` to the scriptblock as an argument – arco444 Dec 08 '15 at 15:47
  • Where is $path defined? For example, what directories could we expect to be parsed? – FoxDeploy Dec 08 '15 at 15:50
  • As Arco444 says, if you want to pass a local variable into PowerShell remotely, use the `$using:` syntax. If you've got `$path` defined in your script and want it to be plugged in for the remote computers in this invoke-command block, change $path to `$using:path` instead. – FoxDeploy Dec 08 '15 at 16:01
  • Possible duplicate of [Invoke-command -ArgumentList parameter syntax](http://stackoverflow.com/questions/23761109/invoke-command-argumentlist-parameter-syntax). – Matt Dec 08 '15 at 16:37
  • Specifically look at [this answer](http://stackoverflow.com/a/23761457/3829407) as it shows how to pass arguments to the scriptblock. Else just lookup usage for `-ArgumentList` – Matt Dec 08 '15 at 16:38
  • I need to check how much space take up individual directories (eg. the documents, desktop, pictures, etc) on remote computers and make a report. I generate $path by finding the user login in AD user object (eg. $path = c:\usesr\LOGIN\documents) – mocarnik Jan 12 '16 at 21:53
  • Thamks Acro44 and Matt, it works :) – mocarnik Jan 12 '16 at 22:32

1 Answers1

0

If path is not defined, PowerShell will run Get-ChildItem on the current directory, so if you run this on a remote system, it would execute in the default directory (either C:\windows\system32 for Administrative users, or the user's profile for a user execution).

But I noticed that you're using the -f operator, which tends to burn people.

I dislike using the string -format operator, because the syntax is just so opaque to me, and hard for people to support. If what you'd really like is a report of the name of a directory and the size of all of the items, I'd much rather recommend emitting an object instead, as seen in this sample.

Invoke-Command -ComputerName $computerObject.Name -ScriptBlock {
[pscustomobject]@{DirectoryName=(Get-location).Path;
Size=((Get-ChildItem  $path -Recurse | Measure-Object -Property Length -sum).sum)/1MB -as [int]}

Result

DirectoryName     Size
-------------     ----
C:\Users\Stephen 16756

I went ahead and divided by 1MB to reduce the length of the number returned, then also cast as an Integer to round up. Hope this takes you in the right direction!

FoxDeploy
  • 12,569
  • 2
  • 33
  • 48
  • 1
    I still don't see how `$path` is getting defined here. Am I missing something? – arco444 Dec 08 '15 at 15:59
  • I don't know where you're defining Path. Are you defining it somewhere? I'm just offering a different way to present the same info you would be getting from your command. How about you update you post with your current code? – FoxDeploy Dec 08 '15 at 16:00