A note on the scripts in this blog

Featured

When this Blog was migrated from Windows Live to WordPress some things did not translate well or at all. The most noticeable are “\”; that’s the “\” as in c:\ or in C:\Scripts\PS1. which just isn’t there anymore. They also put “the and sign” nbsp; in the place of spaces and tabs and `< and `> with the “and sign” gt and the “and sign” lt.

I don’t know why or how, but if you copy these scripts, you will need to check these very closely.

Thank you for your support.

Automates a SharePoint 2010 installation

###### Start Posh Script ######## 

<#
.Script Name
New-SPInstallation.ps1 

.SYNOPSIS
Automates a SharePoint 2010 installation.

.DESCRIPTION
The script automates a SharePoint 2010 installation.
Requires that the binaries are installed on the server.

.PARAMETER databaseName
Name of the configuration database.

.PARAMETER databaseServer
Name of the database server.

.PARAMETER centralAdminDatabase
Name of the Central Administration content database.

.PARAMETER port
Port to use.

.PARAMETER windowsAuthProvider
NTLM or Kerberos, default set to NTLM.

.PARAMETER userName
Farm Administrator account in the format ‘domain\username’.

.PARAMETER password
Pasword for the Farm Administrator account.

.PARAMETER passPhrase
Farm password, used to add new machines to the farm.
#>

param(
[string]$databaseName,
[string]$databaseServer,
[string]$centralAdminDatabase,
[string]$port,
[string]$windowsAuthProvider = "NTLM",
[string]$userName,
[string]$password,
[string]$passPhrase
)
Add-PSSnapin Microsoft.SharePoint.Powershell
$Host.Runspace.ThreadOptions = "ReuseThread"

# Converting password strings to secure strings
$securePassword = ConvertTo-SecureString -String $password -AsPlainText -Force
$securePassPhrase = ConvertTo-SecureString -String $passPhrase -AsPlainText -Force

# Creating a PSCredential object
$psCredentials = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $userName, $securePassword

# New Configuration Database
New-SPConfigurationDatabase -DatabaseName $databaseName -DatabaseServer $databaseServer -AdministrationContentDatabaseName $centralAdminDatabase -Passphrase $securePassPhrase -FarmCredentials $psCredentials

# Install help files
Install-SPHelpCollection -All

# Install services
Install-SPService

# Install Features
Install-SPFeature -AllExistingFeatures

# Create a new Central Administration
New-SPCentralAdministration -Port $port -WindowsAuthProvider $windowsAuthProvider

# Copy shared application data
Install-SPApplicationContent

###### End Posh Script ######## 

We can run this script by typing the following:

New-SPInstallation.ps1 -databaseName “My config DB” `
-databaseServer “SqlServer01″ `
-centralAdminDatabase “My Admin_ContentDB” -port 5057 `
-username “ps\admin” `
-password “SecretP@assword” -passphrase “JoinDoain”

Using Central ISE Snippet Repository

Using Central ISE Snippet Repository

In a previous tip we illustrated that ISE code snippets (press CTRL+J to view them) are plain ps1xml-files that you can manage in File Explorer. By default, the PowerShell 3.0 ISE editor loads snippets from your private cache folder.

Imagine you and your colleagues set up a network drive to share useful snippets. Instead of manually checking this folder and manually copying the snippets from this folder onto your local machine, you can also tell the ISE editor to load the snippets directly from the central repository. Here is a sample:

 

$snippetPath = Join-Path (Split-Path $profile.CurrentUserCurrentHost) "Snippets"

$snippetPath

Get-ChildItem -Path $snippetPath -Filter *.ps1xml |

