Tag Archives: Python

PowerShell Copy, Shallow and Deep

Back in March, when I started this post, I was reading over some Python code. Yeah,… you read that right. As I did that, I was introduced to a term I don’t think I’ve seen before: It was “deepcopy.” The Python module import looked like this: from copy import deepcopy. Deepcopy, what is that?

I don’t remember the name for it, but as I read up on this term and the function from this Python module, I do remember coming up against this in PowerShell. Essentially, it’s this. If you make a copy of an array—we’ll use that for this example—changes to the original array will change the copy, as well. The deepcopy option allows Python to create a copy that isn’t tied to the original. Let’s begin by looking at a shallow copy example in PowerShell.

Any change made to the original object—$Array—will be seen in the copy of the object, too. There’s a link between them. As a shallow copy, its values will continue to reference the original object. A deep copy is independent; there is no reference to the original object. Let’s work through a quick example of making a deep copy of our array in PowerShell.

I gather that there may be other ways to create deep copies, and you’re welcome to comment and share those if you’d like. This is an extremely simple and straightforward example—my favorite—of something of which you may need to be careful, as you continue to write PowerShell.

Encoding and Decoding PowerShell Strings

Every few months Base64 comes up and I have to go looking for that one post I saw that one time. It’s because that code, on that one site, hasn’t been memorized — not by me, anyway. So, here it is. The below example shows how to encode and decode a string using Base64. Keep in mind that different things, such as HTTP Headers, might require different character sets. In this example, I’m using UTF-8, but it could have been ASCII, or Unicode, or something else.

Clear-Host
$UserName = 'tommymaynard'
$Password = 'password'

"The UserName is '$UserName' and the password is '$Password'."
"Encoding as $($UserName):$($Password)"

$Text = "$($UserName):$($Password)"
$Bytes = [System.Text.Encoding]::UTF8.GetBytes($Text)
$EncodedText =[Convert]::ToBase64String($Bytes)
"Encoded text: $EncodedText"

$DecodedText = [System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String($EncodedText))
"Decoded text: $DecodedText"

When the above code is executed it produces the below results.

The UserName is 'tommymaynard' and the password is 'password'.
Encoding as tommymaynard:password
Encoded text: dG9tbXltYXluYXJkOnBhc3N3b3Jk
Decoded text: tommymaynard:password

And, now that’s that. When I need this next, it’ll be right here on my own blog. Now back to that Python to PowerShell project where I need Base64 encoding. By the way, this is a great online encode/decode tool that you might find helpful: https://www.base64decode.org.

It’s Been Forever, Right?

You may not have even noticed — some did; some didn’t, but it’s been real quiet around here. Here, as in my blog. Due to the pandemic, I’ve had much more time at home to write, and somehow I just didn’t. I rode the exercise bike more, I sat in the backyard and watched the desert animals more, I drank more iced tea — probably, I washed my hands more, and I even slept more. That approximate hour that’s necessary to drive, park, and walk into the office has been used for extra sleeping, minus the time lost in the morning for the exercise bike. It hasn’t been used for, well writing. There’s been plenty of learning, authoring PowerShell, and picking up certifications, but it’s about time to start authoring PowerShell content again.

During my dry (writing) period, I felt like I should learn more about Azure and Microsoft 365, so I studied, tested, and passed those two fundamentals certifications. I also got that Microsoft Python certification — huh? Did you even know that was a thing!? I think I found out about the certification around mid-2019 and I instantly wanted it based on the strangeness of the whole thing. I thought more about it, however, and came up with some better reasoning. One, while I’ve read one Python book, I haven’t had an opportunity to implement Python in production and so perhaps the certification would provide some proof of skill in that area. Two, Microsoft has a Python certification. This isn’t the same as the initial reason. Microsoft is serious about Python, so much so that they want me to know it. This means that it may have value alongside any Microsoft and/or Azure work I may complete in the future. We all know how important and frequently used Python is across our industry. Even as PowerShell aficionados, it’s something we have all probably noticed — so why not know something about it, too? Beyond that, PowerShell skills transfer to Python quite well.

I won’t begin discussing it in this post so much, but it’s Splunk that’s really brought me back. Learning something about it and writing code that sends data to it, is why I’m even authoring this post — I just know it. I’ve spent the last few weeks learning about how to get output from my PowerShell functions into Splunk as telemetry data. That code is sitting idle in VS Code now, and it needs a home. Well, a home outside its code repository. So while we won’t start discussing it more now, I will be discussing it soon. As I’ve done before, I intend to use my blog to help hang on to code I may want in time. Additionally, and like I’ve always meant to do — going all the way back to 2014 — it may help someone else. There was a good deal of information I didn’t understand a few weeks ago. The PowerShell Splunk code I did find online wasn’t instantly beneficial to me. I didn’t have a Splunk background. Hopefully, someone reading my upcoming writings (that has a PowerShell background), will find it beneficial due to only having a small amount of, or no, experience with Splunk. I’ll do my best to help explain the Splunk part the way I had hoped I would’ve found it online.

Back soon (this time).