PowerShell for Windows Admins


January 6, 2015  2:56 AM

PowerShell Summit Europe 2015–topic submissions

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
Powershell

Topic submissions for the PowerShell Summit Europe are still open. If you want to be considered as a speaker please submit your topic very soon.

At the moment there aren’t enough submissions to enable us to put on a quality event. The 2014 European Summit was an excellent event with many good sessions – now is the time to submit your sessions. We need your sessions.

We have a policy of accepting sessions from new speakers as well as established experts. It’s not who you are but the quality of the session that counts.

Details on how to submit session proposals are available here

http://powershell.org/wp/2014/11/24/call-for-presentations-for-powershell-summit-europe-2015/

Please submit your proposals soon as we can’t run the European PowerShell Summit without them

January 5, 2015  10:33 AM

Accessing your PowerShellget repository from another machine

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
Powershell

So far the new PowerShellGet repository has been accessed from the local machine. You need to register the new repository on each machine from which you want to access it.

Register-PSRepository -Name PowerShellModules -SourceLocation http://W12R2DSC:81/nuget/PowerShellModules -InstallationPolicy Trusted

The only change to is substituting the name of the server for localhost in the source location URI.

NOTE: you will need to ensure that the Windows firewall is either off or allows the appropriate traffic through to the server hosting you repository

You can use the new repository

PS C:\Windows\system32> Get-PSRepository | ft Name, SourceLocation -AutoSize

Name              SourceLocation
—-              ————–
PSGallery         https://www.powershellgallery.com/api/v2/
MSPSGallery       http://search.microsoft.com/default.aspx
PowerShellModules http://w12r2dsc:81/nuget/PowerShellModules

And see the modules

PS C:\Windows\system32> Find-Module -Name Pester | ft -a

Version Name   Repository        Description
——- —-   ———-        ———–
3.2.0   Pester PSGallery         Pester provides a framework
3.2.0   Pester PowerShellModules Pester provides a framework

And install modules

PS C:\Windows\system32> Install-Module -Name pester -Repository PowerShellModules

PS C:\Windows\system32> Get-Module -ListAvailable pest* | ft -a
Directory: C:\Program Files\WindowsPowerShell\Modules
ModuleType Version Name   ExportedCommands
———- ——- —-   —————-
Script     3.2.0   Pester {Describe, Context, It, Should…}


January 5, 2015  5:01 AM

Working with multiple PowerShellGet repositories

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
Powershell

In http://blogs.msmvps.com/richardsiddaway/2015/01/04/creating-a-powershellget-repository/  I showed how to create a repository to use with PowerShellGet.  At the end of that article PowerShellGet would search all of the defined repositories for a module

£> Find-Module -Name Pester | Format-Table Version, Name, Repository -AutoSize

Version Name   Repository
——- —-   ———-
3.2.0   Pester PSGallery
3.2.0   Pester PowerShellModules

If you delete the Pester module and want to re-install it you need to specify the repository otherwise you get an error:

£> Install-Module -Name Pester -Verbose
VERBOSE: The -Repository parameter was not specified.  PowerShellGet will use all of the registered repositories.
VERBOSE: Getting the provider object for the OneGet Provider ‘NuGet’.
VERBOSE: The specified Location is ‘https://www.powershellgallery.com/api/v2/’ and OneGetProvider is ‘NuGet’.
VERBOSE: Getting the provider object for the OneGet Provider ‘NuGet’.
VERBOSE: The specified Location is ‘http://search.microsoft.com/default.aspx’ and OneGetProvider is ‘NuGet’.
VERBOSE: Getting the provider object for the OneGet Provider ‘NuGet’.
VERBOSE: The specified Location is ‘http://localhost:81/nuget/PowerShellModules’ and OneGetProvider is ‘NuGet’.
WARNING: ‘Pester’ matched module ‘Pester/3.2.0′ from provider: ‘PSModule’, repository
https://www.powershellgallery.com/api/v2/’
WARNING: ‘Pester’ matched module ‘Pester/3.2.0′ from provider: ‘PSModule’, repository
http://localhost:81/nuget/PowerShellModules’
OneGet\Install-Package : Unable to install, multiple modules matched ‘Pester’. Please specify a single -Repository.
At C:\Windows\system32\WindowsPowerShell\v1.0\Modules\PowerShellGet\PSGet.psm1:615 char:21
+             $null = OneGet\Install-Package @PSBoundParameters
+                     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidArgument: (Microsoft.Power….InstallPackage:InstallPackage) [Install-Package], E
xception
+ FullyQualifiedErrorId : DisambiguateForInstall,Microsoft.PowerShell.OneGet.CmdLets.InstallPackage

 

