Posts Tagged ‘PowerShell’

Get MAC Address from IP Address

Saturday, March 18th, 2017

I got a call last week from a member of one the other teams where I work.  He asked, “Do you have a script which will resolve a list of IP Addresses to MAC Addresses?” My answer was, “not yet”.  I did a search and found some very convoluted Pinvoke code. I wanted something easier.

When I automate a task, I begin with the manual steps for the task. To get a MAC address from an IP address, I ping the address, then look at the ARP cache. Get-MACFromIP.ps1 does the same thing,  using the inline script method to make the process run in parallel for speed.  It does not require any administrative rights to run, and is an advanced function.  A use example follows, others are in the code help:

I the use WMI ping method to enable name resolution and the return codes.  The script outputs the IP address, DNS Name (if it can be resolved), MAC address, and the verbose level ping reply.  Capture of the output of the ARP table is based on this post, by Joe Keohan.

Script Text

Open the PowerShell ISE (and other Programs) with Alternate Credentials

Tuesday, March 14th, 2017

RunAS for PowerShell is pretty easy. This opens the ISE:

 

Update GPOs with Newer Version

Wednesday, March 1st, 2017

If you use GPOs to enforce baselines, you may find that your enterprise is moving from version 1.1 to version 1.2 of a GPO.  Unfortunately for you, version 1.1 linked in a dozen places.  Wouldn’t you rather just you search for version 1.1 and replace it with version 1.2?  Use Update-GPOLinks.ps1 to do just that.  The script not only finds all the original links and updates them to the new version, it also keeps the link order.

Script Text

Get All GPOs Linked to an OU

Wednesday, March 1st, 2017

Get-AllGPOsLinkedToOU.ps1 returns a unique list of all GPO’s linked to an OU. You can also run a onelevel or subtree search to get a unique list of linked OUs at or below the selected OU. You are prompted for the domain, and navigate to desired OU.
Script Text

Reset GPO Cache

Wednesday, March 1st, 2017

This script deletes the locally stored copies of GPOs and forces a GPUPdate on a computer. Reset-GPOCache.ps1 works by a remote connection to the registry provider to get the path to the Group Policy\History folder, then deletes the files beneath that path. This ensures a fresh application of group polices.

Script Text

Get Resultant Set of Polices (RSOP) with User Selection

Wednesday, March 1st, 2017

The Get-GPResultantSetOfPolicy cmdlet in the GroupPolicy module of PowerShell has a parameter for a user name.  Often I have no idea who has logged onto the computer.  Get-RSOP.ps1 uses WMI to give you a pick list of users on the remote computer and then passes that to the user parameter of Get-GPResultantSetOfPolicy.

Script Text

Remove Active Directory Delegations

Saturday, February 25th, 2017

Over time, Active Directory delegations tend to accumulate and drift from the standards in the enterprise.  Removing the delegations for a user or group can be slow, especially if you do it manually.  Microsoft has a good article about this process, but none of the methods I found did what I needed.  I wanted a script which could look at all or selected OUs in AD for a delegation, and then delete them all.

Remove-DelegatedOUPermissions.ps1 is an advanced function which can be used to report and remove assigned delegated permissions from OU objects and containers.  You can choose the domain and searchbase, and you can search for full name or partial matches.  For example, if you wanted to report on or delete the delegations for Site1PWAdmins and Site2PWAdmins, you could simply specify “PWAdmins”.  The search is case-insensitive, and you can search for more than one string by separating your search terms with a comma.

This function always creates a log file.  The default name is derived from the domain name, and the default location is the desktop.  The function requires the ActiveDirectory module, but unlike Set-ACL, it can be used to write permissions in another domain.  It supports WhatIf, and a confirmation is required before you commit changes.  Because it is an advanced function, you can use Get-Help for details about use.

Script Text

A Dot Source Reminder for Advanced Functions

Saturday, February 25th, 2017

One of the problems with writing advanced functions is that new PowerShell users think that they don’t do anything.  Frankly, I couldn’t figure out a way to get a notification to work, so I reached out to the sponsor for the Charlotte PowerShell User Group, Microsoft PFE Brian Wilhite. Brian sent me some code  which I incorporated into the following snippet:

If you expect to have the end user save the function with code calling it at the bottom of the script, try this version which won’t prompt if there is additional text at the bottom:

Put either bit of code at the bottom of any advanced function. If the script is run inside the ISE, you will get something like this:

If you run it inside the PowerShell console you will see:

If you use the shell menu option to Run with PowerShell, it adds a pause so the script does not close.  Note that the colors are not rendered properly in the example text  — the first line has green text, subsequent lines are default.

Adding Terminal Services Information to User Reports

Saturday, February 4th, 2017

Add-ADTSInfo.ps1 adds TerminalServicesHomeDrive, TerminalServicesHomeDirectory, TerminalServicesProfilePath and AllowLogon as additional members returned by a query of Active Directory user objects.  As you may know, when looking at a user’s properties in the Active Directory Users and Computers MMC there is a tab for these fields.  However, if you look at the properties of a user object, these items simply aren’t there.  There are a few articles and scripts addressing this problem, and you will find that the only way to get the data is by binding to each individual user object and using the a method like this: $ADSIUser.psbase.InvokeGet(‘TerminalServicesProfilePath’).

My script differs from others in that you can pipe an object containing user objects with any properties, and it will add the fields listed above to your results.  I added sorting of the new resulting so that the property names are in order.  This is an advanced function with comment based help.

Script Text

Combine CSV Files to XLSX File with Worksheets

Saturday, February 4th, 2017

Three years ago, I posted CombineXLSheets.vbs, a vbScript which allows you to drop and drag Excel spreadsheets onto it and have them combined into a single workbook file with multiple worksheets.  Now that I am working in PowerShell, I have found the need to consolidate CSV files.  Parts of  Combine-CSVFilesToWorksheets.ps1 come from code ported from the vbscript, and this script does a similar job.

I have been experimenting with input validation choices, and you can see some of it here.  One of the things I learned is that a single parameter can have more than one script validations using ValidateScript.  I also decide that the error handling for this is pretty ugly, and moved other input validation into the body of the script.

The script requires that you specify the folder with the CSV files, and the output file for the combined data.  By default the worksheets are named with the base name of the CSV file, and autofit is applied to the columns.  You may optionally turn off autofit, and can delete all of the CSV files when done or the CSV folder itself.  Because this is an advanced function, you can use Get-Help for full help.  The script also supports ShouldProcess (-WhatIf ).

This script requires Excel.

Script Text