ForEach – Remote Enumeration

Home Forums Scripting PowerShell ForEach – Remote Enumeration

This topic contains 5 replies, has 3 voices, and was last updated by  ljgodzilla 2 months, 1 week ago.

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts

  • LIC
    Member
    #167697

    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)

    [SIZE=11px]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)[/SIZE]
    [SIZE=8px][SIZE=8px]} [/SIZE][/SIZE]


    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 “\$computerC$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:tempout.txt”
    Out-File “C:tempout.txt”
    >

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

    Thank you in advance


    wullieb1
    Participant
    #245819

    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/running-get-childitem-on-remote-server-to-check-file-versions/


    ljgodzilla
    Member
    #391261

    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)}


    ljgodzilla
    Member
    #391262

    Another attempt

    $computers =Read-Host -Prompt ‘Input PC Name’

    Foreach ($Computer in $Computers)
    {
    dir “\$computerc$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 “\$computerc$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


    ljgodzilla
    Member
    #391263

    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


    wullieb1
    Participant
    #245821

    Shamelessly stolen from here https://stackoverflow.com/questions/32393552/get-folder-size-on-a-remote-server-using-powershell

    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[/CODE]

    To run it

    [CODE]
    .Get-Size.ps1 -Computers Computer1,Computer2 -Path c:windowssystem32 Computer Folder Length ——– —— —— Computer1 c:windowssystem32 2.96 GB Computer2 c:windowssystem32 3.43 GB [/CODE][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[/CODE]

    To run it

    .Get-Size.ps1 -Computers Computer1,Computer2 -Path c:windowssystem32 Computer Folder Length ——– —— —— Computer1 c:windowssystem32 2.96 GB Computer2 c:windowssystem32 3.43 GB [/CODE][CODE]
    .Get-Size.ps1 -Computers Computer1,Computer2 -Path c:windowssystem32 Computer Folder Length




    Computer1 c:windowssystem32 2.96 GB Computer2 c:windowssystem32 3.43 GB [/CODE]

Viewing 6 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic.