Powershell Win32Reg_AddRemovePrograms getting date output - date

How can I get the InstallDate from Win32Reg_AddRemovePrograms to display by day/month/year?
This is the command I need to use
get-wmiobject -Class Win32Reg_AddRemovePrograms -ComputerName
AComputer| where {$_.DisplayName -notlike "hotfix" -and
$_.DisplayName -notlike "Security Update" -and $_.DisplayName
-notlike "*Update for Windows *"} | select DisplayName,Version,Publisher,InstallDate

If the InstallDate looks like the InstallDate of the Win32_Product class (e.g 20131209):
[DateTime]::ParseExact('20131209','yyyyMMdd',$null).ToString('dd/MM/yyyy')

just to let you know the Win32Reg_AddRemovePrograms class is not a common class, it is added by the SMS/SCCM. It also only shows info related to 32-bit programs. Source For that reason I do not have the class available on the station and cannot provide you with the exact steps . Please provide output of this commands:
$item = $gwmi Win32Reg_AddRemovePrograms | select -first 1
$item.InstallDate
$item.InstallDate.GetType().fullname

One way is to format the date like the example below.
$date=Get-Date
Monday, December 9, 2013 5:29:50 AM
$date.ToString("dd/MM/yyyy")
09/12/2013
EDIT:
I don't have access to Win32Reg_AddRemovePrograms, so not tested.
try adding this to the end:
| %{ $_.InstallDate = ($_.InstallDate.tostring("dd/MM/yyyy") ); $_ }
So the final: (added ` for line breaks)
get-wmiobject -Class Win32Reg_AddRemovePrograms -ComputerName AComputer| `
where {$_.DisplayName -notlike "hotfix" -and $_.DisplayName -notlike "Security Update" -and $_.DisplayName -notlike "*Update for Windows *"} | `
select DisplayName,Version,Publisher,InstallDate | `
%{ $_.InstallDate = ($_.InstallDate.tostring("dd/MM/yyyy") ); $_ }
Or if it is in the date format like Shay mentioned,
| %{ $_.InstallDate = ([DateTime]::ParseExact($_.InstallDate,'yyyyMMdd',$null).ToString('dd/MM/yyyy')) ;$_}
Full script:
get-wmiobject -Class Win32Reg_AddRemovePrograms -ComputerName AComputer| `
where {$_.DisplayName -notlike "hotfix" -and $_.DisplayName -notlike "Security Update" -and $_.DisplayName -notlike "*Update for Windows *"} | `
select DisplayName,Version,Publisher,InstallDate | `
%{ $_.InstallDate = ([DateTime]::ParseExact($_.InstallDate,'yyyyMMdd',$null).ToString('dd/MM/yyyy')) ;$_}

Related

Powershell to display all automatic services that are stopped and attempt to start those services

I want to create a PS script where it will display all the automatic services that are stopped, and will attempt to start the services afterward.
Below is the PS code. It is successfully displayed all the stopped services on remote server.
Get-WmiObject Win32_Service -ComputerName SERVER1, SERVER2 |`
where {($_.startmode -like "*auto*") -and `
($_.state -notlike "*running*") -and `
($_.name -notlike "gupdate") -and `
($_.name -notlike "remoteregistry") -and `
($_.name -notlike "sppsvc") -and `
($_.name -notlike "lltdsvc") -and `
($_.name -notlike "KDService") -and `
($_.name -notlike "wuauserv")
}|`
select DisplayName,Name,StartMode,State,PSComputerName|ft -AutoSize
You can simply store the results of your query into a variable instead of selecting and displaying it, and you'd have a bunch of win32_Service instances that all have a StartService() method. It's generally a good idea to check the docs of the wmi classes you're using, most of them have methods that act on the object being represented, instead of having to pipe them around to other cmdlets like most Powershell objects:
Win32_Service class methods
You'd use it like this:
$services = Get-WmiObject Win32_Service -ComputerName SERVER1, SERVER2 |`
where {($_.startmode -like "*auto*") -and `
# [...]
}
$service | select DisplayName,Name,StartMode,State,PSComputerName|ft -AutoSize
$Service | ForEach {$_.StartService()}
Consider also using Get-Service unless you have a requirement for using WMI. You have a couple differences but the idea is the same:
$Services = Get-Service -ComputerName SERVER1,SERVER2 |`
where {($_.StartType -eq "Automatic") -and `
($_.Status -notlike "*running*") -and `
($_.Name -notlike "gupdate") -and `
# ...
}
$Services | select Description,Name,StartType,Status,MachineName |ft -AutoSize
$Services | Start-Service
Also, you could also simplify your filter a lot by using the -notin operator:
where {($_.startmode -like "*auto*") -and `
($_.state -notlike "*running*") -and `
($_.name -notin "gupdate","remoteregistry","sppsvc","lltdsvc","KDService","wuauserv")
}

How do I get the server name in Format-Table?

