Im using the following powershell query to get a list of disks:
Get-NcDisk | Select-Object -Property model | Sort-Object -Property Model -Descending | foreach {$_.model}
It outputs like below:
X316_SMKRE06TA07
X316_HARIH06TA07
X316_HARIH06TA07
X316_HARIH06TA07
How can I get it to output a numbered count of each type of disk like below:
1 X316_SMKRE06TA07
3 X316_HARIH06TA07
Group-Object will do this for you..
I can't use Get-NcDisk but it may just be:
Get-NcDisk | Select-Object -ExpandProperty model | Group-Object
Example output using a string array:
"X316_SMKRE06TA07","X316_HARIH06TA07","X316_HARIH06TA07","X316_HARIH06TA07" | Group-Object
Count Name Group
----- ---- -----
1 X316_SMKRE06TA07 {X316_SMKRE06TA07}
3 X316_HARIH06TA07 {X316_HARIH06TA07, X316_HARIH06TA07, X316_HARIH06TA07}
Related
I have the following PowerShell code:
[xml]$xml = Get-Content uccxResourceList_forReference.xml
$xml.resources.resource |
Select-Object firstname, lastname, extension,
#{Name="Team"; Expression={($_.team.name)}} |
Sort Team |
Format-Table
which produces a table like this:
firstName lastName extension Team
--------- -------- --------- ----------------
Homer Simpson 1000 SafetyInspectors
Frank Grimes 1001 SafetyInspectors
Lenford Leonard 1002 SafetyInspectors
Carlton Carlson 1003 SafetyInspectors
Montgomery Burns 2000 Executives
Waylon Smithers 2001 Executives
What I would like to do is output each team into its own file. So not just a simple | Out-File teamlist.txt at the end, but I would like to output a text file containing all of the "SafetyInspectors" and another with all of the "Executives".
I know I could get this done with a subsequent foreach loop but I feel it could also be done in the pipeline and I just don't know how to do it.
I'd prefer to output to a csv file (which easily is imported again) , so:
[xml]$xml = Get-Content uccxResourceList_forReference.xml
$xml.resources.resource |
Select-Object firstname, lastname, extension,
#{Name="Team"; Expression={($_.team.name)}} |
Group-Object Team | ForEach-Object {
$_.Group | Export-Csv ("{0}.csv" -f $_.Name) -NoTypeInformation
}
Should return something like this:
> gc .\Executives.csv
"firstName","lastName","extension","Team"
"Montgomery","Burns","2000","Executives"
"Waylon","Smithers","2001","Executives"
> gc .\SafetyInspectors.csv
"firstName","lastName","extension","Team"
"Homer","Simpson","1000","SafetyInspectors"
"Frank","Grimes","1001","SafetyInspectors"
"Lenford","Leonard","1002","SafetyInspectors"
"Carlton","Carlson","1003","SafetyInspectors"
I would like to make a script that compares O365 tenant settings. Reading them is fine, now I would like to make some kind of difference object.
A related question is here, but no answer.
Powershell Compare-Object and getting the Differences into a File
I already have a json file from both tenants created like:
$srcTenant | Select-Object -Property * | ConvertTo-Json | Out-File "$targetfolder\$targetfile"
Now, I would like a file that only contains the properties that are collected with the script below:
I am so far:
$properties = ($srcTenant | Get-Member -MemberType Property | Select-Object -ExpandProperty Name)
$selectedproperties = #{}
$i = 0
foreach ($property in $properties) {
if (Compare-Object $srcTenant $trgTenant -Property "$property") {
$selectedproperties.Add($i, "$property")
$i++
}
}
The $selectedproperties variable contains 9 properties and I would like to export only this 9 in the same format as the other two.
Name Value
---- -----
8 StorageQuotaAllocated
7 StorageQuota
6 ResourceQuotaAllocated
5 ResourceQuota
4 OwnerAnonymousNotification
3 OneDriveStorageQuota
2 DefaultLinkPermission
1 ConditionalAccessPolicy
0 AllowDownloadingNonWebViewableFiles
So, I am looking for something like:
$srcTenant | Select-Object -Property (that 9 property above) | ConvertTo-Json | Out-File "$targetfolder\$targetfile
Other options achieving the same result are welcome too :)
Select-Object -Property can take an array of property names.
see the first example here
I found the below article to help me group a list of IP addresses.
Using Powershell, how can i count the occurrence of each element in an array?
The command I am currently using is:
get-content c:\temp\temp3.txt | group
This would get the following output:
> Count Name Group
----- ---- -----
3 192.168.1.1 {192.168.1.1, 192.168.1.1, 192.168.1.1}
3 192.168.1.2 {192.168.1.2, 192.168.1.2, 192.168.1.2}
What command can I use to find all IP's with over a count of 5?
I imagine I would need to put my orignal command as a variable like below:
$groupedoutput get-content c:\temp\temp3.txt | group
Unsure where to go from there. Any help would be appreciated.
Thanks,
S
In full:
Get-Content -Path c:\temp\temp3.txt |
Group-Object -NoElement |
Where-Object { $_.Count -gt 5 } |
Select-Object -ExpandProperty Name
In short:
gc c:\temp\temp3.txt | group |? count -gt 5 |% Name
The -NoElement switch means the groups don't gather up all this stuff: {192.168.1.1, 192.168.1.1, 192.168.1.1} , it's not really necessary here but it saves memory if you aren't going to use the grouped items.
In my folder testtemp, I've got this files
PS C:\scripts\testtemp> Get-ChildItem | select name,lastwritetime
Name LastWriteTime
---- -------------
AABASSI.TMC049913.mapdrives.txt 27/02/2015 11:01:11
AABASSI.TMC049916.mapdrives.txt 02/03/2015 14:29:31
AABASSI.TMC050020.mapdrives.txt 26/11/2014 18:08:48
aabassi.TMS064845.mapdrives.txt 06/03/2015 14:14:50
aaboud.DLECLERCQPC.mapdrives.txt 03/04/2015 09:02:03
aaboud.FHP030221.mapdrives.txt 30/12/2014 15:05:04
aaboud.FHP045846.mapdrives.txt 18/11/2014 10:15:05
Aaccus.FHP047416.mapdrives.txt 25/07/2014 08:16:19
AADRIENVASSE.PHP049659.mapdrives.txt 02/10/2015 14:18:15
AADRIENVASSE.RBXXAMET10.mapdrives.txt 06/08/2015 10:33:33
aagostini.FHP047082.mapdrives.txt 05/03/2015 13:38:07
aagricole.PHP053341.mapdrives.txt 30/03/2015 15:09:03
aagricole.TMS064745.mapdrives.txt 25/03/2015 12:32:06
aaiad.PHP053346.mapdrives.txt 09/06/2015 09:18:58
aaissaoui.FHP029908.mapdrives.txt 28/08/2014 14:41:09
aaissaoui.FHP030685.mapdrives.txt 15/11/2014 18:27:55
aaissaoui.FHP048791.mapdrives.txt 12/09/2014 10:22:19
aaissaoui.FHP048963.mapdrives.txt 06/11/2014 09:55:00
I just want to get only the most recent file for each login (for exemple AABASSi is a login)
If I try sort-object lastwritetime -descending and sort-object -property name -unique, there is a problem because for exemple, AAISSAOUI 28/08/2014 14:41:09 is NOT the most recent file :
Get-ChildItem | select #{l="name";e={($_.name.split(".")[0]).ToUpper()}},lastwritetime | Sort-Object lastwritetime -Descending | Sort-Object -Property name -Unique
name LastWriteTime
---- -------------
AABASSI 26/11/2014 18:08:48
AABOUD 30/12/2014 15:05:04
AACCUS 25/07/2014 08:16:19
AADRIENVASSE 02/10/2015 14:18:15
AAGOSTINI 05/03/2015 13:38:07
AAGRICOLE 25/03/2015 12:32:06
AAIAD 09/06/2015 09:18:58
AAISSAOUI 28/08/2014 14:41:09
Is ther a simple solution for my problem ?
The reason you're having the issue with sorting is that you sort it by the property you actually want, and then you sort it again by a different property (Name). Instead, group the objects by the string before the ".", and select only the newest object from each group. #PetSerAl's method is great:
Get-ChildItem | Group-Object {$_.Name.Split(".")[0]} | % {
$_.Group | Sort-Object LastWriteTime -Descending | Select-Object -First 1
}
I'm using the following script to get the comma counts.
Get-Content .\myFile |
% { ($_ | Select-String `, -all).matches | measure | select count } |
group -Property count
It returns,
Count Name Group
----- ---- -----
131 85 {#{Count=85}, #{Count=85}, #{Count=85}, #{Count=85}...}
3 86 {#{Count=86}, #{Count=86}, #{Count=86}}
Can I show the line number in the Group column instead of #{Count=86}, ...?
The files will have a lot of lines and majority of the lines have the same comma. I want to group them so the output lines will be smaller
Can you use something like this?
$s = #"
this,is,a
test,,
with,
multiple, commas, to, count,
"#
#convert to string-array(like you normally have with multiline strings)
$s = $s -split "`n"
$s | Select-String `, -AllMatches | Select-Object LineNumber, #{n="Count"; e={$_.Matches.Count}} | Group-Object Count
Count Name Group
----- ---- -----
2 2 {#{LineNumber=1; Count=2}, #{LineNumber=2; Count=2}}
1 1 {#{LineNumber=3; Count=1}}
1 4 {#{LineNumber=4; Count=4}}
If you don't want the "count" property multiple times in the group, you need custom objects. Like this:
$s | Select-String `, -AllMatches | Select-Object LineNumber, #{n="Count"; e={$_.Matches.Count}} | Group-Object Count | % {
New-Object psobject -Property #{
"Count" = $_.Name
"LineNumbers" = ($_.Group | Select-Object -ExpandProperty LineNumber)
}
}
Output:
Count LineNumbers
----- -----------
2 {1, 2}
1 3
4 4