I have a CSV file, Devices.csv, containing IP Address, DeviceName, SerialNumber, MAC Address, UserName.
The Users column in all the rows of Devices.csv is prepopulated with a value [Unknown]
The code...
{Import-CSV Devices.csv | Where-Object {$_.IPAddress -eq '192.168.2.124'} | Select-Object -last 1 | FT IPAddress, devicveName, SerialNumber, MACAddress, User -AutoSize }
....outputs
IPAddress deviceName SNumber MACAddress User
--------- ----- ------- ---------- ----
192.168.2.124 ComputerA 1ABCDEFG 00xxYYbbCCdd [Unknown]
I want to be able to replace the [Unknown] text with a Username I have retrieved from a different source. Can I update just the User column on this line in Devices.csv using powershell and keep the rest of the CSV file intact ?
Thanks, Brian
$csv = Import-CSV Devices.csv
$csv | Where-Object {$_.IPAddress -eq '192.168.2.124'} | Select-Object -Last 1 | ForEach-Object {$_.User = $user}
$csv | Export-Csv Devices.csv -NoTypeInformation
Try this:
$otheruser = '...'
Import-Csv Devides.csv | % {
if ($_.User -eq '[Unknown]') { $_.User = $otheruser }
$_
} | Export-Csv Divices_modified.csv -NoTypeInformation
Related
I have this pipeline:
Get-Mailbox -ResultSize Unlimited –RecipientTypeDetails UserMailbox,SharedMailbox |
Where {(Get-MailboxStatistics $_.Identity).LastLogonTime -gt (Get-Date).AddDays(-36)} |
Sort -Property #{e={( Get-MailboxStatistics $_.Identity).LastLogonTime}} -Descending |
Select-Object DisplayName,#{n="LastLogonTime";e={(Get-MailboxStatistics $_.Identity).LastLogonTime}} |
Get-MailboxStatistics |
Select-Object displayname, totalitemsize |
Export-Csv -Path 'C:\JV'
but the output never gets the third column in the output table - size.
DisplayName LastLogonTime
----------- -------------
Some User 2/8/2022 2:47:26 PM
Another Name 2/8/2022 2:46:59 PM
Yet AnotherName 2/8/2022 2:46:54 PM
I believe this should be a more efficient and straight forward way of getting the data you're looking for:
$dateLimit = [datetime]::Now.AddDays(-36)
Get-Mailbox -ResultSize Unlimited –RecipientTypeDetails UserMailbox, SharedMailbox | ForEach-Object {
$stats = Get-MailboxStatistics $_.Identity
# If the date is lower than the date limit, skip this user.
if($stats.LastLogonTime -lt $dateLimit) { return }
[pscustomobject]#{
DisplayName = $_.DisplayName
LastLogonTime = $stats.LastLogonTime
TotalItemSize = $stats.TotalItemSize
}
} | Sort-Object LastLogonTime -Descending | Export-Csv -Path 'C:\path\to\csvfile.csv' -NoTypeInformation
I want to get an output like below.
My output now :
Name Active PrimarySmtpAddress
---- ------ ------------------
DG_Group1 True mail1#contoso.com
DG_Group2 False mail2#contoso.com
DG_Group3 True mail3#contoso.com
My desired output :
mail1#contoso.com
mail2#contoso.com
mail3#contoso.com
script :
$DistroLists = Get-DistributionGroup -ResultSize Unlimited
$MessageTrace = Get-MessageTrace -RecipientAddress $DistroLists.PrimarySmtpAddress -startdate (Get-Date).AddDays(-8) -EndDate (Get-Date)
$DistroLists |
Foreach-Object {
$_ | Add-Member -MemberType NoteProperty -Name Active -Value (
$_.PrimarySmtpAddress -in $MessageTrace.RecipientAddress
) -PassThru
} |
Select-Object Name, Active, PrimarySmtpAddress | Where-Object Active -EQ "FALSE"
Out-File C:\output.txt
You can use Select-Object -ExpandProperty or ForEach-Object -MemberName to grab only the value of a specific property from one or more piped input objects:
... |Select-Object Name, Active, PrimarySmtpAddress | Where-Object Active -eq $false | ForEach-Object -MemberName PrimarySmtpAddress | Out-File...
You can skip first two lines and split the line using space characters. Finally pick the last column as given below:
Get-Content C:\Projects\logtext.txt | Select-Object -Skip 2 | ForEach-Object { "$(($_
-split '\s+',3)[2])" }
mail1#contoso.com
mail2#contoso.com
mail3#contoso.com
I run this command and I get all computer hostnames in the names.txt file.
Each hostname in the file is on a separate line, but every hostname is followed with white spaces which cause an issue when I try to read this file. How can I output to this file without getting the white spaces on each line?
Get-ADComputer -Filter * | Select-Object -property name | Sort-Object -Property name | out-file -filepath C:\temp\names.txt
You have the problem that you don't just have names, you have objects with the property 'name', and you also have the problem that Out-File runs complex objects through some kind of formatting before sending them to the file.
To fix both, expand the name out to just text, and generally use Set-Content instead:
Get-ADComputer -filter * | Select-Object -ExpandProperty Name | Sort-Object | Set-Content C:\temp\names.txt
or in short form
Get-ADComputer -filter * | Select -Expand Name | Sort | sc C:\temp\names.txt
or
(Get-ADComputer -filter *).Name | sort | sc C:\temp\names.txt
expandproperty should get rid of the #()
Get-ADComputer -Filter * | sort Name | Select -expandproperty Name | %{ $_.TrimEnd(" ") } | out-file -filepath C:\temp\names
Untested no AD#home
Piping it through this should work (before piping to the out-file):
EDIT: Piping through % { $_.name } should convert #{name=VALUE} to VALUE:
% { $_ -replace ' +$','' } | % { $_.name }
Like this:
Get-ADComputer -Filter * | Select-Object -property name | Sort-Object -Property name | % { $_ -replace ' +$','' } | % { $_.name } | out-file -filepath C:\temp\names.txt
I need to decide the columns' orders of my table. My actual command is that one:
$tab | Sort-Object "Pourcentage" -Descending |
Format-Table -AutoSize |
Out-String -Width 4000 |
Out-File -Append O:\sigdci\public\parcoursArborescence\bilanAnalyse.txt
It gives me that order:
Derniere modification Categorie recherchee Dernier acces Dossier Pourcentage
But I need "Dossier" to be first, then "Categorie recherchee" and "Pourcentage" shall be 2nd and 3rd. How shall I proceed?
Specify the column headers in the desired order:
$tab | Sort-Object "Pourcentage" -Descending |
Format-Table 'Dossier', 'Categorie recherchee', 'Pourcentage',
'Derniere modification', 'Dernier acces' -AutoSize |
Out-String -Width 4000 |
Out-File -Append 'O:\sigdci\public\parcoursArborescence\bilanAnalyse.txt'
If you need to dynamically determine the column names you could do it like this:
$headers = $tab[0].PSObject.Properties |
Where-Object MemberType -eq NoteProperty |
Select-Object -Expand Name
However, you'd have to bring that list into your desired order somehow. Perhaps you could do it like this:
$allHeaders = 'Dossier', 'Categorie recherchee', 'Pourcentage',
'Derniere modification', 'Dernier acces'
$actualHeaders = $tab[0].PSObject.Properties |
Where-Object { MemberType -eq NoteProperty } |
Select-Object -Expand Name
$headers = $allHeaders | Where-Object { $actualHeaders -contains $_ }
$allHeaders is an array that contains all headers in the correct order. Then you remove all items that aren't present in $actualHeaders from that list, preserving the order of the remaining headers.
I am using the code below to get a list of computer names where they were last modified 181 days ago. I have a .csv file with 2 columns (ComputerName, UserName) Is there a way I can match the output from the code below with the computername column in the csv file and show the output/matches?
$Days = (Get-Date).AddDays(-181)
Get-ADComputer -Property Name,lastLogonDate -Filter {lastLogonDate -lt $Days} -Server DomainController -Searchbase "OU=US,DC=contoso,DC=net" | FT Name,lastLogonDate
Change ... | FT Name,lastLogonDate to ... | select Name,lastLogonDate. You can still pipe the result into ft to format it as a table, but separating selection from presentation will make it easier to put in additional filters.
For displaying just the computers that have matches in your CSV you could do the following:
$computers = Import-Csv 'your.csv' | % { $_.ComputerName }
Get-ADComputer -Property Name,lastLogonDate ... | select Name,lastLogonDate |
? { $computers -contains $_.Name } | ft
To include the username from the CSV with the result you could do something like this:
$computers = #{}
Import-Csv 'your.csv' | % { $computers[$_.ComputerName] = $_.UserName }
Get-ADComputer -Property Name,lastLogonDate ... |
? { $computers.Keys -contains $_.Name } |
select Name,lastLogonDate,#{n='Username';e={$computers[$_.Name]}} | ft