This is probably a version issue, but I simply need to get the server name into the Format-Table PowerShell command.
$compArray = Get-Content C:\Users\Me\Documents\ServerList_All.txt
$Proc = foreach ($strComputer in $compArray) {
Get-WMIObject Win32_Service | Where-Object {
$_.Name -like 'SQL*' -or
$_.Name -like 'MSSQL*' -or
$_.Name -like 'OLAP*' -or
$_.Name -like 'MSDTS*' -or
$_.Name -like 'MSOLAP*' -or
$_.Name -like 'ReportServer*'
} | Sort-Object -Property Name | Format-Table $strComputer, Name, State
}
$Proc | Out-File C:\Users\ME\Documents\ServerStatus_All.txt
This works in PS v2:
| Sort-Object -Property Name | Format-Table Name, State
This does not, but does work in PS v3:
| Sort-Object -Property Name | Format-Table $strComputer, Name, State
Error:
Format-Table : Cannot convert System.Management.Automation.PSObject to one of the following types {System.String, System.Management.Automation.ScriptBlock}.
The only difference is the $strComputer variable. I am reading from a text file, and everything is beautiful in v3+.
No, I cannot upgrade to a newer PS version on the server I am running this from, sadly.
This should work for you in both...
$compArray = (Get-ADComputer -Filter *).Name
$Proc = foreach ($strComputer in $compArray) {
Get-WMIObject -Class Win32_Service -ComputerName $strComputer |
Where-Object {
$_.Name -like 'SQL*' -or
$_.Name -like 'MSSQL*' -or
$_.Name -like 'OLAP*' -or
$_.Name -like 'MSDTS*' -or
$_.Name -like 'MSOLAP*' -or
$_.Name -like 'ReportServer*'
} |
Select-Object -Property #{Name = 'Computer';Expression={$strComputer}}, Name, State |
Sort-Object -Property Name |
Format-Table -AutoSize
}
$Proc
# Results
Computer Name State
-------- ---- -----
LABSQL01 MSSQLFDLauncher Running
LABSQL01 MSSQLSERVER Running
LABSQL01 SQLBrowser Stopped
LABSQL01 SQLSERVERAGENT Running
LABSQL01 SQLTELEMETRY Running
LABSQL01 SQLWriter Running
Computer Name State
-------- ---- -----
LABWSM01 MSSQL$MICROSOFT##WID Stopped

Powershell Remove Printer

Get-WmiObject -Class Win32_Printer | where{$_.Network -eq ‘true‘}| foreach{$_.delete()}
I know this script will delete all network printers, but I need to delete only certain network printers…like CLEPRINT15-2 and CLEPRINT 15-4, but not 15-3. How would I do this?
You already have a where filter on the Network property just more conditionals on the Name property.
Get-WmiObject -Class Win32_Printer |
Where-Object {$_.Network -eq $true -and ($_.Name -eq 'CLEPRINT15-2' -or $_.Name -eq 'CLEPRINT15-4')} |
ForEach-Object {$_.Delete()}
Note: Also be careful with smart quotes. ‘ is different than '
Try this additional where condition with a RegEx class [24]:
Get-WmiObject -Class Win32_Printer |
where{$_.Network -eq $true -and $_.Name -match '^CLEPRINT-?15-[24]$'} |
foreach{$_.delete()}

Powershell looping through message queues

I'm trying to return tables with message queues from three different environments. I could copy and paste the existing code for all three, but I want to make it cleaner and more reusable.
Is there a way to loop through each message queue and return them in separate tables (i.e.: Dev, Dev2, Dev3 queues)?
[object]$dev3Queues = gwmi -class Win32_PerfFormattedData_msmq_MSMQQueue -computerName myServer | Where{$_.Name -like "*dev3*" } | select Name,MessagesInQueue #| Out-File "C:\test.txt"
[object]$dev2Queues = gwmi -class Win32_PerfFormattedData_msmq_MSMQQueue -computerName myServer | Where{$_.Name -like "*dev2*" } | select Name,MessagesInQueue #| Out-File "C:\test2.txt"
[object]$devQueues = gwmi -class Win32_PerfFormattedData_msmq_MSMQQueue -computerName myServer |
Where{$_.Name -notlike "*dev2*" -AND $_.Name -notlike "*dev3*" -AND $_.Name -notlike "*private*" -AND $_.Name -notlike "*Computer Queues*" -AND $_.Name -notlike "*uat*"} | select Name,MessagesInQueue #| Out-File "C:\test3.txt"
$Html = "<html><head>Whoo Queues</head><body><table border=1>"
foreach($element in $devQueues)
{
$Html += "<tr><td>" + $element.Name + "</td><td>"+ $element.MessagesInQueue + "</td> </tr>"
}
$Html += "</table></body></html>"
$Html | out-file C:\temp\DEVQueues.html
#environmentloop - dev,dev2,dev3
#{
#queue loop + html
#}
You can use the ConvertTo-Html cmdlet with the option -Fragment to convert a list of objects to an HTML table of the object properties.
Get-WmiObject -Class Win32_PerfFormattedData_msmq_MSMQQueue |
select Name, MessagesInQueue |
ConvertTo-Html -Fragment
Also, when running Get-WmiObject against a remote server using a WMI filter provides better performance than retrieving all results and filtering them on the local host with Where-Object.
$computer = 'myServer'
$filter = 'Name LIKE "%dev3%"'
Get-WmiObject -Class Win32_PerfFormattedData_msmq_MSMQQueue -Computer $computer `
-Filter $filter
However, since you want to filter the same dataset for various criteria, in your case the best approach might be to first fetch all relevant data from the remote host with a more general WMI filter (to avoid multiple remote connections), and then process them locally with several Where-Object filters:
$server = 'myServer'
$wmiFilter = 'NOT (Name LIKE "%private%" OR Name LIKE "%Computer Queues%" ' +
'OR Name LIKE "%uat%")'
$psFilters = { $_.Name -like "*dev3*" },
{ $_.Name -like "*dev2*" },
{ $_.Name -notlike "*dev2*" -and $_.Name -notlike "*dev3*" }
$data = Get-WmiObject -Class Win32_PerfFormattedData_msmq_MSMQQueue `
-Computer $server -Filter $wmiFilter
'<html><head>Whoo Queues</head><body>'
foreach ($filter in $psFilters) {
$data | ? $filter | select Name, MessagesInQueue | ConvertTo-Html -Fragment
}
'</body></html>'

