By default, the Get-Migrations command, when run in the Visual Studio Package Manager Console, returns the list of Entity Framework migrations that have been applied to the target database in descending order. That is, the list is sorted to have most recent migrations first.
Is there a way to make Get-Migrations return the list of migrations in ascending order, that is, oldest migrations first, instead?
What I've tried so far:
Piped the output to the Sort-Object cmdlet, e.g. Get-Migrations [my params] | Sort-Object -Property Name (I'm not sure what property name to specify, or if this is even applicable?). All of these attempts silently failed (no error message displayed, no change in the Get-Migrations output).
Looked at the Powershell help for Get-Migrations (Get-Help Get-Migrations) and searched the web to see if Get-Migrations itself has a parameter that controls the output sort order. Couldn't find one.
Output of the Get-Migrations command I'm currently getting:
PM> Get-Migrations [my arguments...]
Retrieving migrations that have been applied to the target database.
201704121534436_SomeMigration999
201703291334212_SomeMigration998
[lots and lots more records here that are a pain to scroll through...]
201410110448547_InitializeTables
201410110018266_InitialCreate
Based on the provided sample output it seems the command returns text.
I'd split on the linebreaks, and the on the _ to get the 2 properties separated, be able to sort things afterwards:
#$migrations = Get-Migrations "..."
#test values
$migrations = "201704121534436_SomeMigration999
201703291334212_SomeMigration998
201410110448547_InitializeTables
201410110018266_InitialCreate"
$migrations.Split("`n") | ForEach-Object {
New-Object -TypeName psobject -Property #{
Date = $_.Split("_")[0]
Name = $_.Split("_")[1]
}
} | Sort-Object Name
Related
I tried running the following command in PS:
Get-EventLog -LogName Security | findstr 4720
The result I got seems to be squished as if the column widths need to be adjusted. How can I view all the text that is after the ellipses (...)? See screenshot: https://i.imgur.com/fqV5qIs.png
How to view the returned info in full?
As Santiago mentioned you can use Format-Table.
Though since it looks like you're looking for a specific Event ID, I'd recommend instead of using findstr (which may return unrelated results as it's searching for '4720' anywhere in your results - unless that's your intention of course) instead target the attribute using the Where-Object cmdlet (or its' alias ?). Also, if you want a "pure" PowerShell solution I'd recommend using Select-String instead of findstr
E.g.
Get-EventLog -LogName Security | Where-Object {$_.EventID -eq 4720} | Format-Table -AutoSize -Wrap
To expand on the answer from #Novalis, Where-Object like that is definitely faster than findstr, and the Format-Table should sort out the ... you're seeing.
But to take it one step further an even faster method is to use -FilterHashtable. Specifically :
Get-WinEvent -FilterHashtable #{Logname='Security';ID=4720} | Format-Table -AutoSize -Wrap
The reason it's faster is because when using Where-Object you're asking the system for ALL of the system logs, and then once received by your script you're then filtering them out (same with findstr). FilterHashtable just requests the log entries from system that match the require event ID, so less data needs to be sent to your script.
For license porpuses I try to automate the counting process instead of having to login into every single server, go into directory, search a file name and count the results based on the change date.
Want I'm aiming for:
Running a powershell script every month that checks the directory "C:\Users" for the file "Outlook.pst" recursively. And then filters the result by change date (one month or newer). Then packing this into an email to send to my inbox.
I'm not sure if that's possible, cause I am fairly new to powershell. Would appreciate your help!
It is possible.
I dont know how to start a ps session on a remote computer, but I think the cmdlet Enter-PSSession will do the trick. Or at least it was the first result while searching for "open remote powershell session". If that does not work use the Invoke-Command as suggested by lit to get $outlookFiles as suggested below.
For the rest use this.
$outlookFiles = Get-ChildItem -Path "C:\Users" -Recurse | Where-Object { $_.Name -eq "Outlook.pst" }
Now you have all files that have this name. If you are not familiar with the pipe in powershell it redirects all objects it found with the Get-ChildItem to the next pipe section and here the Where-Object will filter the received objects. If the current object ($_) will pass the condition it is returned by the whole command.
Now you can filter these objects again to only include the latest ones with.
$latestDate = (Get-Date).AddMonths(-1)
$newFiles = $outlookFiles | Where-Object { $_.LastAccessTime -gt $latestDate }
Now you have all the data you want in one object. Now you only have to format this how you like it e.g. you could use $mailBody = $newFiles | Out-String and then use Send-MailMessage -To x#y.z -From r#g.b -Body $mailBodyto send the mail.
I am using the following script to pull a list of all my computers from my wsus server. It then places then in a neat list and this works fine.
However, when I try to output the data to a text file it creates the text file but writes no data. Is there something I am missing in the Output part? Full script below.
$wsus = 'halvedge2'
[reflection.assembly]::LoadWithPartialName("Microsoft.UpdateServices.Administration") | Out-Null
$wsus = [Microsoft.UpdateServices.Administration.AdminProxy]::getUpdateServer()
$wsus.GetComputerTargets() | Select FullDomainName, LastReportedStatusTime, LastSyncTime
out-file -filepath "\\halvedge2\c$\PS1\WSUS\Last installed Update\updates.txt"
Additionally I would like to add an If statement to it looks at the LastSyncTime and for any LastSyncTime over 30 days it only returns them Computer Targets. How can I achieve this?
You do not have to out-file separately:
If the below yields value, then Instead of this:
$wsus.GetComputerTargets() | Select FullDomainName, LastReportedStatusTime, LastSyncTime
Do This:
$wsus.GetComputerTargets() | Select FullDomainName, LastReportedStatusTime, LastSyncTime | Out-File "D:\updates.txt" -Append -Force
Note: First try saving locally. If it works, then try remotely on the UNC. If remote fails, then give permission to the specific user to save it.
I'd like to have a script that grabs the logs from a list of paths and sorts all the entries in the logs into one large consolidated log. I'm fairly new at programming and powershell, but I've got a start. The log entries look like this:
2013-07-17 05:00:00,003 INFO [com.mpi.mp.viewer.web.servlet.ViewerLogFlusher] Skipped sending the empty string to wmsiislo
What I'm thinking is that I need to somehow parse each line into a multidimensional array, and sort it. The problem is, I can't get the hang of the [DateTime] method. My very incomplerte code is below:
#Code to append all logs together
$Biglog = Get-Content C:\Temp\logs\server1.log
foreach ($line in $Biglog){
#do something
}
try this :
ls c:\temp\logs\*.log | gc | out-file c:\temp\log.txt
Import-Csv C:\temp\log.txt -Header "date","info" |Sort-Object date | export-csv c:\temp\sortedlogs.csv
I'm writing a script which will automate the extraction of data from .blg Perfmon logs.
I've worked out the primary Import-Counter commands I will need to use to get the data out, but am trying to parametrise this so that I can do it for each machine in the log file (without having to open the log up in Perfmon, which can take 15 minutes or sometimes more, and is the reason I'm writing this script), and find out what each hostname is.
The script I have does the job, but it still takes a minute to return the data I want, and I wondered if there was a simpler way to do this, as I'm not too familiar with Powershell?
Here's what I have:
$counters = Import-Counter -Path $log_path$logfile -ListSet * | Select-Object paths -ExpandProperty paths
$svrs = #()
# for each line in the list of counters, extract the name of the server and add it to the array
foreach ($line in $counters) {
$svrs += $line.split("\")[2]
}
# remove duplicates and sort the list of servers
$sorted_svrs = $svrs | sort -unique
foreach ($svr in $sorted_svrs) {
Write-Host $svr
}
I'm just printing the names for the moment, but they'll go into an array in the proper script, and then I'll run my Import-Counter block with each of these hosts parametrised in.
Just wondered if there was a better way of doing this?
$sorted_svrs=Import-Counter "$log_path$logfile" -Counter "\\*\physicaldisk(_total)\% disk time" | %{$_.countersamples.path.split("\")[2]} | sort -Unique