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

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"

Related

Powershell: Extract physical disk properties based on a logical drive name or a filepath [duplicate]

This question already has answers here:
Combine `Get-Disk` info and `LogicalDisk` info in PowerShell?
(3 answers)
obtain disk serial number from drive letter
(4 answers)
Closed 2 years ago.
Went through few similar queries, but could not a get a related solution as per my requirement.
Here is what I want:
I am writing a powershell script that, given a logical drive name (or a filepath), should be able to return me its underlying physical drive info like serial number, manufacturer etc.
I can get these details from Get-Disk or Get-PhysicalDisk. I see logical volume details from Get-Volume. But how do I correlate these two results to filter out results based on a specific logical volume or path (like get serial number of physical disk for the logical drive D:)
Any help is appreciated.
This is what I have tried until now:
My logical driver for which I want to get disk details:
PS C:\Users\Administrator> Get-Volume -FilePath U:\
DriveLetter FriendlyName FileSystemType DriveType HealthStatus OperationalStatus SizeRemaining Size
----------- ------------ -------------- --------- ------------ ----------------- ------------- ----
U New Volume NTFS Fixed Healthy OK 19.91 GB 20 GB
Get-Disk has all the information I need:
PS C:\Users\Administrator> Get-Disk | Select Manufacturer, SerialNumber, UniqueId
Manufacturer SerialNumber UniqueId
------------ ------------ --------
3PARdata SGH622X73L 60002AC0000000000E006A980001AB73
3PARdata SGH622X73L 60002AC0000000000E006A990001AB73
3PARdata SGH622X73L 60002AC0000000000E006E9D0001AB73
3PARdata SGH622X73L 60002AC0000000000E006E9E0001AB73
3PARdata SGH622X73L 60002AC0000000000E006E9F0001AB73
3PARdata SGH622X73L 60002AC0000000000E006EA00001AB73
My problem is, how do I filter out which of the above listing belongs to my logical volume U:\ ?
#zett42's link worked for me. Posting it here again:
get-partition -DriveLetter U | get-disk

How can I use "IF" statement to determine the amount of free disk space to add to a VMDK?

The script I am using is below. We are upgrading our 1500 VDI's from Windows 10 1809 to 1909 and we want to automate the process as much as possible since this will be a process that we will have to do regularly with each new version Windows puts out. We are using SCCM (System Center Configuration Manager). The script works perfect for extending disk space for multiple machines. The .csv file it is importing only contains a list of virtual machines the script is to expand disk space on. I want to use an "if" statement to determine if the amount of free space is below 30GB and, if so, to add the needed disk space to bring it up to 30GB free space. Example: "if" free disk space is -lt 30GB, Set-HardDisk -CapacityGB (the difference to make 30GB free space available), "if" free space is -eq to 30GB, do nothing. I have researched Google and this site for anything remotely similar and wasn't able to find what I need. All help accomplishing this is appreciated.
##Task Change Disk Size
##Variable clear
$csvobjects = #()
$cskobject = #()
$network = #()
$isalive = #()
##Import VM name(s)
$csvobjects = Import-CSV -path "C:\Temp\ExpandHDDiskList.csv"
Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -Confirm:$false
connect-viserver -server anyserver.com -User anyuser#anyserver.com
foreach ($csvobject in $csvobjects){
##Variable clear
$network = #()
$isalive = #()
##Pre-change data gathering
$beforechange = (GET-VM -Name $csvobject.vmname | FT -auto CapacityGB|out-string)
##Stop VM
GET-VM -Name $csvobject.vmname | Get-HardDisk -Name 'Hard disk 1' | Set-HardDisk -CapacityGB 75 -Confirm:$false
start-sleep -s 60
# Variable specifying the drive you want to extend
$drive_letter = "C"
# Script to get the partition sizes and then resize the volume
$size = (Get-PartitionSupportedSize -DriveLetter $drive_letter)
Resize-Partition -DriveLetter $drive_letter -Size $size.SizeMax
}
You can use Get-VMGuest to get the free space within the guest VM.
Set the target amount of free space in a variable. 30GB (GB = 1024^3)
$gb = 1024 * 1024 * 1024
$space = 30 * $gb
Get the free space from the VM using Get-VMGuest
$vm = Get-VMGuest $csvobject.vmname
Now, assuming a single hard disk in the machine, determine if the free space is less than 30GB. If it is, increase the hard disk size so that there is 30GB of free space. The calculation takes the capacity of the disk and adds on the required free space (30GB) and then subtracts the current free space. This is all done in bytes so convert it into GB and round it to a whole number to avoid weird disk sizes:
if ($vm.Disks[0].FreeSpace -lt $space) {
Get-HardDisk $vm.Vm | Select -first 1 | Set-HardDisk -CapacityGB ([math]::round(($vm.Disks[0].Capacity + $space - $vm.Disks[0].FreeSpace) / $gb))
}
Take care when running, particularly if you have machines with multiple hard disks.
Assuming Windows VMs, you don't need to stop the VM. You can resize the filesystem remotely using Invoke-Command or Invoke-VMScript.
With PowerShell 5 or newer you can replace
$gb = 1024 * 1024 * 1024
with
1GB
And replace
$space = 30 * $gb
with
30GB
So, the entire script would look like this
$vm = Get-VMGuest $csvobject.vmname
if ($vm.Disks[0].FreeSpace -lt 30GB) {
Get-HardDisk $vm.Vm | Select -first 1 | Set-HardDisk -CapacityGB ([math]::round(($vm.Disks[0].Capacity + 30GB - $vm.Disks[0].FreeSpace) / 1GB))}

How can I list all running Windows processes using Ruby without any additional library?

I want to list all processes running on my Windows system using Ruby without installing any additional dependency or library that is not already part of Ruby. I have not found any way to do this online. Is there any clean way to do this from Ruby?
You can use the Kernal::system method to execute a command line argument. For example:
system("tasklist")
Image Name PID Session Name Session# Mem Usage
========================= ======== ================ =========== ============
System Idle Process 0 Services 0 24 K
...
ruby.exe 1336 Console 1 9,100 K
tasklist.exe 944 Console 1 5,332 K
Alternately--as points #Pavling out--you can use [Kernal::`](aka backtick), but some find it less readable. YMMV.

Powershell script to output Size on disk

I'm very new to powershell and is it possible to obtain the actual size of disk of a file? I was able to use the du, but is there another way of doing this without using that application?
This will give you the actual size of a file in bytes:
(gci <insert file path> | Measure-Object -Property length -Sum).sum
You can then use other logic to convert to KB, MB, GB, whatever you want. You can use the same command for size of directories, with the -Recurse option to get the size of all subdirectories and files in the root.

Powershell showing disk space details of inaccessible drives

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.

Resources