£> Install-Module -Name Pester -Repository PowerShellModules -Verbose
VERBOSE: Repository details, Name = ‘PowerShellModules’, Location = ‘http://localhost:81/nuget/PowerShellModules';
IsTrusted = ‘True'; IsRegistered = ‘True’.
VERBOSE: Repository details, Name = ‘PowerShellModules’, Location = ‘http://localhost:81/nuget/PowerShellModules';
IsTrusted = ‘True'; IsRegistered = ‘True’.
VERBOSE: Using the specified source names : ‘PowerShellModules’.
VERBOSE: Getting the provider object for the OneGet Provider ‘NuGet’.
VERBOSE: The specified Location is ‘http://localhost:81/nuget/PowerShellModules’ and OneGetProvider is ‘NuGet’.
VERBOSE: In PSModule Provider – ‘Get-InstalledPackage’.
VERBOSE: Performing the operation “Install-Module” on target “Version ‘3.2.0’ of module ‘Pester'”.
VERBOSE: The specified Location is ‘NuGet’ and OneGetProvider is ‘NuGet’.
VERBOSE: Downloading module ‘Pester’ with version ‘3.2.0’ from the repository
http://localhost:81/nuget/PowerShellModules’.
VERBOSE: NuGet: Installing ‘Pester 3.2.0′.
VERBOSE: NuGet: Successfully installed ‘Pester 3.2.0′.
VERBOSE: Module ‘Pester’ was installed successfully.


January 4, 2015  11:25 AM

Creating a PowerShellGet repository

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
Powershell

In this post – http://blogs.msmvps.com/richardsiddaway/2014/12/21/delivering-powershell-code-with-the-november-preview/ – I showed how to use PowerShellGet, which is a feature new to PowerShell 5.0 to install PowerShell modules. By default PowerShelget uses the PowerShell gallery at https://www.powershellgallery.com as its source.

I don’t feel comfortable installing modules direct from a public gallery so I want my own.  I suspect that many organizations will want to use their own module repository rather than something on the Internet over which they have no control.

There are a number of options for setting up your own gallery.

You could create an online nuget feed. This is described in this post from the PowerShell team:
http://blogs.msdn.com/b/powershell/archive/2014/05/20/setting-up-an-internal-powershellget-repository.aspx

You could create your own nuget repository – an example of doing this can be found here – http://learn-powershell.net/2014/04/11/setting-up-a-nuget-feed-for-use-with-oneget/

After reading http://asaconsultant.blogspot.no/2014/05/build-your-local-powershell- module.html I decided to start with ProGet (http://inedo.com/proget/pricing) as it seemed simple and was installed locally so I could take it with me to do demos.

To keep things even simpler I went with the free version and used the download that also  installs SQL Express. The installer also can create a service INEDOPROGETSVC to host the web service ProGet will use. Set the Service to use the LocalSystem account so that it can access the location to which the modules will be published.

The ProGet default feed is nuget but you want your own for this game.  Its not obvious how to do this but open Proget administration page – http://localhost:81/administration

I’m using port 81 as this is running on my DSC server.

Click manage feeds and you’ll find a button labelled Create New Feed. Press the button.

You have 3 choices

A nuget feed

A chocolately feed

a npm feed (private registry)

Lets go with nuget.

After supplying a name and description I went with the defaults to complete the feed creation.

At this point PowerShellget can’t see the new repository so you need to register it.

£> Register-PSRepository -Name PowerShellModules -SourceLocation http://localhost:81/nuget/PowerShellModules -Installati
onPolicy Trusted

And it now shows in my list of PowerShell repositories

£> Get-PSRepository | fl Name, SourceLocation

Name           : PSGallery
SourceLocation : https://www.powershellgallery.com/api/v2/

Name           : MSPSGallery
SourceLocation : http://search.microsoft.com/default.aspx

Name           : PowerShellModules
SourceLocation : http://localhost:81/nuget/PowerShellModules

You need to set a location to which modules will be published