Getting properties of all Stopped Automatic services on Windows Server w/ PowerShell

I want to get the Display Name, Name, Start Mode, Start Name, and State of all Automatic services that are in a Stopped state on a Windows server. Normally, I would just do
get-wmiobject -class win32_service | ? {$_.StartMode -eq "Auto" -and $_.State -eq "Stopped"} | select DisplayName, Name, StartMode, StartName, State
However, the cmdlet above does not distinguish between a state of "Automatic" and "Automatic Delayed Start". The cmdlet I have below will not report any services that have a state of auto delayed start, but I don't know how to get it to also display the other properties I need.
(Get-WmiObject -Class Win32_Service -Filter "state = 'stopped' and startmode = 'auto'" | Select-Object -ExpandProperty name) | Where-Object {(Get-ChildItem HKLM:\SYSTEM\CurrentControlSet\Services | Where-Object {$_.property -contains "DelayedAutoStart"} | Select-Object -ExpandProperty PSChildName) -notcontains $_} | Select-Object #{l='Service Name';e={$_}}
How can I modify the above cmdlet so that it will also display the other properties I want?
EDIT:
I know the method below will work, but its inefficient and non-powershell-like.
$auto_services = #((get-wmiobject -class win32_service -filter "state='stopped' and startmode='auto'" | select-object -expandproperty name) | ? {(get-childitem HKLM:\SYSTEM\CurrentControlSet\Services | ? {$_.property -contains "DelayedAutoStart"} | Select-Object -ExpandProperty PSChildName) -notcontains $_})
foreach ($service in $auto_services) { Get-WMIobject -class win32_service | ? {$_.Name -eq $service} | Select DisplayName, name, startmode, startname, state}
EDIT 2:
What would be even better is if you could list all services and the desired properties and somehow make it so that the "Automatic Delayed Start" services actually show "Auto Delayed Start" as the StartMode instead of showing just "Auto".
Using PowerShell 4.0 (with -PipelineVariable) you can do the following :
get-wmiobject -class win32_service -PipelineVariable s | ? {$_.StartMode -eq "Auto" -and $_.State -eq "Stopped"}| where {(Get-ItemProperty "HKLM:\SYSTEM\CurrentControlSet\services\$($_.name)" -Name 'DelayedAutoStart' -ErrorAction "silentlycontinue").DelayedAutoStart -eq 1} | % {select -InputObject $s -Property DisplayName, Name, StartMode, StartName, State}
Using previous versions you should assign the service object to a var during the pipeline.
get-wmiobject -class win32_service | % {$s=$_;$s} | ? {$_.StartMode -eq "Auto" -and $_.State -eq "Stopped"}| where {(Get-ItemProperty "HKLM:\SYSTEM\CurrentControlSet\services\$($_.name)" -Name 'DelayedAutoStart' -ErrorAction "silentlycontinue").DelayedAutoStart -eq 1} | % {select -InputObject $s -Property DisplayName, Name, StartMode, StartName, State}
Edited
Here is a version for PowerShell 2.0 of all stopped services that are in "Automatic" startmode and not not "Automatic Delayed Start" :
get-wmiobject -class win32_service | % {$s=$_;$s} | ? {$_.StartMode -eq "Auto" -and $_.State -eq "Stopped"}| % {$d = (Get-ItemProperty "HKLM:\SYSTEM\CurrentControlSet\services\$($_.name)" -Name 'DelayedAutoStart' -ErrorAction "silentlycontinue").DelayedAutoStart;$_ } | where {$d -ne '1'} |% {select -InputObject $s -Property #{name="DelayedAutoStart";expression={$d}},DisplayName, Name, StartMode, StartName, State}