Loading...

Follow Fredrik Wall | Not Just Another PowerShell Blog on Feedspot

Continue with Google
Continue with Facebook
or

Valid

When working with Microsoft Deployment Toolkit (MDT) you often want to add stuff to, change or read CustomSettings.ini in a way or another.

To change a value of a property in an ini file you can do this in many ways.
You can install a module, you can replace a whole string with another or you can search for a property and set a new value.

If you know the whole string like “TaskSequenceID=REF-RCPOC03” it’s easy to just replace it.
But If the value like “REF-RCPOC03” can be whatever then you need to search for the property and then split the string and just get the value.

And then replace the string with the property + “=” + value.

Something like:

# Get current value of a property
$currentValue = ((Select-String -Path "$($FilePath)\CustomSettings.ini" -pattern "$($property)\b" -AllMatches -ErrorAction Stop).ToString()).split("=")[1]

# Replace it
(Get-Content "$($FilePath)\CustomSettings.ini").replace("$($property)=$($currentValue)", "$($property)=$($Value)") | Set-Content "$($FilePath)\CustomSettings.ini"

The easiest way is to create functions for this to Add, Set and Get.

3 of my CustomSettings.ini functions can be found on my PowerShell GitHub repo.


Get-MDTCustomSettingsInformation
Set-MDTCustomSettingsInformation
Add-MDTCustomSettingsInformation

https://github.com/FredrikWall/PowerShell/tree/master/Microsoft%20Deployment%20Toolkit

  • Show original
  • .
  • Share
  • .
  • Favorite
  • .
  • Email
  • .
  • Add Tags 

In the second part of the Inventory tool series I showed how we set and find the information in the Active Directory with the Microsoft PowerShell Active Directory module.

In this part I will show the basics on how to build the Windows Forms GUI with PowerShell Studio.

If you want to test this you can download a trial version at Sapiens PowerShell Studio product page.

I use PowerShell Studio 2019 in my blog posts.
But you can use older versions too.

Start PowerShell Studio as administrator!

Create a New Form Project

Name the project and click Create.

And then just click Open in next dialog box.

Now we have an empty Forms project.
A PowerShell Studio Forms project contains several files.

The PowerShell Studio project file is named .psproj.
And a project contains the files Globals.ps1, MainForm.psf and Startup.pss by default.

When you open the project in PowerShell Studio you will see 3 tabs with the 3 files opened.
As below.

Globals.ps1

Here you can declare Global variables and functions.
When writing Windows Forms scripts in PowerShell it’s very important to set global variables and have lots of functions.

MainForm.psf

This tab is the main tab in a project.

And here you have 2 left tabs.

The Designer tab

It’s in the designer tab you add stuff to the form like buttons.

The script tab

When you add something in the designer tab like a button the code section will be added in the script tab like above.

This was the basic basic of PowerShell Studio.

In the next part I will start to show how to build the inventory application and how to work with the form.

  • Show original
  • .
  • Share
  • .
  • Favorite
  • .
  • Email
  • .
  • Add Tags 

It’s Christmas time and here in Sweden it’s time for holiday and lots of people will have some time off from their work.

I will be at work and I will be blogging and recording lots of stuff for this blog.

Next year I will continue to teach PowerShell fundamentals at Cornerstone
and I will continue to talk about PowerShell and spread PowerShell thru PowerShell User Group Sweden.

Best Regards,
Fredrik Wall

  • Show original
  • .
  • Share
  • .
  • Favorite
  • .
  • Email
  • .
  • Add Tags 

In this part I will show how to do this by using the Microsoft Active Directory module in an PowerShell console. The first part can be found here.

We need to start from the beginning.

First of all we need to start PowerShell as Administrator.
Then we need to see if we have the module and if it’s imported.

# List if ActiveDirectory module is available on the computer
Get-Module -ListAvailable ActiveDirectory

# Check if it's imported
Get-Module -Name ActiveDirectory

# Import it if you did not get any information above
Import-Module -Name ActiveDirectory

# List all commands from the Active Directory module
Get-Command -Module ActiveDirectory

What to do if we don’t have the Active Directory PowerShell module installed on our server/computer?

The Active Directory PowerShell module is located in the Remote Server Administration Tools (RSAT).

How to install or enable it can be found here.

Now we can start to explore our users.

# List all computers and display location and displayname properties
Get-ADComputer -filter * -properties location,displayname

# List all computers with something in location
Get-ADComputer -filter * -Properties location,displayname | where -Property location -ne $null

# Get 1 computer
Get-ADComputer -filter * -Properties location,displayname | Select-Object -Last 1

