Powershell showing disk space details of inaccessible drives - windows

I tried to retrieve the disk space details of a remote server A from another server B and used the below PowerShell command:
Get-WmiObject -Class Win32_LogicalDisk -ComputerName aa.bb.cc.dd -filter "DriveType=3" | Select DeviceID,#{Name="Size(GB)";Expression={"{0:N1}" -f($_.size/1gb)}},#{Name="FreeSpace(GB)";Expression={"{0:N3}" -f($_.freespace/1gb)}}
The output is as follows:
DeviceID Size(GB) FreeSpace(GB)
-------- -------- -------------
C: 59.9 17.080
D: 20.0 0.875
F: 100.0 81.865
In server A, Drive D is inaccessible. I can't see the drive space details in My computer. I checked the drive space details in Disk Management of server A and found that the above values are correct.
Again I went to server A and ran the following command in its own PowerShell window:
Get-WmiObject -Class Win32_LogicalDisk -filter "DriveType=3" | Select DeviceID,#{Name="Size(GB)";Expression={"{0:N1}" -f($_.size/1gb)}},#{Name="FreeSpace(GB)";Expression={"{0:N3}" -f($_.freespace/1gb)}} | out-file ./local.txt
Now I got the following output:
DeviceID Size(GB) FreeSpace(GB)
-------- -------- -------------
C: 59.9 17.080
D: 0.0 0.000
F: 100.0 81.864
Actually the commands are almost similar. The only difference is that if we try to get the space details of a remote machine, then we need to add the parameter -ComputerName and the remote machine name.
My question is, why is PowerShell showing two different results when I running the command remotely and locally.
The value that it is showing is actually correct.
I've a list of other servers and use this command within a script to find the disk space details of them. Tomorrow, if another drive becomes inaccessible in one of the servers, this command won't help me find it as it would retrieve its details also.

Related

How to programmatically eject locked HDD eSATA drive with PowerShell?

