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

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}

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.

Export Multi column Excel spreadsheet from Powershell script

I have been working at this for what seems like way too long now. I am pulling all of the ADgroups names and descriptions in my domain. At this point it doesn't necessarily matter but its eating at me.
$exportList = #()
$ADgroups = Get-ADGroup -Filter * -Properties Description
Foreach ($group in $adgroups){
$GroupObj = New-Object System.Object
$GroupObj | Add-Member -Type NoteProperty -Name Name -Value $group.Name
$GroupObj | Add-Member -Type NoteProperty -Name Description -Value $group.Description
$exportList += $GroupObj
}
$exportList | Out-File C:\test\ADGroups.csv
And the file comes out like this where all of the data is within column A
but I would like for the name to show in column A and the Description to show in column B I have tried making different Arrays and tried calling the properties in different ways but nothing I have tried yet has worked and I am sure that I am missing something very simple.
You're using Out-File Instead of Export-CSV which export a text file instead of a comma separated file
Replace the:
$exportList | Out-File C:\test\ADGroups.csv
To:
$exportList | Export-CSV C:\test\ADGroups.csv
Also, You can cut some lines and simply do:
Get-ADGroup -Filter * -Properties Description |
Select Name,Description | Export-CSV C:\test\ADGroups.csv

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

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!

How to search for a string in all stored procedures through SQL Powershell?

I have a bunch of stored procedures in my database, I launched the PowerShell for SQL Server by right clicking on the Stored Procedures folder in SSMS and choosing Start Powershell, I tried the following command but it gives me nothing
PS SQLSERVER:\SQL\MYCOMP\DEFAULT\Databases\MYDB\StoredProcedures> Get-ChildItem | ForEach-Object { (Invoke-SQLCMD -SuppressProviderContextWarning -Query ("sp_helptext '$_'" )) | Select-String mykeyword}
If I remove the | Select-String mykeyword part, it spits out the code for every stored procedure one by one. What am I missing?
Your command is returning an array of PSCustomObjects that have a string property named "Text". You need to pipe the value of that property to select-string. Right now you are calling select-string on an object that is not a string. Change to:
Get-ChildItem | ForEach-Object { (Invoke-SQLCMD -SuppressProviderContextWarning -Query ("sp_helptext '$_'" )) } | Select-Object Text | Select-String yourKeyword
To answer your later question about outputting the sp name, you could store the name in a variable and output it, something like:
$sName = '';
Get-ChildItem | ForEach-Object { $sName = $_.Name; (Invoke-SQLCMD -SuppressProviderContextWarning -Query ("sp_helptext '$_'" )) } | Select-Object Text | Select-String yourKeyword | ForEach-Object { Write-Host $sName }
I see that there's already an answer, but this was slightly more useful to me:
Get-ChildItem | ForEach-Object { if(($line=Invoke-SQLCMD -SuppressProviderContextWarning -Query ("sp_helptext '$_'" ) | Where Text -like '*Title*'| select -expand Text)) {$_;$line}}
I'm outputting the stored procedure objects as well as the matching lines (interspersed). It would probably be worthwhile to make this into a function and package the output as objects but this is a good start.

Resources