How To Find The Windows Operating System
Working in a multiserver surroundings demands very often to have an easy and fast method to collect data about the servers' hardware or software, especiallyWindows OS (Operating System) properties.PowerShell is a very handy tool in that sense, especially when the client'southward SLA (Service Level Agreement) is so strict that you cannot install any 3rd party software to do the chore.
In order to collect Window Operating System (OS) properties information using PowerShell, nosotros use either WMI or CIM classes, Win32_OperatingSystem or CIM_OperatingSystem respectively.
Go Windows Operating Organization Os Details Using PowerShell – Solutions
Here are few solutions for the local machine, remote computers, and writing ownPowerShell CmdLet.
Solution 1 – Get Windows Operating System Os Details Using PowerShell For The Local Car.
We callGet-CimInstanceCmdLet and get the necessary data from CIM_OperatingSystem CIM Grade.
Get-CimInstance -Class CIM_OperatingSystem -ComputerName localhost -ErrorAction Stop | Select-Object *
Hither is the event set for the local machine:

Solution two – Get Windows Operating Organisation Os Details Using PowerShell For Remote Computers
Create the list of servers in the text file and save in, for case, C:\Temp binder and run the same command as in the previous solution just useComputerNameparameter in addition. We basically load the content of the text file usingGo-ContentCmdLet and PowerShell will go through the list and run the aforementioned command every bit in the previous solution for each server on the list.
Get-CimInstance -Class CIM_OperatingSystem -ComputerName (Go-Content -Path C:\Temp\servers.txt) -ErrorAction Stop | Select-Object * | Out-GridView
Here is the effect fix for the list of servers:

I practise not take servers in my abode network so in order to simulate one I have created a very simple input listing of servers in a text file just copying thelocalhostvalue several times.

Solution 3 – Write Ain PowerShell CmdLet (Get-OSInfo ) To Get Windows Operating System Details Using PowerShell
This is my favorite method. It takes some time to write the code but it pays off in the long run with its reusability and tin can be combined with the library of other own CmdLets.
I have written my own CmdLetGet-OSInfo CmdLet that I volition explain to you shortly.
Hither is one example of callingGet-OSInfo CmdLet:
Become-OSInfo -filename "OKFINservers.txt" -errorlog -client "OK" -solution "FIN" -Verbose | Select-Object 'Surroundings', 'Logical Name', 'Server Name', 'Caption', 'Service pack', 'Version', 'Bone Architecture', 'Install Date', 'Last BootUp Time', 'Arrangement drive', 'Windows directory', 'Complimentary RAM (GB)', 'Free infinite in paging files (GB)', 'Free virtual memory (GB)', 'Number of processes', 'Number of users', 'Size stored in paging file (GB)', 'Total virtual retentivity size (GB)', 'Total visible retention size (GB)', 'IP', 'Collected' | Out-GridView
Here is the upshot set while calling Go-OSInfo CmdLet:

