List ComputerName with the powershell result - sql-server

I have a script which gives me the patches & service packs applied on SQL Server by querying the registry. However It does not give me the ComputerName as one of the fields. How can I append the ComputerName to each row of the output?
Get-ChildItem -Path HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall | Get-ItemProperty | Sort-Object -Property DisplayName | Select-Object -Property KBNumber, ReleaseType, ParentKeyName, DisplayName, DisplayVersion, InstallDate | Where-Object {($_.DisplayName -like "Hotfix*SQL*") -or ($_.DisplayName -like "Service Pack*SQL*")}| Format-table -AutoSize

Related

Get-ADUser - searching for expired account. Using variables in command

I am currently working on a Powershell GUI script to help my team easier find accounts with expired passwords, disabled accounts etc and to output these to a CSV. It revolves almost entirely around the "Get-ADUser" command. So far almost everything has worked, bar finding accounts with expired passwords.
I've researched this a lot already but there seems to be no easy way of finding expired accounts using Get-ADUser. I know I can use Search-ADAccount instead but it would be very awkward to do so (as I would need to re-write a lot of code).
Get-Aduser -Properties * -Filter {PasswordExpired -eq $true} just draws a blank.
I've found a partial solution over at https://serverfault.com/questions/805526/get-aduser-password-expired-filter-not-working-correctly/805611
For example,
Get-ADUser -Properties * -Filter * | ? {$_.PasswordExpired -eq $True -and $_.Enabled -eq $true} | Select-Object name, enabled | Export-Csv "C:\report.csv" -NoTypeInformation
works just fine but if I try to assign the 'middle' of the command i.e
{$_.PasswordExpired -eq $True -and $_.Enabled -eq $true}
to a variable and then substitute it into the command I either get an error, a list of all those in my AD or nobody at all. The rational for substituting in a variable is to allow for the possible account statuses (that the user can choose from by selecting a radio button).
I've tried the various permutations of double and single quotes, including and not including curly brackets etc but Powershell will not give me a break!
Thanks!
The Get-ADUser cmdlet exposes the PasswordExpired extended property, which is a boolean indicating if the password is expired. It is based on the msDS-User-Account-Control-Computed attribute. However, you cannot filter with this property.
This would mean you can check the UF_PASSWORD_EXPIRED bit on that property:
Get-ADUser -Filter "Enabled -eq 'True'" -Properties 'msDS-User-Account-Control-Computed' |
Where-Object {($_.'msDS-User-Account-Control-Computed' -band 0x800000) -eq 0x800000} | # UF_PASSWORD_EXPIRED --> 0x800000 = 8388608
Select-Object Name, Enabled | Export-Csv "C:\report.csv" -NoTypeInformation
You can speed up the above by extending the filter to rule out users with PasswordNeverExpires and PasswordNotRequired both $false:
$filter = "Enabled -eq 'True' -and PasswordNeverExpires -eq 'False' -and PasswordNotRequired -eq 'False'"
Get-ADUser -Filter $filter -Properties PasswordNeverExpires, PasswordNotRequired, 'msDS-User-Account-Control-Computed' |
Where-Object {($_.'msDS-User-Account-Control-Computed' -band 0x800000) -eq 0x800000} | # UF_PASSWORD_EXPIRED --> 0x800000 = 8388608
Select-Object Name, Enabled | Export-Csv "C:\report.csv" -NoTypeInformation
I reckon I've found a solution over on Stack Exchange.
See https://serverfault.com/questions/723217/find-out-if-password-expired-or-when-it-expires-for-user-in-a-specific-ou
Early tests suggest it works.

Is there a way to sort within DistinguishedName

I am trying to get a list of users and Id like to sort based on the last OU= in DistinguishedName. The syntax I'm using isn't quite right and I need some pointers. Thank You
get-aduser -Filter {Enabled -eq $true} -Properties * | where {($_.EmployeeNumber -eq $null) -and ($_.PrimaryGroup -eq 'CN=Domain Users,CN=Users,DC=OURDOMAIN,DC=net')} | FT SamAccountName,Name,EmployeeNumber,DistinguishedName,Created | export-csv Users.csv
You can ask for the msDS-parentdistname attribute in AD. It's a relatively new attribute, so it's possible it's not available on the version of Windows Server that you're running in your environment. But you can see if it's there.
It's a constructed attribute, which means it's calculated at the time it's asked for. But that also means you have to explicitly ask for it (you can't use -Properties *).
The use Sort-Object to sort your list.
get-aduser -Filter {Enabled -eq $true} -Properties SamAccountName,Name,EmployeeNumber,DistinguishedName,Created,msDS-parentdistname | where {($_.EmployeeNumber -eq $null) -and ($_.PrimaryGroup -eq 'CN=Domain Users,CN=Users,DC=OURDOMAIN,DC=net')} | Sort-Object msDS-parentdistname | FT SamAccountName,Name,EmployeeNumber,DistinguishedName,Created,msDS-parentdistname | export-csv Users.csv

