Get hard disk serial number from local disk in batch - windows

I need to get the model and serial number of the disk on which windows is installed (local disk C). This way I can get the one from all disks, but how can I get the one from the local disk only?
wmic diskdrive get model,serialNumber

This command displays the virtual disk information of the computer
Get-Disk -Number 0 | % {$_.FriendlyName,$_.SerialNumber}
If you want to run it in cmd or batch file
powershell.exe -command "Get-Disk -Number 0 | % {$_.FriendlyName,$_.SerialNumber}"

Related

Limit the number of CPU cores used by an Windows service running from net start command

I am running an benchmark for PostgreSQL 12 on Windows 10. I want to limit the number of CPU cores used by PostgreSQL service to test how does the CPU performance affect TPU.
Now I am starting PostgreSQL service with following command:
net start postgresql-x64-12
and I know how to limit the number of CPU cores for ordinary Windows application like:
start /affinity 1 "" "C:\Windows\System32\calc.exe"
How can I limit the number of CPU cores used by an Windows service running from net start command? Is there an /affinity option equivalent in net start command?
I found a solution. First, you cannot set CPU affinity to Windows system processes or services (see https://www.atmarkit.co.jp/ait/articles/0703/16/news151.html (Japanese)).
In my situation, I can run PostgreSQL process from pg_ctl command from cmd.exe with /affinity option like:
cmd.exe /c "start /affinity 1F /B c:\path\to\PostgreSQL\12\bin\pg_ctl.exe start -w -s -D C:\path\to\PostgreSQL\12\data"
Note that you cannot use Start-Process cmdlet and ProcessorAffinity property like this:
$app = Start-Process 'c:\path\to\PostgreSQL\12\bin\pg_ctl.exe' 'start -D C:\path\to\PostgreSQL\12\data' -PassThru -NoNewWindow
$app.ProcessorAffinity = 0x3
This causes SetValueInvocationException because pg_ctl.exe is immediately exit after it starts PostgreSQL instance.

Windows Server 2008 R2 C:\Perl\bin\perl.exe execution log (or monitoring) to locate file processing its high CPU usage

We have software written in perl 5 that users access online through a windows 2008 server.
C:\Perl\bin\perl.exe is executing a file that causes high CPU processing indefinately. In our experience we've analyzed task manager and resource manager and it is very 'shadowy': the data changes too fast and there's not a real correlation to what file is causing the issue.
We'd like to correlate which file has been called to run when the CPU is maxing out at 100% for an extended period of time.
If C:\Perl\bin\perl.exe is running then is there a way to determine what it is executing?
We've looked into the 'Overview', 'CPU' and 'Disk' of the resource manager. The CPU tab doesn't tell us the file name whilethe Disk and Overview tabs won't show cpu processing.
The command line (including arguments) is available via WMI.
>wmic process where "name like '%perl%'" get ProcessId,CommandLine
CommandLine ProcessId
perl foo.pl 9900
PS> Get-WmiObject Win32_Process -Filter "name like '%perl%'" `
| Select-Object ProcessId,CommandLine
ProcessId CommandLine
--------- -----------
9900 perl foo.pl
If you prefer a GUI, you can use [Process Explorer]](https://learn.microsoft.com/en-us/sysinternals/) (from Microsoft).

getting driveletter of mounted iso over network

I have a problem with getting driveletter of a mounted ISO over network. I have used Mount-DiskImage -ImagePath $ISOPATH for mounting.
When i mounted ISO file, which was locally stored on disk D:\, i had no problem with getting the volume information via powershell: Get-DiskImage -ImagePath $ISOPATH | Get-Volume. This is how the script looks like:
Mount-DiskImage –ImagePath $ISOPATH
$driveletter = (Get-DiskImage -ImagePath $ISOPATH | get-volume).driveletter + ":"
cd $driveletter\Nanoserver
Above code works fine with local ISO file.
However, when ISO file was mounted from a network share, the Get-Volume command no longer work. It doesn't see the newly mounted drive, despite the fact that its visible and accessible via windows explorer.
Command Get-DiskImage -ImagePath $ISOPATH | Get-Volume no longer show any information, instead its showing nothing, blank result.
The only way that i see this drive via powershell is by using Get-PSdrive command, however, its hard for me to get the letter in automated way with this command.
Has anybody got solution to this problem?
I gladly provide any more information if required.
Thanks
Workaround (untested in production) based on comparing before ($vl1) and after ($vl2):
$vl1 = #(Get-Volume);
$disk = Mount-DiskImage -ImagePath "\\172.19.21.47\www\debian-8.8.0-amd64-netinst.iso" -PassThru ;
$vl2 = #(Get-Volume);
$dl = #( $vl2 | ? {$vl1 -notcontains $_} )[0].DriveLetter
If you guarantee that no simultaneous disk mounts happen in your system.
Possibly need Start-Sleep after mounting to make sure the drive letter assigned

Execute application on remote computer

I want to start a process on remote computer(i know administrative credentials of remote computer). for starting a application on remote machine i used
Start-Process -FilePath 'C:\pqr.exe' -ArgumentList '/a' -Verb runas -WindowStyle Normal
command with "Invoke-command" or "Enter PsSession" which will start process on remote machine. Now problem is, I am able to start process but soon process starves for CPU allocation(it becomes 0%) and suddenly the launced application become not respoding. Is there any other way to allocate it CPU or run above commandlet with admin rights.
your should start-process then change priority of process
The command line syntax:
wmic process where name="AppName" CALL setpriority ProcessIDLevel
Example:
wmic process where name="notepad.exe" CALL setpriority 32768
or
wmic process where name="notepad.exe" CALL setpriority "above normal"
Priority:
idle: 64
below normal: 16384
normal: 32
above normal: 32768
high priority: 128
real time: 256
or use powershell [System.Diagnostics.ProcessPriorityClass]
Specify one of the following enumeration values "Normal, Idle, High, RealTime, BelowNormal, AboveNormal"
example
$a = (Get-Process -Name powershell)
$a.PriorityClass = [System.Diagnostics.ProcessPriorityClass]::RealTime
Have you tried this:
PsExec
Maybe this work for you

How do I list only the valid mounted disk partitions using Get-PSDrive?

I just discovered PowerShell literally yesterday and I love it.
I am trying to list all the valid mounted partitions so I can make them a variable and run chkdsk on them. The problem is that I don't want to detect backup partitions on the machine. The computer I'm using to write and test this script doesn't have any backup/not-mounted partitions. I am thoroughly reading get-help get-psdrives but I don't have the knowledge to understand if the options can do what I need (or if they already are doing it).
This will get you all the drives that you can run chkdsk on:
Get-PSDrive -PSProvider FileSystem
Edit: This will get you the mounted drives that are either Local Disks (3) or removable disks (2):
Get-WmiObject -class “Win32_LogicalDisk" | ?{ #(2, 3) -contains $_.DriveType }

Resources