# Set location and displayname information
# Change UKLAP1008 to your computername
Get-ADComputer UKLAP1008 | Set-ADComputer -Location "1099" -DisplayName "frwa001"

# Check the added information
# Change UKLAP1008 to your computername
Get-ADComputer UKLAP1008 -Properties location,displayname

# List all computers with something in location
Get-ADComputer -filter * -Properties location,displayname | where -Property location -ne $null

To set location for all Laptops in England in my case we can do like this:

# List all Laptops in England
Get-ADComputer -filter * -Properties location,displayname,description | where { ($_.Description -eq "Laptops in England")

# Set location (Cost Center) to 0099 to all Laptops in England
Get-ADComputer -filter * -Properties location,displayname,description | where { ($_.Description -eq "Laptops in England") } | Set-ADComputer -Location "0099"

# List all computers with location information
Get-ADComputer -filter * -Properties location,displayname | where -Property location -ne $null

Now we are ready to start build our GUI with PowerShell Studio!
I will start to show this in the next blog post in this series.

  • Show original
  • .
  • Share
  • .
  • Favorite
  • .
  • Email
  • .
  • Add Tags 

I had an session about building an PowerShell GUI application in less then 40 minutes with Sapien PowerShell Studio and the Microsoft Active Directory module at the PowerShell User Group Sweden meeting on the 10:th of December.

This blog series will show how to build that application that I built then from start to finish plus lots of extra stuff.

Photo: Niklas Åkerlund


The short story of the application is that we have an Active Directory with computers and wan’t to store inventory/asset information for them.

As shown below in the finished application we want Cost Center and Owner for the computers.

I will start to show how to accomplish this in the PowerShell console and then go from there to create a nice PowerShell GUI application.

To be able to do this in my Lab environment I use my LabAD script to setup an lab AD structure with Users and Computers.

I will use displayname to store the owner and location to store the cost center.

In my next post I will show how to use the Microsoft Active Directory module for this.

  • Show original
  • .
  • Share
  • .
  • Favorite
  • .
  • Email
  • .
  • Add Tags 

I often show how to make Windows Forms applications with PowerShell Studio.

Now I’m starting to post some of my Windows Forms PowerShell Stuido projects to my PowerShell Gitub Repository.

First out will be my old Count Group Members.

It’s a simple form that will load a combobox with all groups in the Active Directory.
And display number of members.

I made the script the first time in 2012 when I got the question at a customer how many computers some application installer groups had.

This script has almost none error handling.
But shows how easy it is to make a GUI with PowerShell Studio.

I will post more scripts and some tutorials that shows how to use error handling too.

You can find more of my blog posts about PowerShell Studio here.

And you can find the source files for this project here.

  • Show original
  • .
  • Share
  • .
  • Favorite
  • .
  • Email
  • .
  • Add Tags 
Lab AD

It’s important to have a lab environment and to have a lab AD.
And It’s important to have a lab AD with users, computers, OUs, Groups etc.
Almost like your production AD.

The easy way is to have a PowerShell script that creates a OU structure with this.

When I talk about PowerShell or teaches PowerShell I often shows my lab environment and my PowerShell script.

It’s a script that uses a xml file for settings like how many users, Laptops, workstations, servers and countrys you want.

The script will also use files with Given and Surnames to create “real” users.

So in a couple of minutes you will have a couple of hundred fresh users, computers and groups.

Run your scripts or make changes in the structure.
Just delete the LabOU and run the script and It will be fresh.

And you can do this over and over again!

The scripts, settings file and name files can be found at my PowerShell Github repository.

  • Show original
  • .
  • Share
  • .
  • Favorite
  • .
  • Email
  • .
  • Add Tags 

A quick tip!

When using Update-Help use the parameter -Force.

If something went wrong the first time the help should update from the internet the help will look strange and it will not update okay with just Update-Help.

  • Show original
  • .
  • Share
  • .
  • Favorite
  • .
  • Email
  • .
  • Add Tags 

On the 15th of October we will have a PowerShell User Group Sweden meeting at Retune in Kista.

I will talk about creating QR codes with PowerShell.
A quick introduction to a PowerShell module. A quick introduction on modules and get modules from PSGallery.

And Björn Sundling will do a session on PowerShell and IOT.

Then we will do a session together on Scripting at companies.

The sessions will be at a basic level and with discussions!

The sessions will be held in Swedish!

Sign up here:
https://www.eventbrite.com/e/pugs-traff-oktober-2018-tickets-50501389014

  • Show original
  • .
  • Share
  • .
  • Favorite
  • .
  • Email
  • .
  • Add Tags 

Separate tags by commas
To access this feature, please upgrade your account.
Start your free month
Free Preview