Using Object properties

A lot of people see PowerShell as conversion tool, but it can do so much more.

It can uninstall apps, inventory your applications. Rename files to a standard. Create shares, modify files and sessions and give an Administrator access to a computer without interrupting a session.

It is built on .net/c# so it can do everything a program can. You just have to know your property and methods on your objects.

I am mainly speaking about windows platform. I may do a write up on Linux impact.

I can write excel, word files with out office being installed, send emails and query websites for remote data. Consolidated information and write to databases. The list is ridiculous.

I wanted to interact more so I would like to take on a challenge from the comments. What is something you think you cannot do in PowerShell?!

PowerShell Importing Data from Excel

If you want to import Excel data I recommend giving the people who toke the time to develop this module Import-Excel. It can be Read about on the PSGallery here: https://www.powershellgallery.com/packages/ImportExcel/

After reading we will try to find the module using built im module package for powershell :

“find-module importexcel”

we can pipe this into an “install-module” command with a few parameters to ensure a smooth install.

find-module importexcel | install-module -Scope CurrentUser -SkipPublisherCheck

this should install the module and you import Excel workbooks as easily as you can excel and text files.

Import-Excel, Export-Excel with a few extra parameters like -worksheet so you can target data sets and even write reports with details on different pages!

I use this to write share permission reports with different shares on different worksheets. So other things you can do is a security audit on servers and write a server to each worksheet and then write a pivot table for a summary report.

I how you download and support this module because it is nothing short of amazing, you dont have to have the excel application installed to import or export to Excel!

Convert from OATime

One of my favorite reasons I love PowerShell is because can tap into its programming roots for added power. I will demonstrate later in my post.

If you notice the data is the same in both fields and the send row and under the time column the data type is changed and now actually displays a date.

Here is why:

An OLE Automation date is implemented as a floating-point number whose integral component is the number of days before or after midnight, 30 December 1899, and whose fractional component represents the time on that day divided by 24. For example, midnight, 31 December 1899 is represented by 1.0; 6 A.M., 1 January 1900 is represented by 2.25; midnight, 29 December 1899 is represented by -1.0; and 6 A.M., 29 December 1899 is represented by -1.25.

The base OLE Automation Date is midnight, 30 December 1899. The minimum OLE Automation date is midnight, 1 January 0100. The maximum OLE Automation Date is the same as DateTime.MaxValue, the last moment of 31 December 9999.

first there are not many reasons to convert excel to csv (who am I kidding it is tons). I ran across this issue helping a friend. I can’t believe how incredibly complicated it was to convert the time into a format that was more legible inside of the CSV file.

I think we can agree that sometimes things are harder than what they need to be and I will ensure that this can go easier specially if you take the time to make a function so that you can recall the fix in the future.

Here is a partial solution found as method:

but wait, this is to convert to not convert from!
After looking for all the methods there is no method for ‘fromOADate’!

It clicked immediately that .Net had the solution in its C# library….hopefully. https://docs.microsoft.com/en-us/dotnet/api/system.datetime.fromoadate?view=netcore-3.1

After visiting the link and reading I was more confused and still hopeful I had remembered C#/.net calls look different in PowerShell.

Looks like it the call is unrelated but I only needed the syntax to conjure my own C#

After playing with the way it was called.

now that the proof of concept is there, YAY!

now to test this solution in a different form of reusable code:

$data is defined
Function is defined and ready to take objects and pipline input.

here it is with plain input:

but it could be working incorrectly, lets plug in the data.

lets see the final solution

I know this one was lengthy but I felt like it helped really breakdown the process for beginners.

now the the script/Code:

$data=@()
$data+=New-Object psobject -Property @{name="Xajuan"; Age="" ; Birthdate=41526}
$data+=New-Object psobject -Property @{name="Tyesha"; Age="" ; Birthdate=33172}
$data+=New-Object psobject -Property @{name="Xajuan"; Age="" ; Birthdate=31234}