PowerShell reading a filtered list of SQL Server tables

I’m reading a list of SQL Server tables into a PowerShell array, which I’ll then loop through. The tables I’m interested in all have the name like %PickList%. How can I filter for that string?
Here’s what I have. Filtering for an exact match of a table name works ok, but how can I expand the search to get all such tables?
clear-host
Set-location SQLserver:\sql\...-CONRAD...\default\databases\PT2\tables
$a = Get-ChildItem | Where {$_.Name -eq "santitzie_PickList_Ctry_FirstName"} # ok
# Get-ChildItem | Where {$_.Schema -eq "pt2"} # ok
# Get-ChildItem | Where {$_.Name -eq "santitzie_PickList_Ctry_FirstName"} # ok
# Get-ChildItem | Where {$_.Name -like "%PickList%"} # no
# Get-ChildItem | Where {$_.Name -eq "%Pick%"} # no
foreach ($item in $a) {Write-Host $item} # ok
-like *PickList* would work, or you could fall back to regular .NET string comparison methods and do $name.Contains('PickList').
Contains is case-sensitive whereas -like is not.
For future reference, topics like this are easily Googleable.

Filtering packages by their name from list out of file

I am trying to read program names from a file to filter out the installed ones.
For reading the file I got:
$file = Get-Content "C:\Users\testuser\Desktop\Test.txt"
Then I try to filter packages by their name which isnt working, what I tried:
Get-AppxPackage | Where-Object -Property Name -notin $file | select Name, IsFramework
or
Get-AppxPackage | Where-Object { $_.Name -notin $file } | select Name, IsFramework
I cannot use any .Net statements, how can I solve this?
Edit: Here is my file's content:
Microsoft.BioEnrollment Microsoft.AAD.BrokerPlugin Microsoft.Windows.CloudExperienceHost Microsoft.Windows.ShellExperienceHost windows.immersivecontrolpanel Microsoft.Windows.Cortana Microsoft.AccountsControl Microsoft.LockApp
Just to recap from David Brabant, the following worked for askingQuestions:
Get-AppxPackage | ?{ $file.Contains($_.Name) } | select Name, IsFramework
This works great and is probably one of the best ways to do this.
This didn't work for me for all applications, so here's another take at filtering the list. :)
Matching Name:
Get-WmiObject -Class Win32_Product | Where-Object {$_.name -Like "appName"}
Not Matching Name:
Get-WmiObject -Class Win32_Product | Where-Object {$_.name -NotMatch "appName"}
Note that using Where-Object is not case sensitive but looks for exact match. On the flip side, you can also use wildcards.
but to filter by file, you could do something like:
[string[]]$List = (Get-Content -Path 'C:\test.txt') -replace ' ',"`r`n"
foreach ($ProgramName in $List)
{
Get-WmiObject -Class Win32_Product | Where-Object {$_.name -Like $ProgramName}
}
Note that -replace ' ',"rn" is only necessary for spaces between application names. You can just remove this and have each application on a new line. Also note that this could possibly produce duplicates.
Wildcard Example:
Get-WmiObject -Class Win32_Product | Where-Object {$_.name -Like "*adobe*"}
Cheers!

Powershell: Take items from an array and assign variables to them

I'm trying to extract the GUIDS from this list.
Get-WMIObject WIN32_Product | ? {$_.IdentifyingNumber -like "*26A24AE4-039D-4CA4-87B4-2F8321*FF*"} | Format-List IdentifyingNumber
is it possible to convert each item into a string and possibly assign them to variables?
Here's what I was trying, but it's not working. but maybe this will show the logic i am trying:
$A = Get-WMIObject WIN32_Product | ? {$_.IdentifyingNumber -like "*26A24AE4-039D-4CA4-87B4-2F8321*FF*"} | Format-List IdentifyingNumber
$GUIDList = ForEach-Object{$A.ToString()}
$GUIDList
Format-list is, as the name suggests, for display formatting purposes.
Based on your usage, I suppose you can use Select-Object instead:
| Select-Object IdentifyingNumber
After that you will have an array of objects with property IdentifyingNumbers.
If you just want array /list of strings ( assuming the property is string ) you could do:
| Select-Object -expand IdentifyingNumber
Here's another useful option that uses a WQL filter and uses the Foreach-Object cmdlet to extract the identifiers:
Get-WmiObject Win32_Product -Filter "IdentifyingNumber LIKE "%26A24AE4-039D-4CA4-87B4-2F8321%FF%"} |
Foreach-Object {$_.IdentifyingNumber}

Resources