We help IT Professionals succeed at work.

Powershell scripting issue PS-Remoting

128 Views
Last Modified: 2020-09-17
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,
Comment
Watch Question

CERTIFIED EXPERT
Top Expert 2014
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
CERTIFIED EXPERT

Author

Commented:
@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
}
CERTIFIED EXPERT
Top Expert 2014

Commented:
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.
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.