Hither is the list of properties that are returned with the PowerShell CIM_OperatingSystem CIM Form.
INFO: I take written 2 PowerShell Improver functions ( Become-CIMClassProperty and Select-CIMClassAllProperties) that help u.s. working withWMIandCIM classes. Beginning listing all the properties and datatypes of WMI or CIM classes and the 2d one makes the select statements with all the properties for the WMI or CIM grade. How To List CIM Or WMI Class All Properties And Their Datatypes With PowerShell AND How To Write Select Statement For All Properties Of CIM Or WMI Course With PowerShell.
Get- OSInfo CmdLet Explained
Get- OSInfo CmdLet collects Windows Operating System OS properties information for the listing of servers and this CmdLet belongs to Efficiency Booster PowerShell Projection. This project is the library of different CmdLets that tin can assistance us IT personal to practice our everyday tasks more efficiently and accurately.
Source code forGet- OSInfo CmdLet can exist downloaded from this zero file and so please experience free to download it and it would be easier for you to follow me along.
Go- OSInfo CmdLet is part of Mutual module and if you accept downloaded the source code it can exist institute in the folder…\[My] Documents\WindowsPowerShell\Modules\03common
INFO: If yous desire to know how to install and configure Efficiency Booster PowerShell Project files please read the following article: How To Install And Configure PowerShell: CmdLets, Modules, Profiles.
Get- OSInfo CmdLet – Input Parameters
As input parameters nosotros have:
- computers– it is a list of servers passed equally input parameter with default value 'localhost' and accepts both pipeline options. Parameter belongs to the "ServerNames" parameter fix. The "ServerNames" parameter set is the default parameter set up.
- filename– it is the name of the text file with the list of servers and represents an alternative choice to the "computers" parameter. The parameter belongs to the "FileName" parameter gear up.
- errorlog– switch datatype and when turned on it will write errors into an external mistake log file using Write-ErrorLog CmdLet. The mistake log file is located in the PSLogs folder of [My] Documents.
- client– it is a mandatory input parameter and by convention, I utilise 2 letters for client shortcode (for example, OK = O client, BK = B client, etc.). This parameter value is part of the filename parameter naming convention.
- solution– it is a mandatory input parameter and by convention, I employ ii-three messages for solution shortcode (for case, FIN = Financial solution, Hour = Hr solution, etc.).
The naming convention for thefilenameparameter is as follows:Client + Solution + Text.txt.The text file should be located in…[My] Documents\WindowsPowerShell\Modules\01servers folder.
For example:
- OKFINTestServers.txt– List of test environment servers for OK client and FIN solution
- OKFINProdServers.txt – Listing of production environmentservers for OK client and FIN solution.
- OKFINAllServers.txt – Listing of all servers for OK customer and FIN solution.
INFO: To get a deeper caption almostclientandsolutioninput parameters please read these two sections Parameter customer, Parameter solution.
INFO: In club to customize installation of CmdLet to your needs and setup necessary CSV file please read the following article How To Install And Configure PowerShell: CmdLets, Modules, Profiles
Here is the parameters definition code:
Part Get-OSInfo { [CmdletBinding(DefaultParametersetName="ServerNames")] param ( [Parameter( ValueFromPipeline=$truthful, ValueFromPipelineByPropertyName=$true, ParameterSetName="ServerNames", HelpMessage="List of computer names separated by commas.")] [Alias('hosts')] [string[]]$computers = 'localhost', [Parameter( ParameterSetName="FileName", HelpMessage="Proper name of txt file with listing of servers. Txt file should be in 01servers folder.")] [string]$filename, [Parameter( Mandatory=$faux, HelpMessage="Write to error log file or not.")] [switch]$errorlog, [Parameter(Mandatory=$true, HelpMessage="Customer for example OK = O customer, BK = B customer")] [string]$client, [Parameter(Mandatory=$true, HelpMessage="Solution, for instance FIN = Financial, 60 minutes = Human Resource")] [cord]$solution )
INFO: To know more aboutPowerShell ParametersandParameter Sets with some awesome examples please read the following articles How To Create Parameters In PowerShell and How To Use Parameter Sets In PowerShell Functions.
INFO:PowerShell Pipeliningis a very of import concept and I highly recommend you lot to read the article written on the subject. How PowerShell Pipeline Works. Here I take shown in many examples the realpowerofPowerShellusing thePipelining.
BEGIN Cake
In theBeginblock nosotros:
- If theFileNameparameter set has been used we test if text file with a list of servers exists.
- If the file exists read the file and create the list of servers every bit a string assortment in the$computersvariable.
- … and if non write a warning with information to the caller to create the file.
BEGIN { if ( $PsCmdlet.ParameterSetName -eq "FileName") { if ( Test-Path -Path "$home\Documents\WindowsPowerShell\Modules\01servers\$filename" -PathType Leaf ) { Write-Verbose "Read content from file: $filename" $computers = Get-Content( "$home\Documents\WindowsPowerShell\Modules\01servers\$filename" ) } else { Write-Warning "This file path does Non exist: $home\Documents\WindowsPowerShell\Modules\01servers\$filename" Write-Alarm "Create file $filename in folder $home\Documents\WindowsPowerShell\Modules\01servers with list of server names." interruption; } } }
PROCESS Block
In theProcedureblock we run this cake of code for each server passed into the pipeline or read from the text file:
- We replace thelocalhostdefault value for the local server with the actual name of the local motorcar.
- Nosotros use Get-ComputerInfo CmdLet to read boosted data about each server (name, environment, logical name, IP address).
- We implement Mistake Handling usingtry-catch blocks and writing errors in an external text file using Write-ErrorLog CmdLet.
- We employ PowerShell splatting to prepare the input parameters for the side by side phone call ofGet-CimInstance CmdLet.
- We call CIM class CIM_OperatingSystem usingGet-CimInstance CmdLet
- We go through each Operating System Bone installed of each server and ready the resultset ofGet-OSInfo CmdLet.
In theProcedureblock, I would similar to emphasize three things used in the code:
i – Splatting
Here is splatting:
$params = @{ 'ComputerName'=$figurer; 'Class'='CIM_OperatingSystem'; 'ErrorAction'='Stop'} #Values are already in kilobytes that is the reason to divide with megabytes to get gigabytes. $OSInfos = Go-CimInstance @params | Select-Object CSName, Caption, CSDVersion, Version, OSArchitecture, InstallDate, LastBootUpTime, SystemDrive, WindowsDirectory, @{Proper name="FreePhysicalMemory";Expression={("{0:N2}" -f($_.FreePhysicalMemory/1mb))}}, @{Name="FreeSpaceInPagingFiles";Expression={("{0:N2}" -f($_.FreeSpaceInPagingFiles/1mb))}}, @{Name="FreeVirtualMemory";Expression={("{0:N2}" -f($_.FreeVirtualMemory/1mb))}}, NumberOfProcesses, NumberOfUsers, @{Name="SizeStoredInPagingFiles";Expression={("{0:N2}" -f($_.SizeStoredInPagingFiles/1mb))}}, @{Name="TotalVirtualMemorySize";Expression={("{0:N2}" -f($_.TotalVirtualMemorySize/1mb))}}, @{Name="TotalVisibleMemorySize";Expression={("{0:N2}" -f($_.TotalVisibleMemorySize/1mb))}}
2 – Effect set up Creation And Type Proper noun Of Result fix
Again for creation of result set I use splatting as y'all can see in the source code:
$properties = @{ 'Environment'=$env; 'Logical proper name'=$logicalname; 'Server name'=$OSInfo.CSName; 'Caption'=$OSInfo.Caption; 'Service pack'=$OSInfo.CSDVersion; 'Version'=$OSInfo.Version; 'Os Compages'=$OSInfo.OSArchitecture; 'Install Appointment'=$OSInfo.InstallDate; 'Last BootUp Time'=$OSInfo.LastBootUpTime; 'Organisation drive'=$OSInfo.SystemDrive; 'Windows directory'=$OSInfo.WindowsDirectory; 'Free RAM (GB)'=$OSInfo.FreePhysicalMemory; 'Free space in paging files (GB)'=$OSInfo.FreeSpaceInPagingFiles; 'Free virtual memory (GB)'=$OSInfo.FreeVirtualMemory; 'Number of processes'=$OSInfo.NumberOfProcesses; 'Number of users'=$OSInfo.NumberOfUsers; 'Size stored in paging file (GB)'=$OSInfo.SizeStoredInPagingFiles; 'Full virtual retention size (GB)'=$OSInfo.TotalVirtualMemorySize; 'Total visible memory size (GB)'=$OSInfo.TotalVisibleMemorySize; 'IP'=$ip; 'Collected'=(Get-Engagement -UFormat %Y.%grand.%d' '%H:%M:%S)} $obj = New-Object -TypeName PSObject -Property $properties $obj.PSObject.TypeNames.Insert(0,'Report.OSInfo')
In addition, I give a name (Report.OSInfo) to the type of upshot set as y'all can see in the code:
$obj.PSObject.TypeNames.Insert(0,'Report.OSInfo')
And then if we phone call PowerShellGet-Member CmdLet to go the blazon of result fix forBecome- OSInfo CmdLet nosotros will become Written report. OSInfo blazon and not standard PowerShell PSObject type.
Get-OSInfo -client "OK" -solution "FIN" | Get-Member
Hither is the result:

Type Name of result set gives us a possibility to further expend the utilise of the result set. For case, we can use MS SQL Database and create tabular array OSInfo based on result set type proper name (Report.OSInfo ) where we can record the result set and employ MS SQL Reporting Services to evidence the results collected over a sure menstruation of fourth dimension.
iii – Convert RAM Size in Gigabytes [GB]
Notice how we accept used the expression to catechumen all the values from Kilobytes [KB] into Gigabytes [GB] which is the norm nowadays and much meliorate representation than default values delivered:
$OSInfos = Get-CimInstance @params | Select-Object CSName, Caption, CSDVersion, Version, OSArchitecture, InstallDate, LastBootUpTime, SystemDrive, WindowsDirectory, @{Name="FreePhysicalMemory";Expression={("{0:N2}" -f($_.FreePhysicalMemory/1mb))}}, @{Name="FreeSpaceInPagingFiles";Expression={("{0:N2}" -f($_.FreeSpaceInPagingFiles/1mb))}}, @{Name="FreeVirtualMemory";Expression={("{0:N2}" -f($_.FreeVirtualMemory/1mb))}}, NumberOfProcesses, NumberOfUsers, @{Name="SizeStoredInPagingFiles";Expression={("{0:N2}" -f($_.SizeStoredInPagingFiles/1mb))}}, @{Name="TotalVirtualMemorySize";Expression={("{0:N2}" -f($_.TotalVirtualMemorySize/1mb))}}, @{Name="TotalVisibleMemorySize";Expression={("{0:N2}" -f($_.TotalVisibleMemorySize/1mb))}}
Finally, hither is theProcessblock source lawmaking:
PROCESS { foreach ($computer in $computers ) { if ( $computer -eq 'localhost' ) { $computer = $env:COMPUTERNAME } $computerinfo = Become-ComputerInfo -computername $calculator -client $customer -solution $solution $hostname = $computerinfo.hostname $env = $computerinfo.environment $logicalname = $computerinfo.logicalname $ip = $computerinfo.ipaddress try { Write-Verbose "Start processing: $figurer - $env - $logicalname" Write-Verbose "Get-go CIM_OperatingSystem processing..." $OSInfos = $nada $OSInfo = $null $obj = $null $params = @{ 'ComputerName'=$figurer; 'Class'='CIM_OperatingSystem'; 'ErrorAction'='Stop'} #Values are already in kilobytes that is the reason to separate with megabytes to get gigabytes. $OSInfos = Get-CimInstance @params | Select-Object CSName, Caption, CSDVersion, Version, OSArchitecture, InstallDate, LastBootUpTime, SystemDrive, WindowsDirectory, @{Name="FreePhysicalMemory";Expression={("{0:N2}" -f($_.FreePhysicalMemory/1mb))}}, @{Name="FreeSpaceInPagingFiles";Expression={("{0:N2}" -f($_.FreeSpaceInPagingFiles/1mb))}}, @{Proper name="FreeVirtualMemory";Expression={("{0:N2}" -f($_.FreeVirtualMemory/1mb))}}, NumberOfProcesses, NumberOfUsers, @{Proper name="SizeStoredInPagingFiles";Expression={("{0:N2}" -f($_.SizeStoredInPagingFiles/1mb))}}, @{Name="TotalVirtualMemorySize";Expression={("{0:N2}" -f($_.TotalVirtualMemorySize/1mb))}}, @{Name="TotalVisibleMemorySize";Expression={("{0:N2}" -f($_.TotalVisibleMemorySize/1mb))}} Write-Verbose "Finish CIM_OperatingSystem processing..." foreach ($OSInfo in $OSInfos) { Write-Verbose "Outset processing OS: $OSInfo" $backdrop = @{ 'Environment'=$env; 'Logical name'=$logicalname; 'Server name'=$OSInfo.CSName; 'Caption'=$OSInfo.Caption; 'Service pack'=$OSInfo.CSDVersion; 'Version'=$OSInfo.Version; 'OS Architecture'=$OSInfo.OSArchitecture; 'Install Date'=$OSInfo.InstallDate; 'Last BootUp Time'=$OSInfo.LastBootUpTime; 'Organisation drive'=$OSInfo.SystemDrive; 'Windows directory'=$OSInfo.WindowsDirectory; 'Free RAM (GB)'=$OSInfo.FreePhysicalMemory; 'Free space in paging files (GB)'=$OSInfo.FreeSpaceInPagingFiles; 'Costless virtual retentivity (GB)'=$OSInfo.FreeVirtualMemory; 'Number of processes'=$OSInfo.NumberOfProcesses; 'Number of users'=$OSInfo.NumberOfUsers; 'Size stored in paging file (GB)'=$OSInfo.SizeStoredInPagingFiles; 'Total virtual retention size (GB)'=$OSInfo.TotalVirtualMemorySize; 'Total visible retentiveness size (GB)'=$OSInfo.TotalVisibleMemorySize; 'IP'=$ip; 'Nerveless'=(Get-Appointment -UFormat %Y.%m.%d' '%H:%1000:%South)} $obj = New-Object -TypeName PSObject -Property $properties $obj.PSObject.TypeNames.Insert(0,'Report.OSInfo') Write-Output $obj Write-Verbose "Finish processing OS: $OSInfo" } Write-Verbose "Finish processing: $calculator - $env - $logicalname" } catch { Write-Warning "Computer failed: $figurer - $env - $logicalname Os failed: $OSInfo" Write-Warning "Error message: $_" if ( $errorlog ) { $errormsg = $_.ToString() $exception = $_.Exception $stacktrace = $_.ScriptStackTrace $failingline = $_.InvocationInfo.Line $positionmsg = $_.InvocationInfo.PositionMessage $pscommandpath = $_.InvocationInfo.PSCommandPath $failinglinenumber = $_.InvocationInfo.ScriptLineNumber $scriptname = $_.InvocationInfo.ScriptName $ErrorArguments = @{ 'hostname' = $computer; 'env' = $env; 'logicalname' = $logicalname; 'errormsg' = $errormsg; 'exception' = $exception; 'stacktrace'= $stacktrace; 'failingline' = $failingline; 'positionmsg' = $positionmsg; 'pscommandpath' = $pscommandpath; 'failinglinenumber' = $failinglinenumber; 'scriptname' = $scriptname } Write-Verbose "Start writing to Error log." Write-ErrorLog @ErrorArguments #Write-ErrorLog -hostname $figurer -env $env -logicalname $logicalname -errormsg $errormsg -exception $exception -scriptname $scriptname -failinglinenumber $failinglinenumber -failingline $failingline -pscommandpath $pscommandpath -positionmsg $pscommandpath -stacktrace $stacktrace Write-Verbose "Terminate writing to Fault log." } } } }
INFO: To larn about PowerShellError Handlingand codedebuggingplease read the following articles: How To Log PowerShell Errors And Much More and How To Debug PowerShell Scripts.
END Block
Endblock is empty.
INFO: To sympathiseBEGIN,ProcedureandENDblocks in PowerShell please read PowerShell Function Begin Process Cease Blocks Explained With Examples.
Comment-Based Help Department
For every one of my own CmdLets, I writeAnnotate-Based help also.
INFO: If yous want to learn how to write comment-based Help for your own PowerShell Functions and Scripts please read these articles How To Write PowerShell Assist (Step past Step). In this article How To Write PowerShell Function's Or CmdLet's Help (Fast), I explain the PowerShell Add together-on that helps united states of america to be fast with writing help content.
How To Use Go- OSInfo CmdLet – Tips
To get Windows Operating Organisation Os details for the local motorcar we just callGet- OSInfo CmdLet and provide values for mandatory parameters (client and solution):
Get-OSInfo -client "OK" -solution "FIN"
Here is the consequence set:

As we take seen in the solution department of this article we can phone callGet- OSInfo CmdLet for the listing of servers:
Get-OSInfo -filename "OKFINservers.txt" -errorlog -client "OK" -solution "FIN" -Verbose | Select-Object 'Surround', 'Logical Name', 'Server Name', 'Caption', 'Service pack', 'Version', 'OS Architecture', 'Install Date', 'Last BootUp Time', 'Organization bulldoze', 'Windows directory', 'Free RAM (GB)', 'Free space in paging files (GB)', 'Gratis virtual retentiveness (GB)', 'Number of processes', 'Number of users', 'Size stored in paging file (GB)', 'Total virtual memory size (GB)', 'Full visible memory size (GB)', 'IP', 'Collected' | Out-GridView
Hither is the result set for list of servers:

We can PowerShell pipeline upshot fromGet- OSInfo CmdLet intoSave-ToExcel CmdLet and get the issue set every bit an Excel Sail:
Get-OSInfo -filename "OKFINservers.txt" -errorlog -client "OK" -solution "FIN" -Verbose | Select-Object 'Environment', 'Logical Name', 'Server Proper noun', 'Caption', 'Service pack', 'Version', 'OS Architecture', 'Install Date', 'Last BootUp Time', 'Organization drive', 'Windows directory', 'Costless RAM (GB)', 'Free space in paging files (GB)', 'Free virtual memory (GB)', 'Number of processes', 'Number of users', 'Size stored in paging file (GB)', 'Full virtual memory size (GB)', 'Total visible retention size (GB)', 'IP', 'Collected' | Save-ToExcel -errorlog -ExcelFileName "Go-OSInfo" -title "Get OS info of servers in Fiscal solution for " -writer "Dejan Mladenovic" -WorkSheetName "OS Info" -client "OK" -solution "FIN"
Hither is the Excel Canvass result:

Bonus Tip: I accept usedBecome- OSInfo CmdLet for documentation purposes while making a PDF document for the customer where we have installed our solution.
Useful PowerShell Windows Operating System Properties Data Articles
Here are some useful articles and resource:
- Get-CimInstance
- Get-CimClass
- CIM_OperatingSystem course
- Win32_OperatingSystem grade
Go- OSInfo CmdLet Source Lawmaking
DISCLAIMER:Get- OSInfo function is function of the Efficiency Booster PowerShell Projection and as such utilize other CmdLets that are part of the same projection. Then the best pick for you in social club for this part to piece of work without whatsoever additional customization is to download the source code of the whole project from hither.
INFO: My best advice to every PowerShell scripter is to learn writing ownPowerShell Avant-garde Functions andCmdLetsand I have written several articles explaining this, so please read them. How To Create A Custom PowerShell CmdLet (Footstep By Pace). Hither I explain how to use PowerShell Add-on Function to exist faster in writing PowerShell Functions How To Write Avant-garde Functions Or CmdLets With PowerShell (Fast).
Hither is the source code of the wholeGet- OSInfo CmdLet:
<# .SYNOPSIS Get Operating Organisation info. .DESCRIPTION Gets Operating organization info for list of servers. List of servers is in txt file in 01servers binder or list of strings with names of computers. CmdLet has two ParameterSets i for list of computers from file and another from list of strings as figurer names. Errors volition be saved in log binder PSLogs with name Error_Log.txt. Parameter errorlog controls logging of errors in log file. Get-OSInfo part uses Get-CimInstance -Grade CIM_OperatingSystem PowerShell function to become Bone info. Result shows post-obit columns: Environment (PROD, Acceptance, Test, Form...), LogicalName (Application, web, integration, FTP, Scan, Last Server...), ServerName, Caption, CSDVersion, Version, OSArchitecture, Install Date, Terminal BootUp Time, SystemDrive, WindowsDirectory, FreePhysicalMemory, FreeSpaceInPagingFiles, FreeVirtualMemory, NumberOfProcesses, NumberOfUsers, SizeStoredInPagingFile, TotalVirtualMemorySize, TotalVisibleMemorySize, IP .PARAMETER computers List of computers that we want to go Bone Info from. Parameter belongs to default Parameter Gear up = ServerNames. .PARAMETER filename Proper name of txt file with list of servers that we want to check OS info. .txt file should exist in 01servers folder. Parameter belongs to Parameter Ready = FileName. .PARAMETER errorlog Switch parameter that sets to write to log or not to write to log. Mistake file is in PSLog folder with proper name Error_Log.txt. .PARAMETER client OK - O client BK - B customer etc. .PARAMETER solution FIN - Financial HR - Humane resource etc. .Example Become-OSInfo -client "OK" -solution "FIN" Description --------------------------------------- Test of default parameter with default value ( computers = 'localhost' ) in default ParameterSet = ServerName. .EXAMPLE Go-OSInfo -client "OK" -solution "FIN" -Verbose Clarification --------------------------------------- Examination of Verbose parameter. Note: Detect how localhost default value of parameter computers replaces with name of server. .Instance 'Fault' | Get-OSInfo -client "OK" -solution "FIN" -errorlog Description --------------------------------------- Test of errorlog parameter. At that place is no server with proper name ERROR so this phone call will fail and write to Error log since errorlog switch parameter is on. Look Error_Log.txt file in PSLogs folder. .EXAMPLE Go-OSInfo -computers 'APP100001' -client "OK" -solution "FIN" -errorlog Description --------------------------------------- Examination of computers parameter with one value. Parameter accepts array of strings. .EXAMPLE Become-OSInfo -computers 'APP100001', 'APP100002' -customer "OK" -solution "FIN" -errorlog -Verbose Description --------------------------------------- Test of computers parameter with array of strings. Parameter accepts array of strings. .Case Become-OSInfo -hosts 'APP100001' -customer "OK" -solution "FIN" -errorlog Clarification --------------------------------------- Test of computers paramater allonym hosts. .Case Become-OSInfo -computers (Become-Content( "$home\Documents\WindowsPowerShell\Modules\01servers\OKFINservers.txt" )) -client "OK" -solution "FIN" -errorlog -Verbose Description --------------------------------------- Test of computers parameter and values for parameter comes from .txt file that has list of servers. .EXAMPLE 'APP100001' | Get-OSInfo -client "OK" -solution "FIN" -errorlog Description --------------------------------------- Test of pipeline past value of computers parameter. .EXAMPLE 'APP100001', 'APP100002' | Get-OSInfo -client "OK" -solution "FIN" -errorlog -Verbose Description --------------------------------------- Examination of pipeline by value with array of strings of computers parameter. .Instance 'APP100001', 'APP100002' | Select-Object @{label="computers";expression={$_}} | Become-OSInfo -client "OK" -solution "FIN" -errorlog Description --------------------------------------- Examination of values from pipeline by belongings name (computers). .EXAMPLE Get-Content( "$domicile\Documents\WindowsPowerShell\Modules\01servers\OKFINservers.txt" ) | Get-OSInfo -customer "OK" -solution "FIN" -errorlog -Verbose Description --------------------------------------- Test of pipeline by value that comes every bit content of .txt file with list of servers. .EXAMPLE Help Go-OSInfo -Full Description --------------------------------------- Test of Powershell help. .EXAMPLE Get-OSInfo -filename "OKFINservers.txt" -errorlog -customer "OK" -solution "FIN" -Verbose Description --------------------------------------- This is test of ParameterSet = FileName and parameter filename. There is list of servers in .txt file. .Instance Get-OSInfo -file "OKFINserverss.txt" -errorlog -customer "OK" -solution "FIN" -Verbose Description --------------------------------------- This is test of ParameterSet = FileName and parameter filename. This examination will neglect due to wrong name of the .txt file with warning message "WARNING: This file path does NOT be:". .INPUTS Organisation.String Computers parameter pipeline both by Value and by Property Name value and has default value of localhost. (Parameter Ready = ComputerNames) Filename parameter does not pipeline and does non accept default value. (Parameter Set = FileName) .OUTPUTS System.Management.Automation.PSCustomObject Get-OSInfo returns PSCustomObjects which has been converted from PowerShell function Get-CimInstance -Class CIM_OperatingSystem Upshot shows following columns: Surround (PROD, Acceptance, Test, Course...), LogicalName (Application, web, integration, FTP, Browse, Terminal Server...), ServerName, Explanation, CSDVersion, Version, OSArchitecture, Install Date, Last BootUp Time, SystemDrive, WindowsDirectory, FreePhysicalMemory, FreeSpaceInPagingFiles, FreeVirtualMemory, NumberOfProcesses, NumberOfUsers, SizeStoredInPagingFile, TotalVirtualMemorySize, TotalVisibleMemorySize, IP .NOTES FunctionName : Get-OSInfo Created by : Dejan Mladenovic Date Coded : 10/31/2018 19:06:41 More than info : https://improvescripting.com/ .LINK How To Get Windows Operating Organization Details Using PowerShell
Become-CimInstance -Class CIM_OperatingSystem Get-CimInstance -Form Win32_OperatingSystem #> Function Get-OSInfo { [CmdletBinding(DefaultParametersetName="ServerNames")] param ( [Parameter( ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true, ParameterSetName="ServerNames", HelpMessage="List of figurer names separated by commas.")] [Allonym('hosts')] [cord[]]$computers = 'localhost', [Parameter( ParameterSetName="FileName", HelpMessage="Name of txt file with listing of servers. Txt file should exist in 01servers folder.")] [string]$filename, [Parameter( Mandatory=$false, HelpMessage="Write to error log file or non.")] [switch]$errorlog, [Parameter(Mandatory=$true, HelpMessage="Client for example OK = O client, BK = B client")] [cord]$customer, [Parameter(Mandatory=$true, HelpMessage="Solution, for example FIN = Financial, Hr = Homo Resource")] [string]$solution ) Begin { if ( $PsCmdlet.ParameterSetName -eq "FileName") { if ( Test-Path -Path "$domicile\Documents\WindowsPowerShell\Modules\01servers\$filename" -PathType Foliage ) { Write-Verbose "Read content from file: $filename" $computers = Get-Content( "$home\Documents\WindowsPowerShell\Modules\01servers\$filename" ) } else { Write-Alarm "This file path does Not exist: $home\Documents\WindowsPowerShell\Modules\01servers\$filename" Write-Alarm "Create file $filename in folder $home\Documents\WindowsPowerShell\Modules\01servers with list of server names." break; } } } PROCESS { foreach ($computer in $computers ) { if ( $computer -eq 'localhost' ) { $computer = $env:COMPUTERNAME } $computerinfo = Get-ComputerInfo -computername $computer -client $client -solution $solution $hostname = $computerinfo.hostname $env = $computerinfo.environment $logicalname = $computerinfo.logicalname $ip = $computerinfo.ipaddress endeavour { Write-Verbose "Showtime processing: $computer - $env - $logicalname" Write-Verbose "Get-go CIM_OperatingSystem processing..." $OSInfos = $null $OSInfo = $null $obj = $null $params = @{ 'ComputerName'=$computer; 'Grade'='CIM_OperatingSystem'; 'ErrorAction'='Stop'} #Values are already in kilobytes that is the reason to divide with megabytes to become gigabytes. $OSInfos = Get-CimInstance @params | Select-Object CSName, Caption, CSDVersion, Version, OSArchitecture, InstallDate, LastBootUpTime, SystemDrive, WindowsDirectory, @{Proper name="FreePhysicalMemory";Expression={("{0:N2}" -f($_.FreePhysicalMemory/1mb))}}, @{Name="FreeSpaceInPagingFiles";Expression={("{0:N2}" -f($_.FreeSpaceInPagingFiles/1mb))}}, @{Proper name="FreeVirtualMemory";Expression={("{0:N2}" -f($_.FreeVirtualMemory/1mb))}}, NumberOfProcesses, NumberOfUsers, @{Name="SizeStoredInPagingFiles";Expression={("{0:N2}" -f($_.SizeStoredInPagingFiles/1mb))}}, @{Name="TotalVirtualMemorySize";Expression={("{0:N2}" -f($_.TotalVirtualMemorySize/1mb))}}, @{Name="TotalVisibleMemorySize";Expression={("{0:N2}" -f($_.TotalVisibleMemorySize/1mb))}} Write-Verbose "Cease CIM_OperatingSystem processing..." foreach ($OSInfo in $OSInfos) { Write-Verbose "Start processing Bone: $OSInfo" $backdrop = @{ 'Environment'=$env; 'Logical name'=$logicalname; 'Server proper name'=$OSInfo.CSName; 'Caption'=$OSInfo.Caption; 'Service pack'=$OSInfo.CSDVersion; 'Version'=$OSInfo.Version; 'OS Architecture'=$OSInfo.OSArchitecture; 'Install Date'=$OSInfo.InstallDate; 'Last BootUp Time'=$OSInfo.LastBootUpTime; 'System bulldoze'=$OSInfo.SystemDrive; 'Windows directory'=$OSInfo.WindowsDirectory; 'Free RAM (GB)'=$OSInfo.FreePhysicalMemory; 'Free infinite in paging files (GB)'=$OSInfo.FreeSpaceInPagingFiles; 'Gratis virtual retentivity (GB)'=$OSInfo.FreeVirtualMemory; 'Number of processes'=$OSInfo.NumberOfProcesses; 'Number of users'=$OSInfo.NumberOfUsers; 'Size stored in paging file (GB)'=$OSInfo.SizeStoredInPagingFiles; 'Total virtual retentiveness size (GB)'=$OSInfo.TotalVirtualMemorySize; 'Full visible memory size (GB)'=$OSInfo.TotalVisibleMemorySize; 'IP'=$ip; 'Nerveless'=(Get-Date -UFormat %Y.%one thousand.%d' '%H:%One thousand:%S)} $obj = New-Object -TypeName PSObject -Property $properties $obj.PSObject.TypeNames.Insert(0,'Report.OSInfo') Write-Output $obj Write-Verbose "Finish processing OS: $OSInfo" } Write-Verbose "Finish processing: $computer - $env - $logicalname" } catch { Write-Warning "Computer failed: $estimator - $env - $logicalname OS failed: $OSInfo" Write-Warning "Error message: $_" if ( $errorlog ) { $errormsg = $_.ToString() $exception = $_.Exception $stacktrace = $_.ScriptStackTrace $failingline = $_.InvocationInfo.Line $positionmsg = $_.InvocationInfo.PositionMessage $pscommandpath = $_.InvocationInfo.PSCommandPath $failinglinenumber = $_.InvocationInfo.ScriptLineNumber $scriptname = $_.InvocationInfo.ScriptName $ErrorArguments = @{ 'hostname' = $computer; 'env' = $env; 'logicalname' = $logicalname; 'errormsg' = $errormsg; 'exception' = $exception; 'stacktrace'= $stacktrace; 'failingline' = $failingline; 'positionmsg' = $positionmsg; 'pscommandpath' = $pscommandpath; 'failinglinenumber' = $failinglinenumber; 'scriptname' = $scriptname } Write-Verbose "Start writing to Mistake log." Write-ErrorLog @ErrorArguments #Write-ErrorLog -hostname $computer -env $env -logicalname $logicalname -errormsg $errormsg -exception $exception -scriptname $scriptname -failinglinenumber $failinglinenumber -failingline $failingline -pscommandpath $pscommandpath -positionmsg $pscommandpath -stacktrace $stacktrace Write-Verbose "Finish writing to Error log." } } } } Terminate { } } #Get-OSInfo -filename "OKFINservers.txt" -errorlog -client "OK" -solution "FIN" -Verbose | Select-Object 'Surroundings', 'Logical Proper name', 'Server Proper name', 'Caption', 'Service pack', 'Version', 'Bone Architecture', 'Install Date', 'Last BootUp Time', 'System drive', 'Windows directory', 'Free RAM (GB)', 'Free space in paging files (GB)', 'Complimentary virtual retentiveness (GB)', 'Number of processes', 'Number of users', 'Size stored in paging file (GB)', 'Total virtual memory size (GB)', 'Total visible memory size (GB)', 'IP', 'Collected' | Out-GridView <# #Test ParameterSet = ServerName Get-OSInfo -client "OK" -solution "FIN" Get-OSInfo -client "OK" -solution "FIN" -errorlog Get-OSInfo -client "OK" -solution "FIN" -errorlog -Verbose Become-OSInfo -computers 'APP100001' -client "OK" -solution "FIN" -errorlog Get-OSInfo -computers 'APP100001', 'APP100002' -client "OK" -solution "FIN" -errorlog -Verbose Get-OSInfo -hosts 'APP100001' -client "OK" -solution "FIN" -errorlog Get-OSInfo -computers (Become-Content( "$home\Documents\WindowsPowerShell\Modules\01servers\OKFINservers.txt" )) -customer "OK" -solution "FIN" -errorlog -Verbose #Pipeline examples 'APP100001' | Get-OSInfo -customer "OK" -solution "FIN" -errorlog 'APP100001', 'APP100002' | Get-OSInfo -customer "OK" -solution "FIN" -errorlog -Verbose 'APP100001', 'APP100002' | Select-Object @{label="computers";expression={$_}} | Get-OSInfo -client "OK" -solution "FIN" -errorlog Get-Content( "$home\Documents\WindowsPowerShell\Modules\01servers\OKFINservers.txt" ) | Go-OSInfo -client "OK" -solution "FIN" -errorlog -Verbose 'ERROR' | Get-OSInfo -client "OK" -solution "FIN" -errorlog #Test CmdLet help Aid Become-OSInfo -Full #SaveToExcel Become-OSInfo -filename "OKFINservers.txt" -errorlog -client "OK" -solution "FIN" -Verbose | Select-Object 'Environs', 'Logical Name', 'Server Name', 'Caption', 'Service pack', 'Version', 'OS Architecture', 'Install Engagement', 'Last BootUp Time', 'System drive', 'Windows directory', 'Gratis RAM (GB)', 'Free space in paging files (GB)', 'Free virtual retentivity (GB)', 'Number of processes', 'Number of users', 'Size stored in paging file (GB)', 'Full virtual retention size (GB)', 'Total visible memory size (GB)', 'IP', 'Nerveless' | Salve-ToExcel -errorlog -ExcelFileName "Go-OSInfo" -championship "Get OS info of servers in Financial solution for " -author "Dejan Mladenovic" -WorkSheetName "Bone Info" -client "OK" -solution "FIN" #SaveToExcel and transport email Get-OSInfo -filename "OKFINservers.txt" -errorlog -customer "OK" -solution "FIN" -Verbose | Select-Object 'Surroundings', 'Logical Name', 'Server Name', 'Caption', 'Service pack', 'Version', 'OS Architecture', 'Install Date', 'Terminal BootUp Time', 'System drive', 'Windows directory', 'Free RAM (GB)', 'Free infinite in paging files (GB)', 'Gratis virtual memory (GB)', 'Number of processes', 'Number of users', 'Size stored in paging file (GB)', 'Full virtual retentiveness size (GB)', 'Total visible retention size (GB)', 'IP', 'Collected' | Salvage-ToExcel -sendemail -errorlog -ExcelFileName "Get-OSInfo" -title "Go Os info of servers in Financial solution for " -writer "Dejan Mladenovic" -WorkSheetName "Os Info" -client "OK" -solution "FIN" #Benchmark #Time = iv sec; Full Items = 28 Measure out-BenchmarksCmdLet { Get-OSInfo -filename "OKFINservers.txt" -errorlog -client "OK" -solution "FIN" -Verbose } #Fourth dimension = 3,69 sec; Full Items = 28 Measure-BenchmarksCmdLet { Become-OSInfo -filename "OKFINservers.txt" -errorlog -client "OK" -solution "FIN" } #Baseline create Get-OSInfo -filename "OKFINservers.txt" -errorlog -client "OK" -solution "FIN" -Verbose | Save-Baseline -errorlog -BaselineFileName "Get-OSInfo" -customer "OK" -solution "FIN" -Verbose #Baseline annal and create new Go-OSInfo -filename "OKFINservers.txt" -errorlog -client "OK" -solution "FIN" -Verbose | Save-Baseline -archive -errorlog -BaselineFileName "Get-OSInfo" -customer "OK" -solution "FIN" -Verbose #Test ParameterSet = FileName Get-OSInfo -filename "OKFINservers.txt" -errorlog -customer "OK" -solution "FIN" -Verbose Go-OSInfo -filename "OKFINserverss.txt" -errorlog -client "OK" -solution "FIN" -Verbose #>
Source: https://www.improvescripting.com/how-to-get-windows-operating-system-details-using-powershell/
Posted by: baileyworters.blogspot.com
0 Response to "How To Find The Windows Operating System"
Post a Comment