I have a flash drive which I formatted so that the volume label on the drive is "PHILIP".
I am using Get-PSDrive H -PSProvider FileSystem to determine if the drive is plugged in, however I would really like to determine if the drive is plugged in by the volume label, i.e. Get-PSDrive -VolumeLabel PHILIP -PSProvider FileSystem. Of course the VolumeLabel parameter does not exist so this doesn't work.
Is there a way to list the drives in a computer by volume name?
You can use WMI, I guess:
Get-WMIObject Win32_Volume | ? { $_.Label -eq 'PHILIP' }
You can use the DriveInfo class from the .NET framework as well:
PS> [System.IO.DriveInfo]::GetDrives()
Name : C:\
DriveType : Fixed
DriveFormat : NTFS
IsReady : True
AvailableFreeSpace : 217269202944
TotalFreeSpace : 217269202944
TotalSize : 320070479872
RootDirectory : C:\
VolumeLabel : OS
You can then pipe that to the Where-Object cmdlet (both ? and Where are aliases) to filter that to just the volume you are looking for:
PS> [System.IO.DriveInfo]::GetDrives() | ? {$_.VolumeLabel -eq "PHILIP" }
I use Get-WMIObject like Joey proposes.
To link the wmi results to for example a get-partition i use the caption parameter.
In this example I set the partition letter of volume Philip to D
$datavolume=Get-WMIObject Win32_Volume | ? { $_.Label -eq 'PHILIP' }
$datavolume=$datavolume.Caption
get-partition -DiskNumber 0 | where {$_.accesspaths -like "$datavolume"} | Set-Partition -NewDriveLetter D
Related
I have written few powershell command to perform some auditing on HyperV Clusters. The command works fine, But can anyone help me to trim the output, so that I can collect what I need ?
##Audit-CreatingDC
$AuditDC = Invoke-Command -ComputerName $ComputerName {Get-ChildItem -Path HKLM:\cluster\resources -recurse | get-itemproperty -name CreatingDC -erroraction 'silentlycontinue'}| ft CreatingDC,PSComputerName
####Audit-iSCSI
#Show which hosts are not communicating to the storage with the ‘-s’ and where there are duplicated targets:
$AuditISCSI = Invoke-Command -ComputerName $ComputerName { get-iscsisession } | FT PSComputerName, InitiatorPortalAddress, IsConnected -autosize
######Discover checkdsk errors - "Scan Needed". Execute using txt of one node from each cluster.
$AuditCHKDSK = Invoke-Command -ComputerName $ComputerName { get-volume | Where-Object –FilterScript { $_.HealthStatus -eq "Scan Needed" }} | FT PSComputerName, FileSystem, HealthStatus -autosize
And the output for each is below
CreatingDC PSComputerName
---------- --------------
\\dc-sc-02.oim.corp.com slcoc037
PSComputerName InitiatorPortalAddress IsConnected
-------------- ---------------------- -----------
slcoc037 10.214.61.107 True
PSComputerName FileSystem HealthStatus
-------------- ---------- ------------
slcoc037 CSVFS 1
But I need the output for above in this format
\\dc-sc-02.oim.corp.com
10.241.81.107
CSVFS 1
Can anyone help me to trim these 3 commands ?
You probably already know that almost all powershell outputs are objects. Objects have properties. Displaying a particular property would use the syntax $Object.Propertyname. In your case, CreatingDC is a property of the $AuditDC Variable object. Applying that logic, all you need to do is, display it like this:
$AuditDC.CreatingDC
$AuditISCSI.InitiatorPortalAddress
$AuditCHKDSK.FileSystem
I'm trying to get a list of drive letters in a drop-down menu. I'm currently using the code below and it works just fine in Windows 10, but doesn't work at all in Windows 7.
$Drive_Letters = Get-WmiObject Win32_LogicalDisk
ForEach ($Drives in $Drive_Letters.DeviceID) { $Dest_Drive_Box.Items.Add($Drives) }
In Win 7 I tried adjusting the code to this...
$Drive_Letters = Get-WmiObject Win32_LogicalDisk | Select-Object DeviceID
ForEach ($Drives in $Drive_Letters) { $Dest_Drive_Box.Items.Add($Drives) }
But now it shows "#DeviceID=C:}", "#DeviceID=D:}", etc. in Win 7 and 10 for each drive letter. I need to just show "C:", "D:", etc.
Thanks!
Get-PSDrive
This will return all drives mapped in the current session. The Name property contains the drive letter.
To capture just drive letters:
(Get-PSDrive).Name -match '^[a-z]$'
Tested working in PSv2:
Get-PSDrive | Select-Object -ExpandProperty 'Name' | Select-String -Pattern '^[a-z]$'
$drives = (Get-PSDrive -PSProvider FileSystem).Root
returns an array for drives with the root path:
C:\
D:\
E:\
You can easily trim the ending if you don't want it.
$Drives = Get-WmiObject Win32_Logicaldisk | % {$_.DeviceId}
$Drives | % {$Dest_Drive_Box.Items.Add($_)}
It would appear that each item $drives is a HashTable with one Key-Value Pair DeviceID = driveletter a quick test shows that using $Drives.DeviceID returns just the value of the key-value pair.
ForEach ($Drives in $Drive_Letters) { $Dest_Drive_Box.Items.Add($Drives.DeviceID) }
I want to use the Where-Object to limit the Output of Get-PSDrive to only network shares.
Get-PSDrive shows me the following:
Name Used (GB) Free (GB) Provider Root CurrentLocation
---- --------- --------- -------- ---- ---------------
A FileSystem A:\
Alias Alias
C 16.19 43.47 FileSystem C:\ Users\HansB\Documents
Cert Certificate \
D FileSystem D:\
Env Environment
Function Function
HKCU Registry HKEY_CURRENT_USER
HKLM Registry HKEY_LOCAL_MACHINE
V 451.39 159.76 FileSystem \\192.168.71.31\fs_log_target
Variable Variable
W 197.72 FileSystem \\192.168.71.32\perf200
WSMan WSMan
X 197.72 FileSystem \\192.168.71.32\perf100
Y 271.52 34.33 FileSystem \\192.168.71.30\group200
Z 271.52 34.33 FileSystem \\192.168.71.30\group100
Then I want to get the \\192.168.71.30\group100 Network Share:
Get-PSDrive | Where-Object { $_.Root -match "\\\\192.168.71.30\\group100" }
But I get nothing, why does -match not work?
Use DisplayRoot instead of Root property
Get-PSDrive | Where-Object { $_.DisplayRoot -match "\\\\192.168.71.30\\group100" }
try to run
Get-PSDrive | Where-Object { $_.DisplayRoot -match "\\\\192.168.71.30\\group100" } | select *
Root will be your mapped drive letter, and DisplayRoot your UNC Path
EDIT: as a side note. For escaping regex use [regex]::Escape() method.
PS > [regex]::Escape("\\192.168.71.30\group100")
\\\\192\.168\.71\.30\\group100
Try using -eq instead:
Get-PSDrive | Where-Object {$_.Root -eq "\\192.168.71.30\group100"}
If yoy type Get-PSDrive | select root you will notice that for network drives it will print nothing. So there is something strange with network share roots.
Get-PSDrive | select root | %{write-host $_.Root.GetType()} will show that it's a system string, so not quite sure why it seems to be empty for network drives.
Am new to scripting kindly help me write a script that will connect to VMM and get details such as below.
Name : ABC Machine
CPUCount : 8
Memory : 8192
DynamicMemoryEnabled : False
VHDType : DynamicallyExpanding
MaximumSize : 214748364800
Size : 4194304
Location : C:\ClusterStorage\Volume3\CRB\CRB Test Machine_disk_1.vhdx
Classification : Silver
VHDType : DynamicallyExpanding
MaximumSize : 4748364800
Size : 41304
Location : C:\ClusterStorage\Volume2\CRB\CRB Test Machine_disk_2.vhdx
Classification : Silver
I have been able to get individual commands to get the info however I am not able to make a script that will do it for all VMs and convert disk sizes to GB
My working commands are
Get-SCVirtualMachine -Name "ABC Machine" | select Name, CPUCount, Memory, DynamicMemoryEnabled | fl
$DiskINfo = Get-SCVirtualDiskDrive -VMMServer "abc.abcgroupcloud.com" -VM "ABC Machine"
$DiskINfo.VirtualHardDisk | select VHDType, MaximumSize, Size, Location, Classification
1- create an array with all the VM names (or read it from a file with get-content)
2- use a foreach loop to excecute you script over all these VM
3- use a calulated property to display the size in Gb
$computers=#("ABC machine","XYZ machine")
$computers | foreach-object {
Get-SCVirtualMachine -Name $_ | select Name, CPUCount, Memory, DynamicMemoryEnabled | fl
$DiskINfo = Get-SCVirtualDiskDrive -VMMServer "abc.abcgroupcloud.com" -VM $_
$DiskINfo.VirtualHardDisk | select VHDType, MaximumSize, #{Name="Size in Gb";Expression={$($_.size)Mb / 1Gb}}, Location, Classification
}
Old question, but just to add some info.
This will get all the Virtual Machines in your host group in VMM, after entering the correct host group name.
$VMs will be the array, which will contain all the details you are after.
$hg = Get-SCVMHostGroup -Name "My Hostgroup Name"
$hosts = Get-SCVMHost -VMHostGroup $hg
$VMs = $null
ForEach ($h in $hosts)
{
$VMs += Get-SCVirtualMachine -VMHost $h
}
In PowerShell I'm trying to get the drive letter that an ISCSI target is mapped to. I'm using the following to get the ISCSI initiator name.
Get-IscsiTarget | ? {$_.IsConnected -eq $True} | Select -ExpandProperty NodeAddress
I have tried using Get-Disk | Select * and Get-PSDrive | Select * but these cmdlets do not seem to have any fields that I can link a target to, to obtain its drive letter.
As long as you have one active partition (not including reserved) per ISCSI target, you can use the following to match an ISCSI address to its corresponding drive letter.
foreach ($disk in (Get-Disk | ?{$_.BusType -Eq "iSCSI"})){
$DriveLetter = ($disk | Get-Partition | ?{$_.Type -eq "Basic"}).DriveLetter
$ISCSI = $disk | Get-IscsiSession
[pscustomobject]#{
DiskNumber=$disk.Number;
DriveLetter=$DriveLetter;
InitiatorNodeAddress=$ISCSI.InitiatorNodeAddress;
InitiatorIP=$ISCSI.InitiatorPortalAddress;
Size=$disk.Size;
}
}
This will check all connected ISCSI disks and get their corresponding drive letter, then it will put all the information into a customer PowerShell object and return it.