Accessing a remote system and running
Get-WmiObject -ClassName Win32_LogicalDisk -ComputerName $computer
or
Get-CimInstance -ClassName Win32_LogicalDisk -ComputerName $computer
is a standard approach.
If you’re creating a function with that code in you may put the local machine as a default parameter:
$computer = $env:COMPUTERNAME
Running Get-WmiObject locally will work quite happily because you’re using COM to access the local machine.
Get-CimInstance may well fail with this error
PS> Get-CimInstance -ClassName Win32_LogicalDisk -ComputerName $computer
Get-CimInstance : The client cannot connect to the destination specified in the request. Verify that the service on the destination is running and is accepting requests. Consult the logs anddocumentation for the WS-Management service running on the destination, most commonly IIS or WinRM.
If the destination is the WinRM service, run the following command on the destination to analyze and configure the WinRM service: "winrm quickconfig".
At line:1 char:1
+ Get-CimInstance -ClassName Win32_LogicalDisk -ComputerName $computer
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ConnectionError: (root\cimv2:Win32_LogicalDisk:String) [Get-CimInstanc
e], CimException
+ FullyQualifiedErrorId : HRESULT 0x80338012,Microsoft.Management.Infrastructure.CimCmdlets.GetC
imInstanceCommand
+ PSComputerName : RSSURFACEPRO2
The CIM cmdlets use WSMAN to connect to remote machines. This is triggered by using the –ComputerName parameter. The error means you haven’t got the winrm service running on the local machine. On modern Windows remoting, and therefore winrm, are enable by default for servers but disable for client OS e.g. Windows 10.
Easiest way to get this to work is run Enable-PSremoting from and elevated prompt.