Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Powershell Get-Volumn for remote computer

Below is the code I am executing for my local server to get the disk details

$props = @(
    'DriveLetter'
    'FileSystemLabel'
    'FileSystem'
    'DriveType'
    'HealthStatus'
    'OperationalStatus'
    @{n='SizeRemaining';e={"{0:N2}" -f ($_.SizeRemaining/ 1Gb)}}
    @{n='Size';e={"{0:N2}" -f ($_.Size / 1Gb)}}
    @{n='% Free';e={"{0:P}" -f ($_.SizeRemaining / $_.Size)}}
)

$Diskmgmt = Get-Volume | Select-Object $props 


foreach($dsk in $Diskmgmt)
{

    $dl = $dsk.DriveLetter
    $fsl = $dsk.FileSystemLabel
    $fs = $dsk.FileSystem
    $dt = $dsk.DriveType
    $hs = $dsk.HealthStatus
    $os = $dsk.OperationalStatus
    $sizer = $dsk.SizeRemaining
    $siz = $dsk.Size
    $PercentFree = $dsk.'% Free'
}

I need to connect to remote server and get the data.

Please let me know how to connect remote computer for this command.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

These are the Microsoft docs on running remote commands:

https://docs.microsoft.com/en-us/powershell/scripting/learn/remoting/running-remote-commands?view=powershell-7.2

It looks like it may just be as simple as making sure remote execution is enabled on the target computer and then running something like this:

Invoke-Command -ComputerName Server01, Server02 -ScriptBlock {Get-UICulture}

Or if you want to do it interactively, you can connect/disconnect using Enter-PSSession ServerName and Exit-PSSession which are also explained more in detail in the above documentation.

Edit:

You may also want to look into remote variables:

https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_remote_variables?view=powershell-7.2

Basically when you are executing that script block it does not have access to any local variables defined outside of that block unless you explicitly access them using the $using:myVariable sytanx.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading