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

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}}

Related

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))}

Is there any method for getting details of all installed apps in a Windows device using shell commands

I need to get all installed applications and its details in a Windows device using shell commands. I tried using
Get-appxpackage
Get-WmiObject
wmic
Apps that were installed manually seems to be missing in the list. Please help by providing a better method.
An alternative can be to query the registry like this for example:
# HKLM - Local Machine
$InstalledSoftware = Get-ChildItem "HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall"
foreach($obj in $InstalledSoftware){write-host $obj.GetValue('DisplayName') -NoNewline; write-host " - " -NoNewline; write-host $obj.GetValue('DisplayVersion')}
# HKCU - Current User
InstalledSoftware = Get-ChildItem "HKCU:\Software\Microsoft\Windows\CurrentVersion\Uninstall"
foreach($obj in $InstalledSoftware){write-host $obj.GetValue('DisplayName') -NoNewline; write-host " - " -NoNewline; write-host $obj.GetValue('DisplayVersion')}
Check this page out for more:
https://www.codetwo.com/admins-blog/how-to-check-installed-software-version/
Tip! Browse these locations in the registry manually before you dig in as it will help you see the structure and understand what properties are available. If the information you're seeking is not there, you might just ditch this suggestion.
For Windows 64-bit and 32-bit apps use
Get-ItemProperty HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\* | Select-Object DisplayName, DisplayVersion, Publisher, InstallDate | Format-Table > C:\ws\apps.txt
the C:\ws\apps.txt need to be adjusted by you, to your output path.
I found the idea here, Social MS

how to get unidentified network adaptor name with batch

I already knew how to get all network adaptors names, but I can't tell the difference between normally using and not identified
By using
netsh interface ip show interfaces
or other commands ,you may get all adaptors names.
The necessary information can be obtained using the Win32_NetworkAdapter class.For example:
Get-CimInstance -ClassName Win32_NetworkAdapter -Filter #'
NetConnectionID = "Local Area Connection" AND
NetConnectionStatus=2 AND NetEnabled = True AND PhysicalAdapter = True
'# | Format-List *
The Get-WmiObject cmdlet can also be used, but starting in PowerShell 3.0, it has been superseded by Get-CimInstance.Starting with Windows 8, you can use the Get-NetAdapter cmdlet.

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.

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"}}

Resources