Set-PSRepository -Name PowerShellModules -PublishLocation ‘http://localhost:81/nuget/PowerShellModules’

Now to try and publish a module.

 I’m going to use the Pester module I downloaded in the previous article:

£> Publish-Module -Name Pester -NuGetApiKey “Admin:Admin” -Repository PowerShellModules -Verbose
VERBOSE: Repository details, Name = ‘PowerShellModules’, Location = ‘http://localhost:81/nuget/PowerShellModules';
IsTrusted = ‘True'; IsRegistered = ‘True’.
VERBOSE: Repository details, Name = ‘PowerShellModules’, Location = ‘http://localhost:81/nuget/PowerShellModules';
IsTrusted = ‘True'; IsRegistered = ‘True’.
VERBOSE: Repository details, Name = ‘PowerShellModules’, Location = ‘http://localhost:81/nuget/PowerShellModules';
IsTrusted = ‘True'; IsRegistered = ‘True’.
VERBOSE: Module’Pester’ was found in ‘C:\Program Files\WindowsPowerShell\Modules\Pester’.
VERBOSE: Loading module from path ‘C:\Users\Richard\AppData\Local\Temp\409922302\Pester\Pester.psm1′.
VERBOSE: Repository details, Name = ‘PowerShellModules’, Location = ‘http://localhost:81/nuget/PowerShellModules';
IsTrusted = ‘True'; IsRegistered = ‘True’.
VERBOSE: Repository details, Name = ‘PowerShellModules’, Location = ‘http://localhost:81/nuget/PowerShellModules';
IsTrusted = ‘True'; IsRegistered = ‘True’.
VERBOSE: Using the specified source names : ‘PowerShellModules’.
VERBOSE: Getting the provider object for the OneGet Provider ‘NuGet’.
VERBOSE: The specified Location is ‘http://localhost:81/nuget/PowerShellModules’ and OneGetProvider is ‘NuGet’.
VERBOSE: Performing the operation “Publish-Module” on target “Version ‘3.2.0’ of module ‘Pester'”.
VERBOSE: Successfully published module ‘Pester’ to the module publish location
‘http://localhost:81/nuget/PowerShellModules’.

The -NuGetApiKey “Admin:Admin”  is the default and you’d want to change that for a production system.

Finally test the repository contents

£> Find-Module -Repository PowerShellModules

Version    Name                                Repository
——-    —-                                ———-
3.2.0      Pester                              PowerShellModules


January 1, 2015  5:25 AM

PowerShell in 2015

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
Powershell

See Don Jones’ predictions for PowerShell in 2015 – http://blogs.technet.com/b/heyscriptingguy/archive/2015/01/01/powershell-predictions-for-2015.aspx


December 31, 2014  8:51 AM

PowerShell Summit NA 2015

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
Powershell

We will be getting extra capacity for the PowerShell Summit NA 2015 in Charlotte. The extra places should become available 11 January 2015 – see http://powershell.org/wp/2014/12/30/powershell-summit-n-a-2015-additional-capacity/

for details.

Please check the powershell.org site for further news


December 31, 2014  8:42 AM

PowerShell review of 2014

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
Powershell

See my review of 2014 from a PowerShell perspective on the Scripting Guy blog – http://blogs.technet.com/b/heyscriptingguy/archive/2014/12/31/2014-a-powershell-year-in-perspective.aspx


December 30, 2014  1:14 PM

More on dates

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
Powershell

My recent post on date conversions seems to have generated a fair bit of confusion and discussion. The original task was to read in a string from a CSV file that contained a date in UK format – DD/MM/YYYY and reformat it to a string is US format – MM/DD/YYYY

As I showed you can’t use [datetime] type accelerator for this as it expects the date string to be in US format.

A number of people pointed out that you can use the –Date parameter on Get-Date

£> $strUK = ’25/12/2014′
£> Get-Date -Date $strUK

25 December 2014 00:00:00

Now if you want that output as a string in US format

£> Get-Date -Date $strUK -Format “MM/dd/yyyy”
12/25/2014

Use a variable to contain that data and you can work with it as you wish.

You need to be aware that using –Date on Get-Date is culture aware:

£> Get-Date -Date ’25/12/2014′