function Convert-FromOATime
{
<#
.Synopsis
   Coverting OA time to regular time. 
.DESCRIPTION
   A proof of concept blending programming knowledge and scripting.
.EXAMPLE
   $data.birthdate | Convert-FromOATime
.EXAMPLE
   $data | select name,@{name='age';e={(get-date -format yyyy) -($($_.birthdate|Convert-FromOATime|get-date -format yyyy))}},@{name='birthdate';e={$_.birthdate|Convert-FromOATime}}
.NOTES
   By Xajuan Smith
   PowerShell.city
#>
[cmdletbinding()]
param([parameter(Mandatory,ValueFromPipeline,ValueFromPipelineByPropertyName)][Alias('Time')][string[]]$Date)

begin{
        $results=@()
     }

Process{
            foreach($Item in $Date)
            {
                $results+=[DateTime]::FromOADate($($Item)) | get-date -Format MM-dd-yyyy

            }
       }


end{$results}


}#end Function


#example
$data | select name,@{name='age';e={(get-date -format yyyy) -($($_.birthdate|Convert-FromOATime|get-date -format yyyy))}},@{name='birthdate';e={$_.birthdate|Convert-FromOATime}}

Formatting dates from text or objects

Ever imported data just to have a non readable date stamp?

Get-date can take your text that is a date and turn it into something more readable.

Take “15-FEB-20” for example. It’s not really a wildly used format.

Pipe that to get-date like so:

Then you can format your date to remove time and what not.

If you have imported the data you can fix the entry using the select statement.

$data |select name,@{name=’Birthday’;e={$_.birthday | get-date -format MM-dd-yyyy}}

Planning stops the issues from Spanning.

Planning is vital to any infrastructure environment and when scripting or using PowerShell is by no means any different. when you add a high level plan/structure and use it as a map of to-dos and things you want to add later and things you would like to change.

See the source image

Failure to plan can mean data loss and errors that go uninvestigated. Either of which is not what you want to lay eye on or expose ears to. Error handling should go hand in hand with any PowerShell approached solution to cover your bases.

One way is to write a road map of comments in your script prior to coding. reference snippets as often as you can. enough detail so that you wont be lost of you reference that script or function 2 or 3 years from current date to create easily understood code and recycle.

Does anyone object?

Writing scripts and functions we sometimes just want something to do or be one way with one purpose.

I have mulled over things and re-coded tools to work with different things. The one thing I find to be the best in every situation is always script or code with objects in mind so that the rescuing of old code is easier.

Yeah it takes more time to do this and ensure that you aren’t reinventing the wheel.

The reality of even using PowerShell is to make your life easier. You want to ensure the process you use is refined enough to keep management at bay on your decisions and feeling confident about your solutions by reassuring them with facts and successes.

So instead of making a paper air plane that only flys in circles. Create a small engine that can fly a tiny drone and can be controlled with a strict protocol.

You may think to your self drones are cumbersome and you don’t always need to fly things but, hang on! That tiny engine can go in to a tiny car and use the same controller and so on.

I don’t want you to only be a pilot but an engineer that can reuse useful creations. Explore your horizons during down time, explore modules and other scripts to become inspired.

Take what you see and make it your foresight for newer projects. Have you taken the time to create something lately and really make it reusable?

Remember the Shell runs on your mind Power!

Google cloud and the absence of PowerShell.

I was looking into google cloud to explore some options for a client to leverage some automation elements and was very interested until I learned of the lack of PowerShell support.

For the life of me I cannot understand Google’s purpose to neglect populations of individual platforms and still want professionals to take their platform seriously as their competitors. Okay I’m done complaining.

It is something I sincerely hope Google notices and rectifies so that it supports PowerShell as it installs on all OS genres. To leave out automation potential is saying I’m just doing this cloud thing to be like everyone else.

So in my humble opinion there is simply no Power in google cloud shell.

How many others are fine learning python and writing shorthand and okay with not embracing a world where we take any OS as a catalyst that connects your tools to all the cloud providers.

Let me know what you think in the comments below.

Mobile PowerShell Needed?

I have always wanted to get my mittens on a mobile iteration of the prized management tool.

Pocket PowerShell

I made sure I got a mobile sized tablet that would allow for me to use this on the go. Remote Desktop, Team viewer, Screen connect, VNC just dont respond like a native client. Wouldn’t it be something to truly take your scripting on the road with you?!

I believe this is possible now with todays mboile phones and tablets.

Ready to script.

What are your thoughts and your ideal access to management and automation tools?