ForEach-Object {

$psise.CurrentPowerShellTab.Snippets.Load($_.FullName)

This sample loads the snippets from your private local folder, so it shows what ISE does by default. Simply assign another path to $snippetPath to load the snippets from another folder. This could be your central team repository, and it could also be a USB stick you are carrying around with you that holds your favorite snippets.

As it turns out, this is the code executed behind the scenes by Import-IseSnippet. In fact, all *-IseSnippet commands are functions, and you can view the source code like this:

$path = "$pshome\modules\ise\ise.psm1"

ise $path

http://powershell.com/cs/blogs/tips/archive/2013/05/07/using-central-ise-snippet-repository.aspx

Accessing Latest Log File Entries

Sometimes you may just be interested in the last couple of entries in a log file. Here’s a simple yet fast way of outputting only the last x lines from a text log:
# Show last 5 lines of windowsupdate.log file

$logs = Get-Content -Path $env:windir\windowsupdate.log -ReadCount 0
$logs[-5..-1]

This example would return only the last (newest) five lines from the windowsupdate.log file.
It’s pretty fast because Get-Content uses –ReadCount 0, reading in even large text files very fast. The result is a text array with the text lines read in. In the example, only the last 5 lines are output (index -5 through -1). However, $logs will hold the complete log file content which may take considerable memory. So in PowerShell 3.0, there is a more efficient approach:
# Show last 5 lines of windowsupdate.log file

Get-Content -Path $env:windir\windowsupdate.log -ReadCount 0 -Tail 5

Here, only the number of lines specified with –Tail are returned, so there is no need for a text array to store all the other text lines found in the log file.

From: “Powershell Tip Of The Day”

http://powershell.com/cs/blogs/tips/archive/2013/04/26/accessing-latest-log-file-entries.aspx

powershell commands by noun

Here is a handy article on how to get a list of all the available Powershell commndlets on your machine in a nicely sorted list.

http://jdhitsolutions.com/blog/2013/04/friday-fun-powershell-commands-by-noun/?utm_source=feedburner&utm_medium=feed&utm_campaign=Feed%3A+JeffsScriptingBlogAndMore+%28The+Lonely+Administrator%29&utm_content=Google+Feedfetcher#utm_source=feed&utm_medium=feed&utm_campaign=feed?utm_source=rss&utm_medium=rss&utm_campaign=friday-fun-powershell-commands-by-noun

get-command -CommandType cmdlet | sort noun,verb | format-table -group noun

Finding Group members in AD

This is a simple script that will pull up the names of the members of a given group in AD. I use Excell to collect all the names.

 

$group = Read-Host "Enter group name to find"
$row = 2
$as = Get-ADGroup $group | Get-ADGroupMember

$xlSummaryAbove = 0 
$xlSortValues = $xlPinYin = 1 
$xlAscending = 1 
$xlDescending = 2
$xl = New-Object -comobject excel.application 
$xl.Visible = $true 
$wb = $xl.Workbooks.Add() 
$ws = $wb.Worksheets.Item(1) 
$row = 2
 $ws.Cells.Item(1,1) = 'First Name'
 $ws.Cells.Item(1,2) = 'Last Name'
 $ws.Cells.Item(1,3) = 'EMail Address'
 $ws.Cells.Item(1,4) = 'samAccountName'
 $range = $ws.range('A1:D1')
 $range.font.bold = 'true' 
foreach ($item in $as) { 
$name = Get-ADUser $item.samAccountName
 $ws.Cells.Item($row,1) = $name.GivenName
 $ws.Cells.Item($row,2) = $name.surname
 $ws.Cells.Item($row,3) = $item.name
 $ws.Cells.Item($row,4) = $name.samAccountName 
 $row++
  }

# one-column sort --> works 
$range1 = $ws.range('A2:D2500')
$range2 = $ws.range('B2')
[void]$range1.sort($range2, $xlAscending)
[void]$range1.entireColumn.Autofit()

Import Contacts for Lync 2010

My goal was to import a contact list for my Lync Client.
The idea was to set up a new user in our group with at least a start of a contact list for our team.

You can find a write up and the code for this here:
http://gallery.technet.microsoft.com/office/Import-Contacts-for-Lync-5fe0e671
You will need to install some stuff before you can use this.
Like the LynkSdkRedist.msi which is included in the zip file that you can download from the above site. .

I found that if you have multiple people with the same name, it will add them all if you do the import.

If you know the email address of the people you want to add to your contacts, you can just create a CSV file like this:

"GroupName","GroupType","Overwrite","Contacts"
"NSD","CustomGroup","True","sip:user1.mymail.com;sip:user2.mymail.com"

You can add whole distribution groups from the GAL as well.
To add a Distribution Group include this line:

"Dist Group Name from the GAL","DistributionGroup","N/A","DistributionGroup@mymail.com"

In this case my group name was NSD. Not sure what the “sip:” is all about, but you need it.
The “True” will cause it to overwrite any names or the whole group if it already exist.

Using tsclient to map drives

I am working on a project that involves moving a lot of files out of a lab environment into the production environment. The lab is behind a firewall that prevents production machines from mapping drives directly. I did find that my Windows 7 desktop can present its drives to the machines in the lab via the RDP client. The drives show up as a tsclient drive. It looks like this on the remote machine:

G on MyComputer

You need to set up your RDP connection:

clip_image002

And select More… under local devices and resources

clip_image004

And check drives. If you click the plus sign you can even select which drives you want use.

So, I found that I could drag and drop files from the E: drive on my server in the lab to the G drive on my laptop. Great, but I wanted to use robocopy for this. I found that if I did a net use y: \\tsclient\G it mapped the drive to y: .

My laptop has a drive mapped to my production server drive Z:

So I mapped X: to \\tsclient\Z

I could then Robocopy files from the Y: drive to X: with no problem.

I am sure the professional paranoids in security will have a fit about this, but in the meantime I can do my job.