Powershell ConvertToDateTime() Method and Get-WMiObject - powershell

I am generating a report in PS and everything works fine, but when I try to use ConvertToDateTime to make the date readable it comes out blank? But only on Get-WmiObject Win32_Product others like Win32_Operatingsystem seem to come out fine.
$Install = #{n="Installed";e={$_.ConvertToDateTime($_.InstallDate)}}
$frag14 = Get-WmiObject Win32_Product -ComputerName $name | Select Name,Version,PackageName,$Install,Vendor | Sort Name |
ConvertTo-html -Fragment

InstallDate is just a string, it's a CIM based date string. Try this:
$Install = #{n="Installed";e={[DateTime]::ParseExact($_.InstallDate,'yyyyMMdd',$null)}}

You will not get install date in win32_product class, it is a known bug.

According to the class definition of Win32_Product see MSDN InstallDate is a just a string, not a CIM based string. The string contains a date like format yyyyMMdd, ie "20170417". InstallDate2 is a CIM base datetime string. Specifically, it is a CIM_DateTime datatype which should contain a value in this format yyyymmddHHMMSS.mmmmmmsUUU where sUUU is time zone offset in minutes. ie 20170417101205.000000-420.
This Win32_Product class is recommended to vendors to populate, but sadly InstallDate2 is not required and generally always empty. Partial definition of Win32_Product class from MSDN.
class Win32_Product : CIM_Product
{ uint16 AssignmentType;
string Caption;
string Description;
string IdentifyingNumber;
string InstallDate;
datetime InstallDate2;
...
The following Powershell command lists all of your installed products with install dates (warning very slow).
Get-WmiObject -Class Win32_Product | Select-Object -Property Name, InstallDate, InstallDate2

Related

Powershell WMI get software older than 30 days

i'm trying to get all software updates from configMgr in WMI, that are older than 30 days, i just cant seem to figure it out.
Timeformat in WMI is : DateRevised : 20121113180000.000000+000
I tried the following :
$Date30DaysAgo = (get-date).AddDays(-30).ToString("yyyymmddHHMMSS")
Get-WmiObject -Namespace "root\SMS\site_PS1" -Class SMS_SoftwareUpdate -ComputerName SRVSCCM01 -Filter {"DateRevised <= '$($Date30DaysAgo)%'"}
This is working, but i need to get it working, with -Filter and not where-object
$Date30DaysAgo = (get-date).AddDays(-30)
Get-WmiObject -Namespace "root\SMS\site_PS1" -Class SMS_SoftwareUpdate -ComputerName SRVSCCM01 | Where-Object {$_.ConvertToDateTime($_.DateRevised) -lt $Date30DaysAgo }
It's probably because i cant get the time conversion and the WQL less than operator to work.. can someone point me in the right direction ?
You could try to put $Date30DaysAgo in WMI timestamp format using:
$Date30DaysAgo = ([wmi]"").ConvertFromDateTime((get-date).AddDays(-30))
or use
$Date30DaysAgo = [System.Management.ManagementDateTimeConverter]::ToDmtfDateTime(((get-date).AddDays(-30)).ToUniversalTime())
and use that value in your filter

Select network interface to be used for getting IP information using WMI

I'm creating a script that finds and displays IP configuration information. I have managed to create the variables to get the information and to display it:
$ip = (Get-WmiObject -Class win32_networkadapterconfiguration -Filter "index = '12'").IPAddress[0]
$subnet = (Get-WmiObject -Class win32_networkadapterconfiguration -Filter "index = '12'").IPSubnet[0]
$gateway = (Get-WmiObject -Class win32_networkadapterconfiguration -Filter "index = '12'").DefaultIPGateway
$dns = (Get-WmiObject -Class win32_networkadapterconfiguration -Filter "index = '12'").DNSServerSearchOrder
write-host
write-host
write-host "IP address is: $ip"
write-host "Subnet Mask is: $subnet"
write-host "Default Gateway is: $gateway"
write-host "DNS is: $dns"
I'm filtering the interface by index number, which for my case the number is 12 for the right interface. I want to prompt the user with a list of network interfaces. Get-NetAdapter command should not be used because it doesn't work on PowerShell v2. The user must be able to see current interfaces including descriptions and be able to select one netowrk adapter interface. The selected interface must be put into a variable i.e. $interface. The variable must output the corresponding adapter index number. After that, I am able to place the variable in the filter command like: -Filter "index = '$interfaceindexnumber'" which should normally display IP information for the selected interface.
Feel free to use a different filter if you believe index number is not the right solution for this case. Please, make sure to use commands that work with PowerShell v2.
Thank You!
Based on the description of what you are trying to do, which is to allow a user to select an interface that is later used to provide information to you or your script, I took your script and scrubbed it a little bit as well as added in Out-GridView which is great for presenting information to a user. All of these commands should work in PowerShell v2.
I started out by doing just one WMI query and storing it in a variable. Then I pass filtered information to the Out-GridView cmdlet for the user to select whichever interface they want or need to select. I chose to only show the user Description, IPAddress, and Index to prevent too much confusion.
After the user makes the selection, it is assigned to $interface. To extract the full information that was originally queried from win32_networkadapterconfiguration I used the index number of the users selection to filter against $AllNetInterfaces. This could be simplified by just passing all of the information to the Out-GridView to begin with, but again since you plan on having users make selections and such, less is more.
Finally, I call the properties of the object without using the "Write-Host" function because at this point I am validating that it worked and I am not really sure what you are going to do with it later on.
$AllNetInterfaces = Get-WmiObject -Class win32_networkadapterconfiguration -Property *
$Interface = $AllNetInterfaces | Select Description, IPAddress, Index | Out-GridView -Title "Available Network Adapters" -OutputMode Single
$Interface = $AllNetInterfaces | ? {$_.Index -match $Interface.Index}
$Interface.IPAddress[0]
$Interface.IPSubnet[0]
$Interface.DefaultIPGateway
$Interface.DNSServerSearchOrder
If what you are trying to do is show the user the IP information without using ipconfig, then you could use the Out-GridView cmdlet once again, like this:
$Interface | Select IPAddress, IPSubnet, DefaultIPGateway, DNSServerSearchOrder | Out-GridView -Title "IP Info for: $($Interface.Description)"
Get-WmiObject -Class win32_networkadapterconfiguration | Select Index, #{n="IP"; e={$_.ipaddress[0]}}, #{n="Subnet"; e={$_.IPSubnet[0]}}, Description | Format-Table -AutoSize
[int]$ifindex = Read-Host "Select Network Adapter by Index Number"
$adapters = Get-WmiObject -Class win32_networkadapterconfiguration -Filter "index = '$ifindex'"
foreach ($adapter in $adapters){
$ip = $adapter.IPAddress[0]
$subnet = $adapter.IPSubnet[0]
$gateway = $adapter.DefaultIPGateway
$dns = $adapter.DNSServerSearchOrder
}
$ip
$subnet
$gateway
$dns

Powershell last bootup time of computer showing two different outputs

$OSInfo = get-wmiobject -class win32_operatingsystem -computername c83323
($OSInfo `
| Format-List `
#{Name="OS Boot Time";Expression={$_.ConvertToDateTime($_.LastBootUpTime)}} `
| Out-String).Trim() #Output OS Name, Boot Time, and Install Date
output --> OS Boot Time : 12/6/2016 4:09:20 PM
$osinfo = get-wmiobject -class win32_operatingsystem -computername c83323
$osinfo.ConvertToDateTime($osinfo.LastBootUpTime)
output --> Tuesday, December 06, 2016 4:09:20 PM
Why is it when I run the first set, I get time in one way, but when I run it the 2nd way, I get it in a completely different format?
This is because you are using Format-List and/or Out-String in the first case. When using these, PowerShell formats the DateTime object's output, just as if you wrote this:
"$(Get-Date)"
The output of your second instance will be in DateTime type. This format depends on the datetime format you have Choosen on your system.
I modified your code to get the type:
$osinfo = get-wmiobject -class win32_operatingsystem; ($osinfo.ConvertToDateTime($osinfo.LastBootUpTime)).GetType()
But in your first instance, you use something called a calculated property (check this link) , which basically let you "Format" and display attributes in the way you prefer. In your case, thru the expression you have provided your date and time had converted into an array and hence it lost its format.
Get type:
($OSInfo | Format-List #{Name="OS Boot Time";Expression={$_.ConvertToDateTime($_.LastBootUpTime)}}).GetType()
The above type would be an array.
EDIT:
Below snippet should do the trick!
#("Computer1","Computer2") | foreach {
$OSInfo = get-wmiobject -class win32_operatingsystem -computername $_;
"Boot time for Computer: $_ = " + $OSInfo.ConvertToDateTime($OSInfo.LastBootUpTime);
} | Out-File "C:\thefolderYouPrefer\boottime.txt"

Powershell - need to change only Subnet and GW for specific NIC

I have a situation where I need to change the Subnet and Gateway for about 170 servers but leave the IP address alone. It seems like this would be simple but the problem is that many of these servers have more than one interface on them so I have to be selective about which one is being changed.
Luckily I've named all my interfaces with a standard convention so I can select the interface I need, however since its Windows 2008r2 I'm using WMI.
Here is what my problem is: To collect information about the interface name I have to use NetConnectionID which is included in Win32_NetworkAdapter, but to actually make changes to the interface I have to use Win32_NetworkAdapterConfiguration which doesn't include the ability to select the interface based upon the name.
So what I am trying to do us is use the Index property which is in both collections to tie the information together. I first filter Win32_NetworkAdapter based upon the interface name, obtain the index value, and then attempt to select the correct interface from Win32_NetworkAdapterConfiguration so that I can change the required subnet mask and gateway.
Below is my script; the very specific area that is causing the problem is the section where I try to get the Where{$.Index -eq $Index} to work with a variable. If I set the statement to Where{$.Index -eq 10) it works, but if I try to use the variable of $Index it fails.
Here is my code:
$index = gwmi Win32_NetworkAdapter -Filter "NetConnectionID='Swizzlan'" | Select-Object - Property Index
$NICs = gwmi Win32_NetworkAdapterConfiguration -computername . | where{$_.Index -eq $Index}
Foreach($NIC in $NICs) {
$ip = ($NIC.IPAddress[0])
$gateway = "192.168.1.1"
$subnet = "255.255.252.0"
$NIC.EnableStatic($ip, $subnet)
$NIC.SetGateways($gateway)
}
Can anyone tell me how to solve this problem? After hours and hours of searching has not turned up the solution to me and I know it has to be something simple.
Win32_NetworkAdapter value "DeviceID" is equivalent to Win32_NetworkAdapterConfiguration value "Index".
So, in your code try:
$deviceID = gwmi Win32_NetworkAdapter -Filter "NetConnectionID='Swizzlan'" | Select-Object - Property DeviceID
$NICs = gwmi Win32_NetworkAdapterConfiguration -computername . | where{$_.Index -eq $deviceID.DeviceID}
$deviceID.DeviceID is used because the first line returns a PSobject. Another way to do this would be as follows:
$Adapter = gwmi Win32_NetworkAdapter -Filter "NetConnectionID='Swizzlan'"
$NICs = gwmi Win32_NetworkAdapterConfiguration -computername . | where{$_.Index -eq $Adapter.DeviceID}

powershell how to remove `{}#` from output. Is there a special command to do it?

I entered gwmi win32_product | select -property name | select -first 1 and output to a file. My result was #{name=Google Talk Plugin}.
How can I get rid of #{}, and name. I only want it to show Google Talk Plugin?
#{} means your exporting an object with properties. Try the -ExpandProperty parameter in Select-Object. You could also combine both select-object commands, like:
gwmi win32_product | select -expandproperty name -first 1
I ran into a problem similar with
$drive = Get-WmiObject Win32_LogicalDisk -ComputerName $servername | Select-Object DeviceID
$drive comes up as #{DeviceID=C:}, #{DeviceID=D:}, ...
Here is my brute force hack at it.
The second Trim statement was because for some reason if I put it in the first Trim it starts to Trim the letters in the Drive =D: becomes :
enter code here
$Asdrive = #() #declared before to get rid of null pointer issue, also to tell PS this is an array not a string
#Write-Host "Trimming for Get-WmiObject"
for($i=0;$i -lt $drive.length; $i++) {
[string]$sdrive = $drive[$i]
[string]$sdrive1 = $sdrive.Trim("#","{","}","D","e","v","i","c","e","I","D")
[string]$sdrive2 = $sdrive1.Trim("=")
$Asdrive += $sdrive2
}
If you're running at least Version 3, you can also use the member enumeration feature and then array slicing to take the first one, instead of using select:
(gwmi win32_product).name[0]
I add some code as I found this question with google.
Frode F. solution is the best one.
If you write out something like:
Get-ADComputer -Filter * -SearchBase $OU | Select-Object Name
you get a proper List of all Computers in an OU. You can also pipe that to a CVS/HTML file and its still nice.
| Export-CSV "mylist.csv"
But if you store it into a variable (array) every name will be wrapped in #{}.
In my case I needed computer names in a variable. Here is the solution thanks to Frodo:
$computerList = Get-ADComputer -Filter * -SearchBase $OU | Select-Object -ExpandProperty Name
Hope it helps someone.
(would add it as comment under the right solution, but I don't have enough reputation to do so)