[PowerShell] How to Create Simple Class

As a C# developer, I think that I should need to know how to create a class in PowerShell. Since Windows PowerShell 5.0, it adds the ability to create a class. With a class, we could do the coding more like C# (this is a good news to a C# developer like me, lol). We could create methods properties, enums, and other things to Class in PowerShell.

The first thing we need is, use the Class keyword. This is a sample on it,

Class Vehicle {

}

Within the brace, we could add our items in it. Be careful when we create object in PowerShell. The Windows PowerShell automatic type system works remarkably. it allow us create any variable without specify its type. Everything will simply be a System.Object. if you have a string, an int, or a DataTime object, the best practice (or you must) is to specify that type so that things will work well.

Because I am creating a class for a vehicle, I could create something like, Vehicle Registration Plate, Model, Year, Number of Doors, Number of Wheels, Seating Capacity, Engine Displacement, Engine Max Torque….etc

[String]$RegPlate
[int]$NoOfDoors
[datetime]$Year
[String]$Model
[int]$numberOfWheels = 4
[int]$numberOfDoors = 5

I can then use the New-Object cmdlet to create an instance of the Vehicle class,
$myVehicle = New-Object Vehicle

I can now get the default values of all variables (class properties) in $myVehicle.

Now I can simply assign values to $myVehicle,

Now I could see all the updated values,

That is how you create a simple class in Windows PowerShell 5.0. Let’s talk about adding more stuff to the class later. Hope you enjoy and learn the PowerShell with me.

 

[PowerShell] How to Retrieve Data with ADO.NET

I am primary a C# developer since 2005. Before that I was a VB5/VB6/VB.NET developer. Recent, I changed my role from development side to a consulting side. I found out that I have to do some development by using powershell script. It is because I will pass the program to client and we should try to build with script or any “no compile needed language”. And so I start writing PowerShell script. Because of this, I will write some blog about PowerShell  that I learned from my job. And this is the first one in this series.

My first PowerShell task is, retrieve data from MSSQL.

From my C# knowledge, I would like to retrieve data by using ADO.NET. So I start looking for how I could use C# library in PowerShell Script. And lucky that there is a way.

Below is my first script in PowerShell.

Function GetDataFromMSSQL {
    Write-Host "====================================="
    $sqlConn = New-Object System.Data.SqlClient.SqlConnection
    $sqlConn.ConnectionString = "Server=localhost;Integrated Security=true;Initial Catalog=AdventureWorks2019;"
    
    try {
        $sqlConn.Open()
        Write-Host "Data Source is connected."
        #$sqlcmd = $sqlConn.CreateCommand()
        $sqlcmd = New-Object System.Data.SqlClient.SqlCommand
        $sqlcmd.Connection = $sqlConn
        #$selectQuery = "SELECT TOP (1000) [SpecialOfferID],[Description],[DiscountPct],[MinQty],[MaxQty] FROM [AdventureWorks2019].[Sales].[SpecialOffer]"
        $sqlcmd.CommandText = $selectQuery

        $sqladp = New-Object System.Data.SqlClient.SqlDataAdapter $sqlcmd
        $data = New-Object System.Data.DataSet
        $sqladp.Fill($data) | Out-Null

        $rowCount = $data.Tables[0].Rows.Count
        Write-Host "$rowCount rows returned."
        $data.Tables
        $true
    } catch {
        Write-Host "Data Source cannot be connected."
        Write-Host $Error[0].Exception.Message
        $false
    } finally {
        $sqlConn.Close()
    }
}
GetDataFromMSSQL

 

No internet access on Host after enabled Hyper-V (or any VM software, i.e., VirtualBox)

Recent, I installed Oracle VM VirtualBox and enabled Hyper-V feature on my Win10 Professional (1709). Since then I cannot browse any intranet or internet web pages in IE and Edge. But there is no problem at all if I disable the newly added virtual network adapter.

I tried to google and search how to fix it. But I found no clue at all.

After few days, suddenly a light bulb goes on in my head.

I think it might be the adapter priorities so all the network traffic is sending out through the virtual network adapter. I then find out the REAL solution to fix it. Now all of the newly added virtual network adapters are enabled and I have no problem at all on browsing intranet and internet web pages.

Here are the steps to fix.

    1. Open Windows PowerShell
    2. Run “Get-NetAdapter” to list all the adapters info
    3. Now you can see the IfIndex value for the virtual network adapter (it is 18 in my PC, it will be other value in your PC). Remember this value.
    4. Then we run “Get-NetIPInterface” to list all the IP interface info
    5. The “real” network adapter for me to go intranet and internet is IfIndex=52, and its InterfaceMetric is 35.
    6. The virtual network adapter (IfIndex=18) is having InterfaceMetric=15.
    7. In windows, the lower number means the higher priorities. So that means all the network traffic will route out from the virtual network adapter and so it seems I lost all the internet access. Now what we should do is, lower the priorities of the virtual network adapter. Because my wifi adapter is having InterfaceMetric=45. So I think putting 55 for the virtual network adapter is a good choice. Let’s try to make the change now.
    8. Let’s open “Windows Settings”
    9. Click into “Network & Internet”
    10. Click into “Change Adapter Options”
    11. Right click the virtual network adapter and select “Properties” button
    12. select “TCP/IPv4” and click “Properties” button
    13. Click “Advanced…” button
    14. Clear the “Automatic metric” and enter “55” to the textbox of Interface Metric (entering other proper value in your case)
    15. Click “OK” button and until all property windows are closed.
    16. Repeat the same steps from (12) to (15) for “TCP/IPv6”
    17. You may run “Get-NetIPInterface” in PowerShell Window to make sure the values are applied.

DONE!
These are all the steps to fix this issue. I hope these steps could also help you.