Powershell
From Richard's Wiki
Revision as of 19:03, 20 March 2012 by Rkdrm (Talk | contribs) (→Getting a list of user accounts with passwords that don’t expire)
Contents
- 1 Mastering PowerShell eBook
- 2 Get Speed of Network Interfaces
- 3 Useful Scripts Links
- 4 GUI IDE
- 5 Recursive delete of files
- 6 Get disk free space on remote computer
- 7 Active Directory PowerShell Scripts
- 8 Periodically poll a website and log response times
- 9 Open Enterprise Architect Repository
- 10 Parse Enterprise Architect XML Files in a directory
- 11 List all software installed on local computer to Excel
- 12 Powershell V2
Mastering PowerShell eBook
http://powershell.com/cs/blogs/ebook/
Get Speed of Network Interfaces
[System.Net.NetworkInformation.NetworkInterface]::GetAllNetworkInterfaces()
Useful Scripts Links
GUI IDE
A useful Powershell GUI IDE (with debugger) is available at http://powergui.org/
Recursive delete of files
get-childitem . -include *scc,bin,obj -recurse | foreach ($_) {remove-item -recurse -force $_.fullname}
Get disk free space on remote computer
Get-WmiObject Win32_LogicalDisk -Filter "DriveType=3" -ComputerName <<ComputerName>>
Active Directory PowerShell Scripts
- Searching Active Directory with Windows PowerShell (TechNet), excellent
- Free PowerShell Commands for Active Directory Quest Software ActiveRoles Management Shell for Active Directory
Changing an attribute for a group of users (after Import-Module ActiveDirectory)
Get-ADUser -Filter * -SearchBase "OU=Accounting,OU=UserAccounts,DC=YourDomain,DC=com" | Set-ADUser -Manager "John Doe"
Copying one user’s group memberships to a second user (after Import-Module ActiveDirectory)
Get-ADPrincipalGroupMembership -Identity JohnDoe | % {Add-ADPrincipalGroupMembership -Identity JaneDoe -MemberOf $_}
Getting a list of user accounts with passwords that don’t expire (after Import-Module ActiveDirectory)
Search-ADAccount -PasswordNeverExpires | FT Name, ObjectClass, UserPrincipalName
Periodically poll a website and log response times
$url="http://xml.weather.yahoo.com/forecastrss?p=10036" $web = new-object system.net.webclient $datetime = new-object system.datetime $log = "log.txt" while (1) { $start = [DateTime]::Now $zz = $web.DownloadString($url) $end = [DateTime]::Now $duration = $end - $start "" + [DateTime]::Now + ": Duration " + $duration >> $log sleep 60 }
Open Enterprise Architect Repository
$rep = new-object -COM EA.Repository $rep.OpenFile("C:\eATEST.eap")
Parse Enterprise Architect XML Files in a directory
## An example script that will look at all the xml files ## in the specified directory, assume they are valid ## Enterprise Architect export files, and try to list ## the names of all the packages in each file param( $folderPath = $(throw 'folderPath must be supplied') ) $xmlFiles = (resolve-path "$folderPath\*.xml") foreach ($xmlFile in $xmlFiles) { $xmlFile = $xmlFile.Path $xmlDoc = new-object system.xml.xmldocument $xmlDoc.Load($xmlFile); $nsMgr = New-Object system.xml.XmlNamespaceManager $xmlDoc.get_NameTable() $nsmgr.AddNamespace("UML", "omg.org/UML1.3") if ($xmlDoc.XMI) { $packages = $xmlDoc.XMI.SelectNodes("//UML:Package", $nsMgr) if ($packages.Count -gt 0) { Write-Host $xmlFile ":" foreach ($package in $packages) { Write-Host $package.name } } } }
List all software installed on local computer to Excel
$a = New-Object -comobject Excel.Application $a.visible = $True $b = $a.Workbooks.Add() $c = $b.Worksheets.Item(1) $c.Cells.Item(1,1) = "Name" $c.Cells.Item(1,2) = "Version" $c.Cells.Item(1,3) = "Publisher" $c.Cells.Item(1,4) = "InstalledOn" $c.Cells.Item(1,5) = "HelpLink" $c.Cells.Item(1,6) = "UninstallString" $d = $c.UsedRange $d.Interior.ColorIndex = 19 $d.Font.ColorIndex = 11 $d.Font.Bold = $True $intRow = 2 $Keys = Get-ChildItem HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall $Items = $keys |foreach-object {Get-ItemProperty $_.PsPath} foreach ($item in $items) { $c.Cells.Item($intRow,1) = $item.Displayname $c.Cells.Item($intRow,2) = $item.DisplayVersion $c.Cells.Item($intRow,3) = $item.Publisher $c.Cells.Item($intRow,4) = $item.InstallDate $c.Cells.Item($intRow,5) = $item.HelpLink $c.Cells.Item($intRow,6) = $item.UninstallString $intRow = $intRow + 1 } $d.EntireColumn.AutoFit()