Freigeben über


101 Uses for PowerShell

I’ve had a request to consolidate all of my little script examples into a single blog post.  While I’m not starting out with all 101 examples, I will continually update this post with scripts ideas and examples.  Enjoy.

Active Directory

Using Quest’s FREE PowerShell Commands For Active Directory(free download here)

Group Membership
Needed to get all domain accounts underneath my manager’s direct reports distribution list:

$grp = get-qadgroup “CN=<name of group>,OU=<ou name>,DC=<dc name>,DC=<dc name>,DC=<dc name>
$grp.Members |% {$usr = get-qaduser $_; “domain\” + $usr.SamAccountName;}

 

 

Event Log

Query Event Log
Pass some parameters to the event log and show results in a GridView.  This allows for filtered results, but also allows for additional sorting and filtering after the query executes.  This particular script gets any source like ‘*sharepoint*’ in the Application log and returns the last 20 results.

get-eventlog –logname Application –source ‘*sharepoint*’ –newest 20 | out-gridview

 

Files/Folders

File Merge
I used this little script to read a number of log files into a single file.  (i.e. Merge)

cd “d:\projects\output”

Get-ChildItem  *.log  |  Get-Content  >  “d:\projects\output\SingleLog.log”

 

Working with XML
Use this as a starter script to reading XML files down to a specific node

$itemContent = Get-Item –Path “c:\xmlfile.xml” | Get-Content

$xml = [xml]$itemContent

$xml.Node.AnotherNode.SubNode |? {$_.Attribute –eq “value”}

 

Network

Disable Loopback Check
Anyone that builds their own VMs knows the trouble this can cause.  Usually, the resulting error is an HTTP 401.

New-ItemProperty HKLM:\System\CurrentControlSet\Control\Lsa -name "DisableLoopbackCheck"  -value "1" -PropertyType dword

 

Change the default listening port of Remote Desktop
I often remote desktop into my computer at home through the internet.  This script will allow you to change the default listening port of Remote Desktop from the standard 3389 to a different port.  This may be helpful if you would like to remote into different computers or for added security.

Set-ItemProperty “HKLM:\System\CurrentControlSet\Control\Terminal Server\WinStations\RDP-Tcp”  -name "PortNumber"  -value "3390"

 

SharePoint

Feature Module Elements in XML
Automating the creation of the <File Url…. /> node under a module

<File Url=”SiteCollectionImages/logo.gif” Type=”GhostableInLibrary” />

cd “c:\prjs\Microsoft.SharePoint\TEMPLATE\FEATURES\BrandingFiles\SiteCollectionImages”

Get-ChildItem |
%{ WRITE-OUTPUT “<File Url=””$_”” Type=””GhostableInLibrary”” />” } > c:\files.txt

Show All Features Definitions in a GridView or CSV
Shows all features in your farm in a nice, easy to read gridview where you can perform additional filtering and searches.  (toggle the “Query” option to add specific filters)

$farmfeatures = [Microsoft.SharePoint.Administration.SPFarm]::Local.FeatureDefinitions

$farmfeatures | Out-GridView
$farmfeatures | Export-CSV  “c:\farmfeatures.csv”

 

WPF

Create a new Window and display a TreeView control
This script uses SharePoint API to get a list of sites and show them in a WPF TreeView control

windows.xaml

 

<Window
    xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml"
    Height="400" Width="500">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="200*" />
        </Grid.ColumnDefinitions>
                <TreeView Grid.Column="1" Name="treeview1" />
    </Grid>
</Window>

treeview.ps1

Add-Type -AssemblyName presentationframework

$dir = '.'
  # Create WPF Form from XAML file and bind Controls to variables
  $path = Resolve-Path $dir
  $Form=[Windows.Markup.XamlReader]::Load([IO.File]::OpenText('c:\users\ekraus\Desktop\window.xaml').basestream)
  $ListBox = $form.FindName('listBox')
  $RichTextBox = $form.FindName('richTextBox1')
  # Fill ListBox

  dir *.ps1 |% {
    $li = new-Object Windows.Controls.ListBoxItem
    $li.Content = $_.name
    $li.Tag= $_.fullname
    $listbox.Items.Add($LI)
  }
  # Add Handler to Read File
  $ListBox.add_MouseDoubleClick({
    $RichTextBox.SelectAll()
    $RichTextBox.Selection.Text = gc $this.Selecteditem.Tag
  })

  # Invoke Form
  $Form.ShowDialog() | out-null

Comments

  • Anonymous
    January 30, 2009
    PingBack from http://www.anith.com/?p=4046

  • Anonymous
    February 04, 2009
    I’ve posted this under my 101 Uses for PowerShell , but I thought it was so helpful that it deserved

  • Anonymous
    March 12, 2009
    I’ve had a request to consolidate all of my little script examples into a single blog post.&#160; While