Powershell script - How can I get this script to only output values that match a certain string - azure

So basically I am current using this script that checks what license is assigned to each account in Azure AD
Connect-MsolService
$Users= Import-CSV C:\Users\Ark\Desktop\powershell\test01.csv
$Users|%{Get-MsolUser -UserPrincipalName $_.UPN|select userPrincipalNAme,#{n="Licenses Type";e={$_.Licenses.AccountSKUid}}}
The csv file that I am ingesting looks like this
|UserPrincipalName|
|:----------------------|
|test.user#test.com |
|test.user2#test.com |
|test.user1#test.com |
With this scripts it goes through and outputs the correct license info for each account, like so
|UserPrincipalName| Licenses Type
|:----------------------|:-------------
|test.user#test.com |testdomain:SPE_E3
|test.user2#test.com |testdomain:SPE_F1
|test.user1#test.com |testdomain:SPE_E3
Where I am stuck at is I would like for this to only output users that only have a specific type of license. For example if I would only want users that have a testdomain:SPE_E3 license assigned. What can I do to edit my script that would only output users for that specific license, like so
|UserPrincipalName| Licenses Type
|:----------------------|:-------------
|test.user#test.com |testdomain:SPE_E3
|test.user1#test.com |testdomain:SPE_E3

Try with the "Where-Object" filter as follows:
Where-Object {($_.licenses).AccountSkuId -match "SPE_E3"}

Related

PowerShell: update O365 AD bulk attributes through csv file

We are trying to bulk update our Azure Active Directory. We have a excel csv list of UserPrincipalNames that we will update the Title, Department, and Office attributes
# Get List of Clinical CMs
$PATH = "C:\Users\cs\Documents\IT Stuff\Project\Azure AD Update\AD-Update-ClinicalCMs-Test.csv"
$CMs = Import-csv $PATH
# Pass CMs into Function
ForEach ($UPN in $CMs) {
# Do AD Update Task Here
Set-Msoluser -UserPrincipalName $UPN -Title "Case Manager" -Department "Clinical" -Office "Virtual"
}
The CSV:
User.1#domain.com
User.2#domain.com
User.3#domain.com
The Set-MsolUser command will work on its own, but it is not working as intended in this For loop. Any help or insight is greatly appreciated
As Jim Xu commented, here my comment as answer.
The input file you show us is not a CSV file, instead, it is a list of UPN values all on a separate line.
To read these values as string array, the easiest thing to is to use Get-Content:
$PATH = "C:\Users\cs\Documents\IT Stuff\Project\Azure AD Update\AD-Update-ClinicalCMs-Test.csv"
$CMs = Get-Content -Path $PATH
Of course, although massive overkill, it can be done using the Import-Csv cmdlet:
$CMs = (Import-Csv -Path $PATH -Header upn).upn

Powershell - Checking if an Azure AD guest already exists

We have a Powershell script that invites guests in bulk to Azure AD. The email addresses are contained in a CSV file.
It is all working well, but I would like to add functionality to check if the user already exists in the tenant, so that they don't receive a second invitation (it seems you can continue to invite already existing or already invited guests).
For a normal user like bob#company.com, with
$email = "bob#company.com"
I could simply use this:
Get-AzureADUser -ObjectId "$email"
Of course guests are formatted like john_gmail.com#EXT#tenant.onmicrosoft.com. Using that as the ObjectID does indeed work correctly, but of course in the CSV we would see john#gmail.com and so it fails to look up the user.
Is there some sensible way to convert john#email.com to john_gmail.com#EXT#tenant.onmicrosoft.com whilst doing this lookup? It needs to be seen as a normal email address later in the script when it actually invites them.
Thanks
Given that $email = "john#email.com" you can search for "john_gmail.com#EXT#tenant.onmicrosoft.com" like this:
Get-AzureADUser -ObjectId "$($email -replace "#", "_")#EXT##tenant.onmicrosoft.com"
or
Get-AzureADUser -ObjectId ($email.Replace("#","_") + "#EXT##tenant.onmicrosoft.com")
This doesn't change the value of $email while you can still search for the expanded term.
I've realised I can use
$email -replace "#", "_"
To get the output I want. I'll duplicate the variable to avoid causing issues or having to revert it later in the script.

Trying to Export a CSV list of users using Active Directory Module for Windows Powershell

So the below is where I'm at so far:
import-module activedirectory
$domain = "ourdomain"
Get-ADUser -Filter {enabled -eq $true} -Properties whenCreated,EmailAddress,CanonicalName |
select-object Name,EmailAddress,CanonicalName,whenCreated | export-csv C:\Data\test.csv
Unfortunately, when I run the above I get dates in two different formats in the CSV, e.g.:
01/01/2017
1/01/2017 8:35:56 PM
The issue this poses is that there isn't really a clean way to sort them. Excel's formatting doesn't change either of these formats to be more like the other, both because of the inclusion of time in one and not the other, and because the time-inclusive format doesn't use trailing zeroes in the single digit numbers, but the time-exclusive format does.
We have an existing script that captures users using the LastLogonTimestamp attribute that does this correctly by changing the bottom line to the following:
select-object Name,EmailAddress,CanonicalName,#{Name="Timestamp"; Expression={[DateTime]::FromFileTime($_.whenCreated).ToString('yyyy-MM-dd_hh:mm:ss')}}
For some reason this expression runs properly when we query the LastLogonTimestamp attribute, but when we run this version querying the whenCreated attribute, we get an entirely blank column underneath the Timestamp header.
I'm not particularly knowledgeable about PowerShell itself, and my colleague who had found the original script for the LastLogonTimestamp just found it online and adapted it as minimally as possible to have it work for us, so I don't know if something in this line would work properly with one of these attributes and not the other. It seems strange to me though that two attributes using dates in the same program would store them in different formats though, so I'm not convinced that's it.
In any case, any help anyone can offer to help us get a uniform date format in the output of this script would be greatly appreciated - it needn't have the time included if it's easier to do away with it, though if they're equally easy we may as well keep it.
whencreated is already a [DateTime]. Notice the difference between the properties when you run something like this:
Get-ADUser TestUser -Properties lastlogon,whenCreated | select lastlogon,whenCreated | fl
(Get-ADUser TestUser -Properties lastlogon).lastlogon | gm
(Get-ADUser TestUser -Properties whenCreated).whenCreated | gm
This means that you don't have to convert to a DateTime before running the toString() method.
select-object #{Name="Timestamp"; Expression={$_.whenCreated.ToString('yyyy-MM-dd_hh:mm:ss')}}

How to get name of a USB device that is already mounted?

I'm actually writing a node script that detect if a specific USB is plugged, then it copy it content to Desktop. This is for Windows principally. To do this I manually check if 'E:\' path exists , 'F:\' , etc...
But I need to be sure that devices are the ones that I need. They got specific names, for example:
MTR12345 or MTR23RR5 or MTRTGX23.
And I need to know theses names. I searched for differents nodejs and powershell solutions but no ones fit my needs.
I need to get the name of the device that is mounted at E:\ . I'm totally new to PowerShell and NodeJS as well.
How can I do this ? Thanks for your help.
Sounds like you are just looking for the volume names. The WMI class Win32_logicaldisk would return that for mounted devices. Assuming it was populated of course. In it's simplest form:
Get-WmiObject Win32_logicaldisk | Where-Object{$_.VolumeName -eq "MyUSBKey"}
You have some specific examples and a regex query that you are trying to use to narrow down the results. So if you want to match a regex query:
Get-WmiObject Win32_logicaldisk |
Where-Object{Where-Object{$_.VolumeName -match "MTR[A-Za-z0-9]+"}} |
Select -Expand DeviceID
You could even simplify that if you wanted. Just match volumes that start with "MTR". Not as perfect as the other one but just as simple.
Get-WmiObject Win32_logicaldisk |
Where-Object{Where-Object{$_.VolumeName -match "^MTR"}} |
Select -Expand DeviceID

Powershell how to take string from file and put into a variable

I am writing a Powershell script to "build" Windows 7 PCs: adding users, printers, applications, changing settings, et al. I am adding some printer drivers using PNPUtil, but the problem is I won't know what "Published name" the drivers will be given.
If I put the output from the PNPUtil command into a .txt file, is there a way for me to then take the __.inf Published name and put it into a variable so that I can then use that name to add the printer using $printerclass.CreateInstance()?
You don't have to use a file if PNPUtil only outputs the name your interested in. That is, you can assign its output to a variable like so:
$result = pnputil.exe
BTW if you want to use a file, to read content from a file you use Get-Content:
pnputil.exe > result.txt
$result = Get-Content result.txt
$line = $result | Foreach {if ($_ -match 'assigned an (\w+\.inf)') {$matches[1]}}
Okay - I found my own solution: Once the .inf file is added, all the driver names in that .inf are stored in the Microsoft update files. I just need to know the specific name of the driver I need from each .inf file in order to add the Printers.
However, I'd still love to know how to get a string from a line from a file using Powershell.

Resources