Powershell WMI get software older than 30 days - powershell

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

Related

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 - how to replace OS Version number with String

I am querying remote servers for their operating system. I know that I can return the Version, but I want to replace these values with the friendly name. The code I have so far is:
$Computer = (gc c:\servers.txt)
$BuildVersion = Get-WmiObject -Class Win32_OperatingSystem -Property Version, CSName -ComputerName $Computer -ErrorAction SilentlyContinue
$Build=$BuildVersion.version
If ({$BuildVersion.Version -match "5.2.3790"})
{$Build="2003"}
Elseif ({$BuildVersion.Version -match "6.1.7601"})
{$Build="2008"}
Elseif ({$BuildVersion.Version -like "6.3.9600"})
{$Build="2012"}
But this doesn't seem to work and only returns "2003" regardless. Please help, I'm fairly new to PS and coding.
thanks
The problem is your if statements. Putting the Boolean expression inside squiggly brackets makes it a script block, and that's going to get cast as a string before being cast as a Boolean. Strings cast to Booleans always evaluate to true unless they're empty.
PS C:\> {$BuildVersion.Version -match "5.2.3790"}
$BuildVersion.Version -match "5.2.3790"
PS C:\> ({$BuildVersion.Version -match "5.2.3790"}) -as [bool]
True
PS C:\> $BuildVersion.Version -match "5.2.3790"
False
PS C:\> ($BuildVersion.Version -match "5.2.3790") -as [bool]
False
So what you're running is essentially:
if ([bool]'$BuildVersion.Version -match "5.2.3790"') [...]
And that's always going to be true.
Try:
$Computer = (gc c:\servers.txt)
$BuildVersion = Get-WmiObject -Class Win32_OperatingSystem -Property Version, CSName -ComputerName $Computer -ErrorAction SilentlyContinue
$Build=$BuildVersion.version
If ($BuildVersion.Version -match "5.2.3790")
{
$Build = "2003"
}
Elseif ($BuildVersion.Version -match "6.1.7601")
{
$Build = "2008"
}
Elseif ($BuildVersion.Version -like "6.3.9600")
{
$Build = "2012"
}
Bottom line is that squiggly brackets are not parentheses and you can't use them like they are.
However, there's also a major logic error here. You're potentially fetching an array for $BuildVersion because you're reading from a file, but then you treat it like a single value. You never loop through $BuildVersion. However, I do not have enough information about what you're actually trying to do with your script (like what you do with $Build) to be able to fix that.
I originally said this, but I've since changed my mind
The reason this is only returning 2003 is that you're only running your If code on a single entry in the list.
Wrong
As TessellatingHeckler says, the reason your if wasn't working is that you had too many curly braces, so PowerShell wasn't actually evaluating your logic.
However, you still need to step through each of the computers to do what you're trying to do. We'll do that by adding in a ForEach loop. I also went ahead and replaced your If {} logic with a Switch statement, which I think is easier to understand for a scenario like this with multiple clauses. If's just get way too verbose.
Finally, I'm assuming you want to output the results too, so I added a custom object here, which is just a way of choosing which properties we want to display.
$Computer = (gc c:\servers.txt)
ForEach ($system in $computer){
$BuildVersion = Get-WmiObject -Class Win32_OperatingSystem -Property Version, CSName -ComputerName $system -ErrorAction SilentlyContinue
$Build=$BuildVersion.version
switch ($build){
"5.2.3790" {$Build="2003"}
"6.1.7601" {$Build="2008"}
"6.3.9600" {$Build="2012"}
}
#output results
[pscustomobject]#{Server=$system;OSVersion=$build;CSName=$buildVersion.CSname}
}#EndOfForEach
Output
>Server OSVersion CSName
------ --------- ------
dc2012 2012 DC2012
sccm1511 2012 SCCM1511
You can use this:
Get-WmiObject -Class Win32_OperatingSystem | Select-Object -ExpandProperty Caption
Additionally you can see everything this WMI object holds like this:
Get-WmiObject -Class Win32_OperatingSystem | fl *
Edit: if you want to remove some text from the string, you can use -replace:
(Get-WmiObject -Class Win32_OperatingSystem |
Select-Object -ExpandProperty Caption) -replace "Microsoft Windows Server ",""

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)

WMI optimization

I have the following script.
$script:WMIClassNames = `
"CIM_Processor",`
"CIM_PhysicalMemory",`
"Win32_ComputerSystem",`
"CIM_BIOSElement",`
"Win32_OperatingSystem",`
"Win32_NetworkAdapterConfiguration",`
"Win32_Volume",`
"Win32_QuickFixEngineering",`
"CIM_Process",`
"WIN32_Service",`
"WIN32_NTLogEvent"
$script:WMIClassInfo = Get-WmiObject -ComputerName $script:strSysName -Namespace "Root/CIMV2" -List | Select Name | Where-Object {$script:WMIClassNames -contains $_.Name}
When i measure the time of the script, it is taking almost 4 to 10 min to execute this script on remote machines. Can this script be optimized so that the execution time can be reduced?
Thanks for the help.