I am trying to eject eSATA HDD drive via "USB tray icon of windows" for safe remove, but it is not possible because "another program using the drive" error (I already tired of this stup## Windows error).
I tried this code on PowerShell:
$vol = get-wmiobject -Class Win32_Volume | where{$_.Name -eq 'F:\'}
$vol.DriveLetter = $null
$vol.Put()
$vol.Dismount($false, $false)
And this other:
$Eject = New-Object -comObject Shell.Application
$Eject.NameSpace(17).ParseName($usbDrvLetter+“:”).InvokeVerb(“Eject”)
And nothing happens.
Any method valid to get this to work?
Not sure but the overload definitions for the .Dismount() method are:
OverloadDefinitions
-------------------
System.Management.ManagementBaseObject Dismount(System.Boolean Force, System.Boolean Permanent)
So maybe change to: $Vol.Dismount($true, $true) effectively force the dismount.
I've tried testing this, except in general I think you should use the CIM cmdlets instead of -WMI. Get-WMIObject is deprecated in Windows PowerShell and has been removed from PowerShell Core.
It's also important to consider the potential return codes (documented here) from the Dismount method.
RETURN VALUE Return code Description
------------ ------------------------
0 Success
1 Access Denied
2 Volume Has Mount Points
3 Volume Does Not Support The No-Autoremount State
4 Force Option Required
To get the volume instance:
$Vol = Get-CimInstance -ClassName Win32_Volume -Filter "DriveLetter = 'E:'"
That's obviously similar enough to the older approach. However, invoking methods is done a little differently:
$Vol | Invoke-CimMethod -MethodName Dismount -Arguments #{ Force = $true; Permanent = $true }
ReturnValue PSComputerName
----------- --------------
2
And, it doesn't appear to dismount the drive. However, if I change the arguments:
$Vol | Invoke-CimMethod -MethodName Dismount -Arguments #{ Force = $true; Permanent = $false }
ReturnValue PSComputerName
----------- --------------
0
This does appear to dismount the drive, as Explorer shifted focus. However, the drive is still visible and when clicked can be accessed. I never saw the Safely eject message. It seems that Dismounting the drive is not equivalent to safely ejecting it.

How to write a powershell script to know disk is basic or dynamic?

I have to check whether a given node contains any dynamic disk or not and get the list of dynamic disk using Power Shell script. I am not supposed to use diskpart command. Any other solutions other than diskpart will be appreciated.
https://social.technet.microsoft.com/Forums/windowsserver/en-US/cd7c0327-3fe9-45fc-a177-5a9927d468f3/does-the-getdisk-funtion-only-return-basic-disks?forum=winserverpowershell
Get-WmiObject Win32_DiskPartition -filter "Type='Logical Disk Manager'" | Select-Object *
you may use also diskpart utility, which is easily scriptable (I worked with it in Python)
the idea is that when you perform diskpart and then list disk,
output will be like:
Disk ### Status Size Free Dyn Gpt
-------- ------------- ------- ------- --- ---
Disk 0 Online 476 GB 0 B *
So you'll see all dynamic disks marked with asterisk under "Dyn"

How to find the drive letter of a newly inserted USB drive using Powershell?

I am trying to accomplish this:
When I insert a USB drive in Windows (Win7 and Win8), I want a script to be started (Powershell or batch command) automatically. The machine has autoplay disabled.
I setup a scheduled task with event id 2010 in DriveFrameworks-UserMode as trigger. The scheduled task works. However, I am having difficulties finding the drive letter of the newly inserted USB drive.
I found a few solutions on the Internet, but they didn't fit my requirements. The solutions either check for all drive letters (A to Z), or check for USB drive. I would like to correlate the drive letter that is associated with the event in the DriveFrameworks-UserMode trigger.
This post (How do i get the drive letter of a USB Drive in Powershell?) gave me some hints and I checked classes win32_logicaldisk, win32_diskdrive, and win32_pnpentity, but I couldn't find a clue matching them.
Any help is appreciated.
This should help:
$diskdrive = gwmi win32_diskdrive | ?{$_.interfacetype -eq "USB"}
$letters = $diskdrive | %{gwmi -Query "ASSOCIATORS OF {Win32_DiskDrive.DeviceID=`"$($_.DeviceID.replace('\','\\'))`"} WHERE AssocClass = Win32_DiskDriveToDiskPartition"} | %{gwmi -Query "ASSOCIATORS OF {Win32_DiskPartition.DeviceID=`"$($_.DeviceID)`"} WHERE AssocClass = Win32_LogicalDiskToPartition"} | %{$_. deviceid}
$drive = gwmi win32_volume | ? {$letters -contains ($_.name -replace "\\")}
$drive.DriveLetter
PS. There is a similar discussion here

Check if Windows path is on locl disk or shared device

I have a java application with many jars. I noticed that running the app from a share takes more time than running it from localdisk.
I would like to create a wrapper script that copies that jars to local disk only if the app was installed on a network path.
Is there a way to find out if a specific folder is on localdisk or network?
you could use wmi for that : check drivetype of the Win32_LogicalDisk class
type 3 => local
type 4 => net drive
an example using powerhsell
gwmi win32_logicaldisk -filter "deviceId='X:'" |select -expand drivetype
or
switch(gwmi win32_logicaldisk -filter "deviceId='X:'" |select -expand drivetype){3{"local"}4{"network"}}

Powershell Command to determine the Free Space of a Cluster Shared Volumes on a Remote Windows Server

I am looking for a powershell command that will pull the free space of a Cluster Shared Volume as it does not have a logical drive letter on a remote system. The Following is the Command I use on Logical Drives so you can see an example of the output I am Looking for. I am looking for just a numeric value of available megabytes.
Get-WMIObject -computer server -filter "DeviceID = 'C:'" Win32_LogicalDisk |
ForEach-Object {[math]::truncate($_.freespace / 1MB)}
Thanks in advance for your Help!
You can't determine the free space of a Cluster Shared Volume (CSV) on a per-host basis. CSVs are cluster-shared resources, so you need to use cluster tools for obtaining information about them:
$cluster = 'Cluster Name'
$volume = 'Cluster Shared Volume Name'
Import-Module failoverclusters
Get-ClusterSharedVolume -Name $volume -Cluster $cluster `
| select -Expand SharedVolumeInfo `
| select #{n='FreeSpace';e={($_.Partition.Size - $_.Partition.UsedSpace)/1MB}}

Resources