Category Archives: General

Use Windows Forms to generate GUI messagebox

 

a usefull post from: www.powershellmagazine.com credits for Jaap Brasser

While it is great that PowerShell can do so many things in the background without having to interact with it, occasionally it might be useful to have a graphical notification. For example, when a job finishes or when input is required in order for the script to be able to continue. For this purpose a popup window can be displayed; this is possible using the Windows.Forms namespace and in particular the MessageBox class.

The following example will display a MessageBox form with an OK button and the ‘Hello World’ text:

The MessageBox class can accept more arguments than just a single string. To explore the possible constructors the Show method can be called without any parameters displaying all OverloadDefinitions. In the following code the OverloadDefinitions and the total count of possible definitions will be displayed:

The output from the previous command shows that it is possible to change the MessageBoxButtons and that it is possible to add a MessageBoxIcon. The GetNames method of the Enum class can be called to enumerate the possible entries for these options:

The parameters for Title, Caption, MessageBoxButtons and MessageBoxIcon will be specified to create a new MessageBox. The output will be stored in the $MessageBox variable and can be used for further execution in the script:

For more information about the MessageBox class and to view all the definitions have a look at its MSDN entry:

MessageBox Class

Find files on all workstations with powershell

 

You can find files on all workstations with powershell with a short code.

First make a list off computernames and put them into a variable

Next use Invoke-Command to send the Get-ChildItem to those computers.

Notice that I create a new table entry for the computername in this line. It uses the computername environment variable:

Simplistic GUI for Powershell input

 

I ran into a simple example of how you can use out-gridview with the -OutputMode switch (like -Passthru). It creates a simplistic GUI for Powershell input.

  • This script displays all servers in your domain and lets you select multiple.
  • Then it provides you with a list of powershell scripts in c:\scripts and lets you choose one.
  • At last it will run the script on those servers and presents the output in a gridview.

All kudos for Mike Robbins

 

Use a Powershell Scriptblock

 

You can use a powershell scriptblok to execute a command on multiple computers.

If you use the invoke-command code you have to re-open the connection every time you execute a command to the remote computer. There are more ways to perform this task. This is just one of them.

This script uses credentials and some servers and pretents to stop a service. You can replace this however with every other command you choose naturally.

 

Create folders from CSV file with Powershell

 

Based on a CSV (comma seperated values) file you can populate a folder with subfolders from that CSV file. Just make a csv file with a new foldername on each line and save it. Use this script to create a subfolder from every line in the file.

 

Use Powershell to check securtity eventlog for logon

 

Use Powershell to report content from the eventlog. You can filter the type of log (security in this case) and even filter on the content of a log entry.

Use Powershell to check securtity eventlog for logon:

  • -Logname targets the logfile you wish to scan
  • -Newest only displays the latest x entries
  • -InstanceID represents the EventID. 4624 is an successfully logon

Check computers for services running as Administrator

 

This script will query all the computers in your domain for services configured to run as  Administrator, then outputs to Excel.
To use this script you must install the RSAT tools on Windows Vista or 7 and enable the “Active Directory Module for Windows PowerShell” feature.

Check computers for services running as Administrator:

 

How to check your powershell version

 

How to check your powershell version? That’s easy, just run the following command to check the powershell version of the computer you are working on.

Capture

You can run this command to check 1 or several computers:

Capture

Use Powershell to find text in files

 

If you need to use Powershell to find text in files in a folder structure you can use the Get-Childitem command to look for those files.

  • The -Recurse sends the command to all subfolders.
  • The Select-String defines the text to be found in the files.
  • The Select-Object defines the output to the screen.

Compare the content of two files

 

You can use Powershell to compare two items. These items can be two text files or, in this case, to registry files to see what has been changed. First make an export of the registry to file1.reg. Then make the change and export the registry to file2.reg. To compare those two files use the following command.

[code language=”powershell”]
Compare-object $(Get-Content ‘file1.reg’) $(Get-Content ‘file2.reg’)
[/code]

The Compare-Object does the matching. Remember that the code in between ( ) is executed first.

Be careful when using this command for it can be very memory consuming when comparing two complete registry files of a computer. Do not start this on a productive server without testing on a separate workstation!

Powershell Remoting

 

Running scripts on remote computers can be done in several different ways.

First the WinRM service needs to be started on the remote computers in order to manage them. In Windows 2012 WinRM is turned on by default. In a domain you can enable this for the rest of the computers via group policies:
computer configurationpoliciesadministrative templateswindows componentswindows remote ManagementWinRM Service
or on the computer itself:

Read more »

Get Powershell 4.0 from Microsoft

 

Windows Management Framework 4.0 includes updates to Windows PowerShell, Windows PowerShell ISE, Windows PowerShell Web Services (Management OData IIS Extension), Windows Remote Management (WinRM), Windows Management Instrumentation (WMI), the Server Manager WMI provider, and a new feature for 4.0, Windows PowerShell Desired State Configuration (DSC).

http://www.microsoft.com/en-us/download/details.aspx?id=40855