Tuesday, March 12, 2019

Favorite Powershell Scripts

Mock sitemap
This simple script gives us an overview of all pages - we just need to clean up the bin and obj folder listings  - do some simple replaces and turn it into links or a sitemap
It creates a list of all of the filetypes specified into an output text file

Below I have specified on .aspx files but you could do something like .htm,asp,aspx instead to include multiple file types.

Get-ChildItem D:\mydirectory\My.WebApplication -recurse -include *.aspx| ForEach-Object { $_.FullName } > D:\mydirectory\My.WebApplication\output.txt

Tuesday, December 4, 2018

Linq Exclude from separate list

 var urlList = new List<DisplayAssets>();

 var assetsToExclude = (from a in db.DisplayAssets
                join h in db.Hardware_Assets on a.id equals h.displayId
                where h.assetId == hardwareid
                select a.id);

var allAssets = db.DisplayAssets;

var urls = allAssets.Where(x => !assetsToExclude.Contains(x.id));

        urlList = urls.Select(a => new DisplayAssets()
            locaterAddress  = a.locaterAddress,
            assetType = a.assetType,
            id = a.id

Sunday, January 28, 2018

Form Collection 3 approaches

Here are 3 ways to do it specifically with a FormCollection object.

public ActionResult SomeActionMethod(FormCollection formCollection)
  foreach (var key in formCollection.AllKeys)
    var value = formCollection[key];

  foreach (var key in formCollection.Keys)
    var value = formCollection[key.ToString()];

 // Using the ValueProvider
     IValueProvider valueProvider = formCollection.ToValueProvider();
            foreach (string key in formCollection.Keys)
                ValueProviderResult result = valueProvider.GetValue(key);
                string value = result.AttemptedValue;

Linq and Lambda syntax

var jsonData = from user in users
               select new[] { user.Id.ToString(),
                              user.Person.Firstname };
Alternatively, you can use the lambda syntax:
var jsonData = users.Select(user => new[] { user.Id.ToString(),
                                            user.Person.Firstname });

Join Linq To list outside of database

    var filterList = new List<string>();

            var rptRecords = (from wo in BucketEntities.tbl_SalesWorkOrder
                              join fi in BucketEntities.FranchiseInfoes on wo.FranchiseID equals fi.FranchiseID
                              join pc in BucketEntities.tbl_Pivotal_Company on fi.AccountCode equals

                              join wot in BucketEntities.tbl_SalesWorkOrderTasks on new { wo.OrderID, wo.FranchiseID } equals
                            new { wot.OrderID, wot.FranchiseID }

                              where (wo.WSADate >= startdate && wo.WSADate <= enddate)
                                && wot.AcceptedYN != false
                              where ((wo.DispatchCode == "DVC") || (wo.DispatchCode.Substring(0, 10) == "Dryer Vent")) ||
                             ((filterList.Contains(wot.TaskCodeNumber) || wot.TaskCodeNumber == "00"))

Formatted object title

We often use the title to scroll over for more information.  In this example I was tasked with showing a complete address on scroll over.  I wanted to drop HTML into the title.  I used the MvcHtmlString  and the String Format and the Html.Encode methods to accomplish this.

MvcHtmlString s = MvcHtmlString.Create(String.Format("{0}\n{1}\n{2},{3} {4}", c.division.divisionName, c.division.street, c.division.city, c.division.stateCountry.stateCountryName, c.division.postalCode));

 <li  title='@Html.Raw(Html.Encode(s))'>@c.division.company.companyName</li>

Static Copy Method

I keep this in a static class called ManipulateModels 

 public static void CopyTo(this object S, object T)
//Works fine however if a default value is set then it will need to be handled or it will be reset
            var notNullProps = S.GetType().GetProperties()
                                        .Where(x => x.GetValue(S, null) != null);

            foreach (var pS in S.GetType().GetProperties())
                foreach (var pT in T.GetType().GetProperties())
                    //example for Default Value
                    //had to do this due to setting a default value of 0
                    //if (pT.Name == "BuildingID") continue;
                    if (pT.Name != pS.Name) continue;
                    object x = pS.GetValue(S, null);
                    if (x != null)
                        (pT.GetSetMethod()).Invoke(T, new object[] { pS.GetGetMethod().Invoke(S, null) });
            //foreach (var pS in notNullProps.GetType().GetProperties())