25 December 2014 00:00:00
£> Get-Date -Date ’12/25/2014′
Get-Date : Cannot bind parameter ‘Date’. Cannot convert value “12/25/2014″ to type “System.DateTime”. Error: “String was not recognized as a valid DateTime.”
At line:1 char:16
+ Get-Date -Date ’12/25/2014′
+                ~~~~~~~~~~~~
+ CategoryInfo          : InvalidArgument: (:) [Get-Date], ParameterBindingException
+ FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.GetDateCommand


December 30, 2014  9:10 AM

ANR and AD searches

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
Active Directory, Powershell

A comment on this post –  http://richardspowershellblog.wordpress.com/2014/12/29/using-givenname-and-surname-instead-of-samaccountname/

suggested using ANR – Ambiguous Name Resolution as a method of searching AD.

ANR provides a fuzzy search mechanism for AD – think wildcard search. If you perform an ANR search you’ll get anything matches – using your input as the root of the wildcard search – across display name, given name, name, samaccountname and surname.

Consider the searches shown last time based on the name Dave Green. Lets perform a ANR search on the first name

£> Get-ADUser -Filter {anr -eq ‘Dave’} | select Name

Name
—-
Jo Daven
Dave Green
Dave Brown
Dave White

Get-ADUser -LDAPFilter “(anr=Dave)” | select Name

will give the same result.  In my AD I get 3 results. Any account where any of the names listed above that start with the letters ‘Dave’  will be returned. Notice that in one of the results the letters are in the surname not the first name.

Similar issues if you perform ANR searches based on surname

£> Get-ADUser -LDAPFilter “(anr=Green)” | select Name

Name
—-
Dave Green
Fred Green
Dale Greensmith

or

Get-ADUser -Filter {anr -eq ‘Green’} | select Name

This time notice that the surname Greensmith is returned as well as Green.

You could use the whole name:

£> Get-ADUser -LDAPFilter “(anr=Dave Green)” | select Name

or

£> Get-ADUser -Filter {anr -eq ‘Dave Green’} | select Name

Name
—-
Dave Green
Dave Greenly

NOTE: I created the Dave Greenly account after the previous searches which is why it didn’t show earlier.

ANR searches are also slower than searching on specific attributes because a number of properties are being searched.

An ANR search is a good first step if  you’re not sure what you’re looking for but you will usually need to refine the search using –Identity parameter or more specific filters if you if you want to get to a single object.


December 29, 2014  5:50 AM

Using GivenName and Surname instead of samAccountName

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
Active Directory, Powershell

A recent comment on this post – https://richardspowershellblog.wordpress.com/wp-admin/post.php?post=2762&action=edit – asked about using the given name and surname rather than the samAccountName in Get-ADUser.

Get-ADUser has 4 options when using the –Identity parameter:

Account name = samAccountNmae

Distinguished Name

GUID

Security identifier = SID.

 

Using the given name and surname. on the surface doesn’t seem possible BUT (and there’s always a but with PowerShell) you can use the –Filter or –LDAPFilter parameters.

Where you know the samAcccountName you can do this:

Get-ADUser -Identity gdreen

Working with the names you could try this:

Get-ADUser -Filter {GivenName -eq ‘Dave’ -and Surname -eq ‘Green’}

 

Alternatively, you could use an LDAP filter:

Get-ADUser -LDAPFilter “(&(GivenName=Dave)(Sn=Green))”

The LDAP syntax is a bit more complex but you can parse it as

(GivenName=Dave) AND (Sn=Green)

You have to use the LDAP name, Sn, for the Surname property rather than the more friendly property name that the –Filter parameter allows.

 

If you wanted to modify the code in the original article to use this approach:

$users = Import-Csv -Path C:\Scripts\adtest.csv

foreach ($user in $users) {
$fname = $user.GivenName
$lname = $user.Surname

Get-ADUser -Filter {GivenName -eq $fname  -and Surname -eq $lname} -Properties * |
select SamAccountName, Division, Office, City
}

I’ve found that its easier to substitute variables into the filter rather than try and and use the object from the CSV file directly.

 

The LDAP filter version would be

$users = Import-Csv -Path C:\Scripts\adtest.csv

foreach ($user in $users) {

Get-ADUser -LDAPFilter “(&(GivenName=$($user.GivenName))(Sn=$($user.Surname)))” -Properties * |
select SamAccountName, Division, Office, City
}

In this case you’re substituting into a string and it works quite nicely.


Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to: