How to format output to DD, HH, MM - powershell

I'm trying to work out a devices up time using PowerShell. My code is as so:
$wmi = Get-WmiObject -Class Win32_OperatingSystem
$upTime = $wmi.ConvertToDateTime($wmi.LocalDateTime) – $wmi.ConvertToDateTime($wmi.LastBootUpTime)
When I call $upTime, it returns the following:
Days : 0
Hours : 1
Minutes : 8
Seconds : 5
Milliseconds : 311
Ticks : 40853110010
TotalDays : 0.0472836921412037
TotalHours : 1.13480861138889
TotalMinutes : 68.0885166833333
TotalSeconds : 4085.311001
TotalMilliseconds : 4085311.001
While I can see the up time, I need to specifically format the output to D:dd, H:hh, M:mm as I will be automatically feeding this into a monitoring system, but after much searching on here, google etc, I can't see how to achieve this. Can anyone suggest how to go about doing this?

Try:
"D:{0:dd}, H:{0:hh}, M:{0:mm}" -f $upTime
alternatively, if you like to escape lots of things:
$upTime.ToString('\D\:dd\,\ \H\:hh\,\ \M\:mm')
This will format the string to have 2 leading zeros when ether days, hours or seconds is 0. And 1 leading zero if they are 1-9.
"D:01, H:02, M:23"

You can format it in normal .NET style
$UptimeStr = $Uptime.ToString("ddd:dd, H:hh, mmm:mm")
or
$UptimeStr = '{0:ddd:dd, H:hh, mmm:mm}' -f $Uptime
If the above formatting is not what you wanted the formatting is explained on MSDN

Related

Powershell get timezone with DST

In powershell I'm using
(Get-Timezone).BaseUtcOffset
to get the UTC offset of a computer which gives me +1h for my timezone. That is technically correct since I'm in CET in winter (UTC+1) and CEST in summer (UTC+2). Right now tho it is DST, so CEST (UTC+2) for me so I'm wondering how I could get this information in powershell since the above command tells me that my timezone is UTC+1 and doesn't mention DST at all.
As a workaround I currently use
$date = Get-Date
($date - $date.ToUniversalTime()).TotalMinutes
to get the offset from UTC of my timezone with DST. It evaluates to +120 minutes which is exactly what i need.
Output of Get-Timezone:
Id : W. Europe Standard Time
DisplayName : (UTC+01:00) Amsterdam, Berlin, Bern, Rom, Stockholm, Wien
StandardName : Mitteleuropäische Zeit
DaylightName : Mitteleuropäische Sommerzeit
BaseUtcOffset : 01:00:00
SupportsDaylightSavingTime : True
Output of $date - $date.ToUniversalTime():
Days : 0
Hours : 2
Minutes : 0
Seconds : 0
Milliseconds : 0
Ticks : 72000000000
TotalDays : 0,0833333333333333
TotalHours : 2
TotalMinutes : 120
TotalSeconds : 7200
TotalMilliseconds : 7200000
Yes, you can use the static method IsDaylightSavingTime on the TimeZoneInfo class to get this information from a desired DateTime:
$now = [DateTime]::Now
[System.TimeZoneInfo]::Local.IsDaylightSavingTime($now) # Returns $True or $False
thank you Bender the Greatest you got me on the right path. The class System.TimeZoneInfo has a function that does what i want:
[System.TimeZoneInfo]::Local.GetUtcOffset((Get-Date)).TotalMinutes
gives me +120 minutes

Read in a *HUGE* CSV file, export specific columns based on column name in header, output comma delimited data

So, I know I can read in a csv file using import-csv like so:
$test = import-csv BPUSAUV20FARS-1000.csv
I found another stack overflow question which gave me some code to decipher column names, like so:
$columns = $test[0].psobject.properties.name
I found a reddit post that helped me find a way to extract multiple columns using select-object like so:
$properties = #('Index', 'Year', 'Day', 'Time', 'Line', 'Beam', 'Pos TPU', 'Depth TPU', 'Status')
$test |Select-Object $properties
But the output from the above command likes like this:
Index : 1
Year : EM2040-0073-1000-20200224-222235
Day : 25
Time : Accept
Line : 0.648
Beam : 24-FEB-2020:22:22:34.98
Pos TPU : 4.617
Depth TPU : 1124834.70
Status : 10247261.01
Index : 2
Year : EM2040-0073-1000-20200224-222235
Day : 26
Time : Accept
Line : 0.749
Beam : 24-FEB-2020:22:22:34.98
Pos TPU : 4.617
Depth TPU : 1124834.73
Status : 10247261.76
Index : 3
Year : EM2040-0073-1000-20200224-222235
Day : 27
Time : Accept
Line : 0.624
Beam : 24-FEB-2020:22:22:34.98
Pos TPU : 4.617
Depth TPU : 1124834.76
Status : 10247263.05
And what I need is this:
1,EM2040-0073-1000-20200224-222235,25,Accept,0.648,24-FEB-2020:22:22:34.98,4.617,1124834.70,10247261.01
I also need to be able to perform these actions on a few hundred files with several million lines each. The smallest file is about 2.4 million lines.
As for...
I also need to be able to perform these actions on a few hundred files
with several million lines each. The smallest file is about 2.4
million lines.
... dealing with large files in general --- (too long for just a comment)
As per MSDN
[IO.File]::OpenText and as noted in another Q&A
The Get-Content cmdlet does not perform as well as a StreamReader when
dealing with very large files. You can read a file line by line using
a StreamReader like this:
$path = 'C:\A-Very-Large-File.txt'
$r = [IO.File]::OpenText($path)
while ($r.Peek() -ge 0) {
$line = $r.ReadLine()
# Process $line here...
}
$r.Dispose()
Some performance comparisons:
Measure-Command {Get-Content .\512MB.txt > $null}
Total Seconds: 49.4742533
Measure-Command {
$r = [IO.File]::OpenText('512MB.txt')
while ($r.Peek() -ge 0) {
$r.ReadLine() > $null
}
$r.Dispose()
}
Total Seconds: 27.666803

print strings and variables in powershell for loop

I need to extract an alerts from rest api and sent it to a file with powershell
I was able to extract the alerts outputs looping the xml file:
foreach ($c in $temp){$c.timeOfAlertFormatted,$c.parent,$c.child,$c.category,$c.servicePlanDisplayName,$c.message}
Thu 09/19/2019 12:00:19 AM
IL
Servername
Phase Failure
Gold
One or more source luns do not have a remote target specified/mapped.
Wed 09/18/2019 02:18:25 PM
IL
Server2
Phase Failure
Gold
One or more source luns do not have a remote target specified/mapped
I am new to PS , what i want to achieve is to add descriptive string
to each filed, i.e:
Time: Thu 09/19/2019 12:00:19 AM
Country: IL
Server: servername
etc ,the rest of the fields.
i tried :
foreach ($c in $temp){Write-Host "Time : $($c.timeOfAlertFormatted)"}
Time :
Time :
Time :
Time :
Time :
Time :
Time :
Time :
Time :
Time :
Time :
Time :
Time : Thu 09/19/2019 12:00:19 AM
its printing empty "Time" fields
here is example of the xml:
It looks like you have already loaded the xml and filtered out the properties you need in a variable $temp.
I think what you want can be achieved by doing:
$temp | Select-Object #{Name = 'Time'; Expression = {$_.timeOfAlertFormatted}},
#{Name = 'Country'; Expression = {$_.parent}},
#{Name = 'ServerName'; Expression = {$_.child}},
Category,ServicePlanDisplayName, Message
The above should output something like
Time : Thu 09/19/2019 12:00:19 AM
Country : IL
ServerName : Servername
Category : Phase Failure
ServicePlanDisplayName : Gold
Message : One or more source luns do not have a remote target specified/mapped.
Time : Wed 09/18/2019 02:18:25 PM
Country : IL
ServerName : Server2
Category : General Failure
ServicePlanDisplayName : Gold
Message : One or more source luns do not have a remote target specified/mapped.
If your variable $temp is NOT what I suspect it to be, please edit your question and show us the XML aswell as the code you use to extract the alerts from it.

