How to check if a flash drive is bootable using PowerShell? - powershell

I want to check if the flash drive is bootable with a PowerShell.
I tried to use (Get-Disk).PartitionStyle, win32_diskpartition.Bootable and win32_diskpartition.BootPartition
- it does not reflect the real situation, they are based on the Partition Style (MBR or GPT).
I have two flash drives, one with a Windows 10 image, the second is empty.
But both of it has the MBR boot sector type. WMI classes also show that "Bootable" is "True".
Is there any other way to find if the flash drive is bootable?

Try to use this commands to get boot disk, test at 4 flash drives and it works
Get-Disk |Where-Object {$_.IsBoot -eq $true -and $_.BootFromDisk -eq $true}
Additional
I checked it and was only half right. On usb drives, by default there is a sector that is responsible for the boot disk flag(may it stay if usb was once bootable and not deleted then).

Related

Net view - get Just ' Share Name'

I need to get all of the shares name in some storage's.
Im using Net view $StorageName and it's show The result in a Table format :
Share name Type Used as Comment
----------------------------------------------------------------------------
Backups Disk
CallRecordings Disk
Download Disk System default share
home Disk Home
homes Disk System default share
Installs Disk
Justin Disk Copy of files from Justin laptop
michael Disk
Multimedia Disk System default share
Network Recycle Bin 1 Disk [RAID5 Disk Volume: Drive 1 2 3 4]
Public Disk System default share
Qsync Disk Qsync
Recordings Disk System default share
Sales Disk Sales Documents
SalesMechanix Disk
Server2012 Disk Windows Server 2012 Install Media
Usb Disk System default share
VMWareTemplates Disk
Web Disk System default share
The command completed successfully.
Thats good, but I need Just the Share Name.
I need Help please.
Thanke You!
Here is one way you could do it with the net view output:
(net view $StorageName | Where-Object { $_ -match '\sDisk\s' }) -replace '\s\s+', ',' | ForEach-Object{ ($_ -split ',')[0] }
Basically that is saying find the lines that have Disk surrounded by whitespace just in case something else might have Disk in the name. Then replace multiple spaces with a comma. Then, for each of those lines, split again by the comma and take the first value which would be the share name.
If you are on a Windows 8/2012 or newer system (and attempting to enumerate shares on other Windows systems), you could use a CIM session along with Get-SmbShare instead of net view which would return the results as objects and allow you to select the fields you want in the native PowerShell way.
For example:
$cimSession = New-CimSession $StorageName
Get-SmbShare -CimSession $cimSession | Select Name
Name
----
Admin
ADMIN$
C$
IPC$
J$
print$
Public
Software
Another option for parsing the net view output that relies on positioning rather than regular expression matching. personally I feel it's a bit easier to read and just as reliable.
function get-shares {
param($server)
$rawShares = net view \\$server
$shares = $rawShares[7..($s.Count - 3)]
$shares | . {process{$_.substring(0,($_.indexof(" ")))}}
}

Get HDD serial numbers using Powershell

