Announcement

Collapse
No announcement yet.

ForEach - Remote Enumeration

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • ForEach - Remote Enumeration

    This may seem remedial - but I struggle with PS. Specifically with remote management.
    Need to enumerate (with size) profiles located on remote PCs
    The following script works on the local


    Local
    (
    this returns the desired data on local)

    dir c:\users|foreach-begin {} -process{
    $size=(dir$_.FullName -recurse-force-easilentlycontinue|Measure-Object‘length’-sum-Maximum).sum
    write-host("{0:n2}"-f ($size/1MB) +" MB",$_.fullname)

    }


    -------------------------------------------------


    Attempting to adjust for remote use:
    (This returns the directory name on local- no size)

    $computers =Read-Host -Prompt 'Input PC Name'
    $computers | foreach {
    Dir C:\users}
    $size=(dir $_.FullName -recurse -force -ea silentlycontinue | Measure-Object ‘length’ -sum -Maximum).sum
    write-host("{0:n2}" -f ($size/1MB) +" MB",$_.fullname)



    -------------------------------------------------------------------

    This returns remote directories - with no size


    $computers =Read-Host -Prompt 'Input PC Name'
    foreach ($computer in $computers) {
    dir "\\$computer\C$\users"}
    $size=(dir $_.FullName -recurse -force -ea silentlycontinue | Measure-Object ‘length’ -sum -Maximum).sum
    write-host("{0:n2}" -f ($size/1MB) +" MB",$_.fullname)




    As a side note- unable to output to file

    Out-File -output path "C:\temp\out.txt"
    Out-File "C:\temp\out.txt"
    >


    Have google extensively, however unable to get similar (or other) to work)


    Thank you in advance
    Last edited by LIC; 5th September 2018, 19:58.

  • #2
    Couple of things that i can think of at the moment, i don't have any remote systems i can run your code on to check.

    Use Get-ChildItem for your directory listing and add it to a variable you can then use that to get the information you need.

    Something like this would probably work.

    https://powershell.org/forums/topic/...file-versions/

    Comment


    • #3
      Wullieb- Appreciate

      This is where I get confused.


      I have edited- run several permutations of this in PowerShell ISE

      With no output or errors. Cannot step through the logic.


      $computers =Read-Host -Prompt 'Input PC Name'
      {foreach ($computer in $computers) {Get-ChildItem "C:\users\}
      $size=(Select-object $_.FullName -recurse -force -ea silentlycontinue | Measure-Object ‘length’ -sum -Maximum).sum
      write-host("{0:n2}" -f ($size/1MB) +" MB",$_.fullname)}





      Comment


      • #4
        Another attempt


        $computers =Read-Host -Prompt 'Input PC Name'

        Foreach ($Computer in $Computers)
        {
        dir "\\$computer\c$\users"

        $size=(dir $_.FullName -recurse -force -ea silentlycontinue | Measure-Object ‘length’ -sum -Maximum).sum
        write-host("{0:n2}" -f ($size/1MB) +" MB",$_.fullname)}

        Have also substituted

        Get-childItem "\\$computer\c$\users"
        and ChildItejm instead of dir



        9/6/2018 9:52 AM username
        d---- 3/30/2018 11:42 AM username
        d-r-- 10/26/2017 7:54 PM username
        d---- 12/29/2017 4:33 PM username



        Still need to figure out how to get the size portion working
        Last edited by LIC; 6th September 2018, 16:01.

        Comment


        • #5
          All:

          I did find the root of the issue.

          Due to certain restrictions - I cannot run the script with an elevated (Admin) account.

          Is there a way to Invoke-Command (or the like) to run as backup?

          Thanks


          Comment


          • #6
            Shamelessly stolen from here https://stackoverflow.com/questions/...ing-powershell

            Code:
            Param(
            [Parameter(ValueFromPipeline=$True, Mandatory=$True)]
            [Array] $Computers,
            [Parameter(ValueFromPipeline=$True, Mandatory=$True, ValueFromPipelineByPropertyName=$true)]
            [ValidateNotNullOrEmpty()]
            [System.String] $Path
            )
            
            Function Class-Size($size)
            {
            IF($size -ge 1GB)
            {
            "{0:n2}" -f  ($size / 1GB) + " GB"
            }
            ELSEIF($size -ge 1MB)
            {
            "{0:n2}" -f  ($size / 1MB) + " MB"
            }
            ELSE
            {
            "{0:n2}" -f  ($size / 1KB) + " KB"
            }
            } 
            
            function Get-FolderSize 
            {
            Param(
            $Path, [Array]$Computers
            ) 
            $Array = @()
            Foreach($Computer in $Computers)
                {
                $ErrorActionPreference = "SilentlyContinue"
            
            $Length = Invoke-Command -ComputerName $Computer -ScriptBlock {
             (Get-ChildItem $args[0] -Recurse | Measure-Object -Property Length -Sum).Sum
            
            } -ArgumentList $Path
            
            $Result = "" | Select Computer,Folder,Length
            $Result.Computer = $Computer
            $Result.Folder = $Path
            $Result.Length = Class-Size $length
            $array += $Result
            
            }
            
            return $array
            }
            
            Get-FolderSize -Computers $Computers -Path $Path
            To run it

            Code:
             
             .\Get-Size.ps1 -Computers Computer1,Computer2 -Path c:\windows\system32  Computer     Folder              Length  --------     ------              ------  Computer1    c:\windows\system32 2.96 GB Computer2    c:\windows\system32 3.43 GB

            Comment

            Working...
            X