Skip to main content

Posts

Showing posts from 2018

Powershell Commands useful for large CSV files

1) To get the header line, ie: first row in a CSV file  gc .\FILENAME.csv -TotalCount 1 2) To get the header line, and export it to another CSV file.  Note the use of Set-Content rather than Export-CSV gc .\FILENAME.csv -TotalCount 1 | Set-Content .\header-file-name.csv 3) To filter the CSV file based on a regex search , and export the matches to another file. gc .\FILENAME.csv | Select-String -Pattern ( '.*2018-11-20.*' ) | Set-Content .\inner-file-name.csv 4) To see the last few lines, like tail  gc .\20181206_InteractionDetails.csv | select -last 10

Some find commands - useful for copying iCloud Photos to another service.

I've found these commands really helpful when navigating an iCloud Photo library on a Mac. The native Photos Application on MacOS prohibits a lot of bulk file activity.  These commands should be run from Terminal. Before starting, it's best to place yourself in the following directory "/Users/**your username**/Pictures/Photos Library.photoslibrary" This command is best executed from the "Masters" directory  find . -iname  *.cr2  -exec ls -lh {} \; It finds all CR2 files and reports the file sizes   You could change the ls to a copy, say to copy the image to another folder, like another cloud drive.  Find all file types outside a defined list, and filter those only in the Masters directory find . -iname *.* | grep Masters | grep -v -i -E '\.jpg|\.jpeg|\.heic|\.png|\.cr2|\.gif|\.mp4'  Find all file types IN a defined list, and filter those only in the Masters directory find . -iname *.* | grep Masters |

Get Results of Stored Procedure and insert to temp table

I needed to get the storage results recently of a bunch of SQL tables.   Here's how I did it. 1) Create a temporary Table CREATE TABLE #spaceused ( nameout VARCHAR(50) ,rowsout int ,reservedout VARCHAR(50) ,dataout VARCHAR(50) ,index_sizeout VARCHAR(50) ,unusedout VARCHAR(50) ) 2) Send the results to the table, using sp_spaceused  I also created a script to generate about 100 of these INSERTs INSERT INTO #spaceused EXEC sp_spaceused N'TableName' ; GO 3) Query the results... SELECT TOP 10 * FROM #spaceused 4) Delete the temp table when done.  DROP TABLE #spaceused

Send a powershell object straight to SQL Server

Recently I needed to copy directory contents details to an SQL Server. This is the powershell script I used to do it. $path = "C:\Files\" $Table  = Get-ChildItem -Path $path |  Select-Object Name , FullName , CreationTime , Length Write-SqlTableData -ServerInstance "PBRUCE-DT" -DatabaseName "PlayDatabase" -SchemaName "dbo" -TableName "FileInfoArchive" -InputData $Table  -Force

Powershell Active Directory - User and Group

Recently I needed to find out if a user was in an Active Directory Group. The group contained more than 5000 entries, so using Get-ADGroupMember wasn't useful (it timed out) The following command worked well for me... get-adgroup " GroupName " -Properties Member | Select-Object -ExpandProperty Member | get-aduser |  Where-Object {$_.SamAccountName -eq " username "} For small groups, and for full member lists it's as simple as  Get-ADGroupMember groupname   Getting the actual AD details can be performed with powershell with the following get-aduser -Filter 'GivenName -like " FirstName* " -and Surname -like " LastName* "' or, if you know the SamAccountName... get-aduser  SamAccountName To list the groups that an account belongs to  Get - ADPrincipalGroupMembership username | select name

Get a list of MSSQL processes and their sql text

SELECT sqltext.TEXT, req.session_id, req.status, req.command, req.cpu_time, req.total_elapsed_time / 1000 AS runTimeSecs, req.user_id, req.reads, s.login_name, s.database_id, t.name AS [database_name] FROM sys.dm_exec_requests req inner join sys.dm_exec_sessions s        on req.session_id = s.session_id  inner join sys.databases t        on s.database_id = t.database_id CROSS APPLY sys.dm_exec_sql_text(sql_handle) AS sqltext

Getting Access to a Seagate Central Hard Disk

List Available LVM Volume Groups pvs List what Logical Volumes exist inside the group lvdisplay vg1   (enter the VG listed from the VG column from pvs)  Mount normally, ie: mount /dev/vg1/lv1 /mnt/disk  However, if the block size is greater than 4KB, you'll need to use fuseext2, something like... fuseext2 -o ro -o sync_read /dev/vg1/lv1 /mnt/disk

thread.cs not found in C#

Recently I received an error in Visual Studio 2015 like this.. TargetInvocationException was not handled An unhandled exception of type 'System.Reflection.TargetInvocationException' occurred in PresentationFramework.dll In addition, the main window stated thread.cs not found.   Because I made a few changes before running the code (it compiled fine) It took me a while to locate the problem, it linked back to a XAML control. I previously had the code... GetRogue_Button.Visibility = Visibility.Hidden; But, when the code ran, the Button itself was null.  The method I placed the hide code in was called through an TextChangedEventHandler (I also used an observable collection in my code).  The solution: if ( GetRogue_Button != null && GetRogue_Button.Visibility != Visibility.Hidden) { GetRogue_Button.Visibility = Visibility.Hidden; }