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.
CodePudding user response:
Get-Volume
also has a parameter CimSession
where you can give it a computername or an array of computernames. (Runs the cmdlet in a remote session or on a remote computer. Enter a computer name or a session object)
This lets you simplify your code to:
$props = @{Name = 'ComputerName'; Expression = {$_.PSComputerName}},
'DriveLetter','FileSystemLabel','FileSystem','DriveType','HealthStatus','OperationalStatus',
@{Name = 'SizeRemaining_GB'; Expression = {"{0:N2}" -f ($_.SizeRemaining/ 1Gb)}},
@{Name = 'Size_GB'; Expression = {"{0:N2}" -f ($_.Size / 1Gb)}},
@{Name = '% Free'; Expression = {"{0:P}" -f ($_.SizeRemaining / $_.Size)}}
$Diskmgmt = Get-Volume -CimSession server01, server02 | Select-Object $props | Sort-Object ComputerName, DriveLetter
CodePudding user response:
These are the Microsoft docs on running remote commands:
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:
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.