Get HDD serial numbers using Powershell - 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.

Related

HP BIOS Battery Health Settings

I am attempting to change the BIOS battery charging settings with PowerShell and will eventually deploy it using Intune. I am interested in doing this because I have some users that leave their computers plugged in full-time and it ruins the battery within a year. Changing this setting will only allow the computer to charge up to a specified limit which will hopefully increase the batteries overall lifespan.
I am having issues with my code which is giving me a "The Term ' ' is not recognized as the name of a cmdlet, functions, script file, or operable program". I am using the following documentation to create the script and am using an HP computer that has been verified to have this setting in the BIOS.
https://developers.hp.com/hp-client-management/blog/managing-battery-health-settings-hpcmsl-and-intune
$setting = Get-HPBIOSSettingValue -Name "Battery Health Manager"
if ($setting -eq "Let HP manage my battery charging") {
exit 0
}
Set-HPBIOSSettingValue -Name "Battery Health Manager" -value "Let HP manage my battery charging"
exit 0
For the purpose of this exercise I don't really need to test the detect script because I already know what this setting is set to. Does anyone know what I am doing wrong? The script also failed when I saved it as a .ps1. I have a feeling there is something very basic that I am missing because I am new to powershell.
Sounds like you don't have the HP Powershell module installed.
The command Get-HPBIOSSettingValue and Set-HPBIOSSettingValue are not native to Windows - you'll need to download the relevant HP module and install it - try Powershell Gallery.

How to check if a flash drive is bootable using 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).

Task sequence variable for OS, I want to install

I'm verry beginer in powershell, I'm wonking in a project, the goal it's to set the Biossetting like disabling or enabling the secureBoot and UEFI mode, while installing windows 7 or 10 by MDT.
I'm working with Dell and hp computer, I have the script for setting the bios of hp or dell
Hp:
$bios=Get-WmiObject -Namespace root/hp/instrumentedBIOS -Class HP_BIOSSettingInterface
$bios.SetBIOSSetting("UEFI Boot Options", "Enable","")
Dell:
(Get-WmiObject DCIM_BIOSService -namespace root\dcim\sysman -ComputerName .).SetBIOSAttributes($null,$null,"Secure Boot","1")
Then, my first problem these command is not working in any computer I need to install some modules, some cmdlet from hp or dell website, I want to know if make my script ".exe", it's gonna work in every-computer ?
Because I need to run my script with with deployement of windows.
My second and difficult task, I want to know with variable task sequence to use in my script, to detect the os of the tasksequence, I find this code in internet, after too much research in internet
$TaskPath = "$($MdtDrive):\Task Sequences"
$ControlPath = "$MDtroot\Control"
$OSPath = "$($MdtDrive):\Operating Systems"
$OS = (Get-ChildItem -Path $OSPath | Out-GridView -PassThru -Title "Select required OperatingSystem").Name
This code detect if the OS of the task sequence I want install in my computer is windos 7 or windows 10?
Thanks !
If I recall correctly from my days of systems deployment, Dell and HP both make dedicated tools for settigns BIOS configuration. Just make sure you run it in WinPE. Depending on which BIOS settings you change you make even have to boot WinPE twice to make sure the OS installs the way you want.
Dell: http://en.community.dell.com/techcenter/enterprise-client/w/wiki/7532.dell-command-configure
HP: https://deploymentbunny.com/2010/10/18/enable-tpm-via-task-sequence-on-hp-boxes/
Although it is definitely possible to make these settings in WMI I would only look to it as a last resort. Windows has to be compatible with every piece of hardware, whereas Dell/HP tools are targeted at their systems. It's like using a scalpel vs a Swiss army knife.
I have some difficults I’m working in a script who set the bios configuration while installing windows 7 or 10 by MDT, then my first question is:
Wich variable I can use to identify the os of the new task sequence I mean the current os the mdt preparing to install in the computer after the user select the os during the installation.
I’m wondering if this code doying the job
$OS = Get-ChildItem -Path $OSPath | Out-GridView -PassThru -Title “Select required OperatingSystem”
$OSPath = “$($MdtDrive):\Operating Systems”

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