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,
PowershellShell ScriptingScripting Languages

Avatar of undefined
Last Comment
footech

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
footech

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
ASKER
ITguy565

@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
}
footech

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.
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23