I am archiving a large number of HDD's at my company, and I have a powershell script that grabs the hostname and user list from a Windows OS installation, and I would like to programatically find the serial number of the drive as well. I have the following bit of Powershell code (mostly completed) that should do this, but there's a complication as well.
$Disks = Get-WMIObject -class win32_PhysicalMedia
$SerialNumber = foreach($Disk in $Disks) {IF ($Disk.SerialNumber -ne ' WD-WCC2EAV91692') {[do something here]}}
I am connecting the drives with a USB HDD dock, and it seems that if the computer is booted with the drive connected internally (via SATA cables, I haven't tested externally yet), then the SerialNumber field is populated. However, if I connect it after the computer has booted up, the SerialNumber field is always blank. Is there a way to have the computer re-scan for this info when I connect the drive, or is this info only gathered at boot-up, for example, by the BIOS or something?
AFAIK the SerialNumber is optional and provided by the driver. So if the USB-dock driver is not providing the information to Windows, then there's no easy way to retrieve it.
This thread (look for "Maxim Shatskih") says that IOCTL_SCSI_PASS_THROUGH is supported by USBSTOR, so it would be possible to write or find a program that can get the serial number from a USB attached drive, by passing the appropriate SCSI command to the drive (get mode page 80h).
A package that may do what you need is sg3_utils for Windows (it was originally written for Linux but ported to Windows).
I just tested booting the computer up with the HDD in the USB dock, and I got the same result. Technically, the SerialNumber field wasn't blank, but was all zero's instead of the actual serial number, which is probably just the same difference. It's seeming like it may be more work than it's worth to do this as a part of the script (assuming it's even possible), so I will likely just continue scanning the SN barcode into the script. It's an extra step, but only takes a couple seconds.
Thanks everyone for the input.

A map network drive not showed with Powershell, but is with VBS?

I have made a function with powershell that list all the available drive letters for mapping a new drive. This is working just fine at work(On a domain, Win 7, 64 bit).
But here at home, at my private computer(not on a domain, Win 8.1, 64 bit) I can't get Powershell to see my Map Drive R:.
It's there, in explore and when I use VBS:
Net use r:
Local name R:
Remote name \\10.10.10.10\Folder
Resource type Disk
The command completed successfully.
In powershell I use the following code:
Get-PSDrive | Where-Object name -EQ 'r'
And Im not getting any result back. If I look in WMI, I'm still not getting anything. Here I use the following code:
Get-CimInstance win32_LogicalDisk | Where-Object DeviceID -EQ 'r:'
I found this link, Getting a free drive letter, and even with this code, it's still select my R:\ as an available drive letter.
Can some point me a the right direction, please :-) ?
Bonus info:
At work all drives are mappet to windows computers.
At home my r:\ is mappet to a linux, nas server(Qnap TS 410).
Get-SMBshare
Don't show the R: drive ether.
Microsoft has issued a knowledge base article (KB3035277) about the issue you reported:
Mapped drives are not available from an elevated prompt when UAC is configured to "Prompt for credentials" in Windows
In the article Microsoft confirms that this is a problem in a number of listed Microsoft products (including Windows 8.1) and they offer two different work arounds.

How to obtain UNC drive info on "Disconnected Network Drive"

We run some processes in a distributed computing environment. Processes on one machine need to communicate information with processes on other machines. One of those piecies of information is the location of certain files. Thus, a process on one machine may have put information into a particular file on a particular network share, and it needs to communicate the location to a process on another machine.
We have no problem with the communication part. The problem is with determining the "location" information that a machine need to disseminate. File paths involving drive mappings are clearly useless: different machines will have differing drive mappings. Thus, what we need to communicate it the full UNC path name.
For the most part, we can obtain that information easily. One place where we are having problems is in a powershell script that needs to obtain this information. Currently, we use the following code:
$l_logicalDisk = Gwmi Win32_LogicalDisk -filter "DeviceID = '$l_currentDrive'"
if ( $l_logicalDisk.DriveType -eq 4 )
{
$l_base = $l_logicalDisk.ProviderName
}
and $l_base provides the \\computername\share information. However, in certain circumstances, this fails. At times, for some unknown reason, a mapped drive will appear as "Disconnected Network Drive" in Explorer.exe, even though the drive and all its files are accessible. (In fact, the script that is running is even located on the supposed "Disconnected Network Drive".) In this situation, the ProviderName field of the logical disk information is blank. Nothing seems to flip the status from "Disconnected Network Drive", nor have I found any way to update the ProviderName information.
So, does anyone know either (1) how to "reconnect" a disconnected network drive from within powershell or (2) how in Powershell to obtain the UNC path information for a directory in a more reliable method that outlined above? Thanks.
You can always ask the registry, this should work on disconnected drives (where $DrvLtr equals the desired network mapped drive letter such as Z or M):
Pushd
cd HKCU:
$UNC=(gci network|?{$_.Name -match "$DrvLtr"}|%{Get-ItemProperty -Path $_}).RemotePath
Popd
$UNC should then be a string with a value like "\Server01\FileShare$" which I think is what you're going for. Then you can just do a
$Path.Replace("$DrvLtr`:",$UNC)
And you're all set

With PowerShell, is there a way to make an external hard disk attached to host available to VM?

I have Hyper-V module installed but don't know what cmdlet to use. I've seen blogs showing how to do this using Hyper-V Manager. I have added a VHDx disk image with following cmdlet:
Add-VMHardDiskDrive -VMName MyWin7PC -ControllerType IDE -ControllerNumber 0 `
-ControllerLocation 0 -Path "C:\Virtual Hard Disks\VDisk.MyWin7PC.Vhdx"
What I now need is a way for VM to have another drive E: which will show files and folders the host has on its G: drive (which is physically connected to a USB hard disk). I need this temporarily to install applications from the USB hard disk. You can do this using the Hyper-V Manager GUI.
I don't use Win8, so this is entirely untested. Judging from the description of Add-VMHardDiskDrive something like this might work, though:
$usbdisk = gwmi Win32_DiskDrive | ? { $_.PNPDeviceID -like 'USBSTOR\*' }
Add-VMHardDiskDrive -VMName MyWin7PC -ControllerType IDE -ControllerNumber 0 `
-ControllerLocation 1 -DiskNumber $usbdisk.Index
You have to make this disk Offliine. Try this:
"select disk 1","offline disk" | diskpart
Where 1 is your USB HD id. And then use Add-VMHardDiskDrive. If you want to do this when VM is online you must use SCSI Controller in VM.