Improving the speed of PowerShell commands in Windows 2016

Example of a problem:
Measure-Command { Get-VMSwitch -SwitchType "External" }
Windows 2012 and 2016 have the same hardware and CPU load is ~50%
Windows Server 2016 (3 external Switches)
Days : 0
Hours : 0
Minutes : 0
Seconds : 6
Milliseconds : 377
Ticks : 63779086
TotalDays : 7.38183865740741E-05
TotalHours : 0.00177164127777778
TotalMinutes : 0.106298476666667
TotalSeconds : 6.3779086
TotalMilliseconds : 6377.9086
Windows Server 2012R2 (3 external Switches)
Days : 0
Hours : 0
Minutes : 0
Seconds : 1
Milliseconds : 376
Ticks : 13762494
TotalDays : 1.59288125E-05
TotalHours : 0.0003822915
TotalMinutes : 0.02293749
TotalSeconds : 1.3762494
TotalMilliseconds : 1376.2494
Windows 2012R2 with a greater or equal CPU load is 6 times faster.
Windows Server 2016 (9 external Switches)
Days : 0
Hours : 0
Minutes : 1
Seconds : 6
Milliseconds : 168
Ticks : 661689307
TotalDays : 0.000765844105324074
TotalHours : 0.0183802585277778
TotalMinutes : 1.10281551166667
TotalSeconds : 66.1689307
TotalMilliseconds : 66168.9307
Windows 2016 slower now 48 times! :)
In Windows 2016 Meltdown/Specter fixes are disabled.
Is there any option for improving the performance of powershell commands in Windows 2016?
Thanks.
Well, I found another solution. It's using bottlenecks through WMI.
The WMI msvm-ethernetswitchport class can provide all the VMSwitches elements, where we can already get the same information that Powershel CMD Get-VMSwitch provides.
https://learn.microsoft.com/en-us/windows/desktop/hyperv_v2/msvm-ethernetswitchport and etc.
A simple example:
ManagementScope scope = new ManagementScope("\\\\.\\ROOT\\virtualization\\v2");
ObjectQuery query = new ObjectQuery("SELECT * FROM Msvm_EthernetSwitchPort");
ManagementObjectSearcher searcher =
new ManagementObjectSearcher(scope, query);
ManagementObjectCollection queryCollection = searcher.Get();
foreach (ManagementObject m in queryCollection)
{
Console.WriteLine("DeviceID : {0}", m["DeviceID"]);
Console.WriteLine("ElementName : {0}", m["ElementName"]);
}
It's sad that PowerShell in Windows 2016 runs so slowly...
In fact, through WMI there are so many options how to get this data.
https://learn.microsoft.com/en-us/windows/desktop/hyperv_v2/hyper-v-networking-api

powershell listing Ram information

