Link to home
Start Free TrialLog in
Avatar of ITguy565
ITguy565Flag for United States of America

asked on

Powershell scripting issue PS-Remoting

Experts,

What am I missing :

if I run the following , I am able to get the output I am looking for..

 enter-pssession -computername Computername
 $diskinfo = [System.IO.DriveInfo]::GetDrives()|select Name, AvailableFreeSpace, TotalSize
            $diskinfo |add-member -MemberType NoteProperty -Name PercentFree -value $null
            $diskinfo |add-member -MemberType NoteProperty -name ComputerName -value $ENV:Computername

            $diskinfo | ForEach-Object { 
                [float]$_.AvailableFreeSpace = $($_.AvailableFreeSpace/1GB)
                [float]$_.Totalsize = $($_.totalsize/1GB)
                [float]$_.PercentFree = (($($_.AvailableFreeSpace) / $($_.TotalSize)) * 100)

                 }

Open in new window





If I run :


$s = new-pssession -ComputerName Computername -name DiskInfo

$scriptblock = {$diskinfo = [System.IO.DriveInfo]::GetDrives()|select Name, AvailableFreeSpace, TotalSize
            $diskinfo |add-member -MemberType NoteProperty -Name PercentFree -value $null
            $diskinfo |add-member -MemberType NoteProperty -name ComputerName -value $ENV:Computername

            $diskinfo | ForEach-Object { 
                [float]$_.AvailableFreeSpace = $($_.AvailableFreeSpace/1GB)
                [float]$_.Totalsize = $($_.totalsize/1GB)
                [float]$_.PercentFree = (($($_.AvailableFreeSpace) / $($_.TotalSize)) * 100)

                 }
 }

 invoke-command -Session $s -ScriptBlock $scriptblock

 import-pssession -session $s

Open in new window



I get a lightshow..


What I am attempting to do is the following :


The script will be run from Computer1 and collect information for several other computers

The information will then be stored on computer1 and exported to CSV.

Can someone please show me where I went wrong with this : I am sure I am missing something simple

Thanks,
ASKER CERTIFIED SOLUTION
Avatar of footech
footech
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ITguy565

ASKER

@FooTech,

That was Exactly what I was looking for. Thanks!

final Code :

Function get-diskinfo($computername){

$scriptblock = {
    [System.IO.DriveInfo]::GetDrives() |
        Select Name,
    @{n = "AvailableFreeSpace"; e = {[float]$($_.AvailableFreeSpace / 1GB)}},
    @{n = "Totalsize"; e = {[float]$($_.totalsize / 1GB)}},
    @{n = "PercentFree"; e = {[float](($($_.AvailableFreeSpace) / $($_.TotalSize)) * 100)}}
}
invoke-command -ComputerName $computername -ScriptBlock $scriptblock |
    Select PScomputerName, TotalSize, AvailableFreeSpace, PercentFree -ExcludeProperty RunspaceId | Export-Csv driveinfo.csv -notype
}
Glad to help.

One note - since you're explicitly stating which properties you want to include in the final Select-Object command, you don't need to include the -ExcludeProperty parameter.