I am trying to find out what type of RAM is in a computer. I'd like to know whether the modules are UDIMM, RDIMM, LRDIMM or any other type. This would be very useful so I can plan what modules are going into what servers at the data center.
This is what I have so far:
Get-WmiObject Win32_PhysicalMemory |
Select-Object PSComputerName, DeviceLocator, Manufacturer, PartNumber,
#{ label = "Size/GB"; expression = { $_.Capacity / 1GB } },
Speed, Datawidth, TotalWidth |
Format-Table -AutoSize
This is useful information but I'd like a column that tells me the type (UDIMM, RDIMM, LRDIMM) and a way to list the DIMMS that are empty as it will make it easier to see what DIMM is full/empty (but this isn't a huge problem).
this is not a duplicate as i am looking for the type of Ram that is stored on the servers so i know what Ram to buy and put in without having to travel a long way to find out i got the wrong type
The Win32_PhysicalMemory class documentation contains a reference for translating the MemoryType value to the type of memory module. Turn it into a hashtable for easy access:
$MemoryTypeMap = #{
"0" = 'Unknown'
"1" = 'Other'
"2" = 'DRAM'
"3" = 'Synchronous DRAM'
"4" = 'Cache DRAM'
"5" = 'EDO'
"6" = 'EDRAM'
"7" = 'VRAM'
"8" = 'SRAM'
"9" = 'RAM'
"10" = 'ROM'
"11" = 'Flash'
"12" = 'EEPROM'
"13" = 'FEPROM'
"14" = 'EPROM'
"15" = 'CDRAM'
"16" = '3DRAM'
"17" = 'SDRAM'
"18" = 'SGRAM'
"19" = 'RDRAM'
"20" = 'DDR'
"21" = 'DDR2'
"22" = 'DDR2 FB-DIMM'
"24" = 'DDR3'
"25" = 'FBD2'
}
Get-WmiObject Win32_PhysicalMemory |Select #{Label = 'Type';Expression = {$MemoryTypeMap["$($_.MemoryType)"]}}
You can see which memory devices are in which slots by using the WMI class of CIM_PhyicalMemoryArray and CIM_MemoryDeviceLocation. I've used WBEMTest and Get-CimInstance, however, and it doesn't seem that Windows reports on which memory slots are open on the motherboard.
My best guess? You'll need to use the Win32_Baseboard class to report on the model of the motherboard, and do some legwork manually to see how many slots each model has. You can then use the data from CIM_MemoryDeviceLocation to determine how many slots are open.
An approach to do so might look like this.
$memorySlots = Get-CimInstance Win32_MemoryDeviceLocation
$motherBoard = Get-CimInstance win32_baseboard
switch ($motherBoard.Product){
#find the motherboard models for the most common models and populate manually w/ count of ram slots
"0TM99H" {$Totalslots = 2}
Default {$Totalslots = 4}
}
Get-WmiObject Win32_PhysicalMemory |
Select-Object PSComputerName, DeviceLocator, Manufacturer, PartNumber,
#{ label = "Size/GB"; expression = { $_.Capacity / 1GB } },
Speed, Datawidth, TotalWidth, #{ label ="FreeSlots";exp={$Totalslots-$memorySlots.Count}}
It would look something like this:
PSComputerName : SLVER
DeviceLocator : DIMM A
Manufacturer : Elpida
PartNumber : 8KTS51264HDZ-1G6E1
Size/GB : 4
Speed : 1600
Datawidth : 64
TotalWidth : 64
FreeSlots : 0
PSComputerName : SLVER
DeviceLocator : DIMM B
Manufacturer : Elpida
PartNumber : 8KTS51264HDZ-1G6E1
Size/GB : 4
Speed : 1600
Datawidth : 64
TotalWidth : 64
FreeSlots : 0
Finally, you asked about memory type present, that also doesn't seem to be info that we know of, from a WMI perspective. Or rather, if WMI does know about it, I couldn't find it anywhere.
I think you'll need to do some manual work too there, gathering the memory part number, then manually research to determine what type of RAM it is, then finally add that info to the output by adding another Switch statement, as shown.
switch ($memorySpecs.PartNumber){
"8KTS51264HDZ-1G6E1" {$RAMType='SoDimm'}
Default {$RAMType="Unknown, research $($memorySpecs.PartNumber)"}
}
Update
Mathias provided an excellent method to look up RAM if the BIOS on that PC reports it to Windows. I've tested on a few systems, some report their RAM Type while others don't. For those that don't, you can reference the partNumber property as I've demoed above to manually look it up. After a few iterations, you should be able to gather the data and program your own exceptions, and wrap up this task.