Monday, April 13, 2009
Saturday, March 7, 2009
import-csv
Monday, February 23, 2009
sever audit
$administratorsAccount = Get-WmiObject Win32_Group -filter "LocalAccount=True AND SID='S-1-5-32-544'"
$administratorQuery = "GroupComponent = `"Win32_Group.Domain='" + $administratorsAccount.Domain + "',NAME='" + $administratorsAccount.Name + "'`""
$user = Get-WmiObject Win32_GroupUser -filter $administratorQuery | select PartComponent |where {$_ -match $userToFind}
$user
local accts
Submitted by: Robert Cott
List Members of the Local Administrators Group
Submitted by: Anonymous Submission
Add Users to the Local Administrators Group or other local group
Submitted by Jimmy Godard
function Get-ScriptPath{ Split-Path $myInvocation.ScriptName
$b = & { $myInvocation.ScriptName }
$b
split-path info
# PowerShell's Split-Path can do this for you:
# parent path:
split-path c:\myproject\Csharpproject\memo.txt -Parent
# same as above, w/o the -Parent switch:
split-path c:\myproject\Csharpproject\memo.txt
# file name:
split-path c:\myproject\Csharpproject\memo.txt -Leaf
# ...and more:
split-path c:\myproject\Csharpproject\memo.txt -Qualifier
split-path c:\myproject\Csharpproject\memo.txt -NoQualifier
split-path c:\myproject\Csharpproject\memo.txt -IsAbsolute
Get the Data You Need
Day 7: Manage Users<
function Ping-Address {
PROCESS {
$ping = $false
$results = Get-WmiObject -query `
"SELECT * FROM Win32_PingStatus WHERE Address = '$_'"
foreach ($result in $results) {
if ($results.StatusCode -eq 0) {
$ping = $true
}
}
if ($ping -eq $true) {
Write-Output $_
}
}
}
function Restart-Computer {
PROCESS {
$computer = Get-WmiObject Win32_OperatingSystem -computer $_
$computer.Reboot()
}
}
Saturday, February 14, 2009
Wednesday, February 11, 2009
count events
BEGIN {
$errors = 0
$warnings = 0
$info = 0
}
PROCESS {
switch -wildcard ($_.entrytype) {
"err*" {$errors++}
"warn*" {$warnings++}
"info*" {$info++}
default {}
}#switch block
}#process block
END {
"The System log contains " + $errors + " error messages."
"The System log contains " + $warnings + " warning messages."
"The System log contains " + $info + " information messages."
}#end block
}#function block
#get-eventlog "System" -newest 100 CountEvents
======================= anothe approach ==============
$events = get-eventlog "System" -newest 100
function CountEvents{
$count = 0
foreach ($event in $args[0]) {
if ($event.entrytype -like $args[1]) {$count++}
}#foreach block
return $count
}#function block
$errors = CountEvents $events "err*"
$warnings = CountEvents $events "warn*"
$info = CountEvents $events "info*"
"The System log contains " + $errors + " error messages."
"The System log contains " + $warnings + " warning .messages."
"The System log contains " + $info + " information messages."
Tuesday, February 10, 2009
Pinging a Server from a Server you are not logged in to
So if you wanted server remote1 to ping server remote3 you could do something like this:
Get-WmiObject Win32_pingstatus –filter “Address =’remote3’” –computername remote1.| statuscode,address,_server
Wednesday, February 4, 2009
PowerShell Scriptomatic V1.0
create AD accts & exchange 2003 mail boxes links
Find all mailboxes with quota limit is NOT set to default. & more
Creating new AD user accounts from a csv-file data has become even easier
Manage Exchange Server 2003 Using Windows PowerShell and WMI
More About Using PowerShell to Manage Exchange Server 2003
PowerShell Tutorial 12 - Part 1: Active Directory Management
WMI PowerShell for Exchange 2003
How to write a LDAP search filter
Searching Active Directory with Windows PowerShell
Objective 2 - QAD Script to Change Passwords
http://mow001.blogspot.com/2005/12/g...able-from.htmlhttp://mow001.blogspot.com/2006/03/w...-part-one.htmlgr /\/\o\/\/
Prof. PowerShell
READ MORE: Prof. PowerShell
• Show What You Learned About PowerShell
Homework Assignment #1: Creating a formatted report of a selected scoped of peak paged memory.
by Jeffery Hicks
January 2009
• Give Me Time
PowerShell's DateTime object will leave you with time on your hands to do other things.
by Jeffery Hicks
January 2009
• Stringing Along
Use a PowerShell object's methods to do many of the same tasks for which you'd write VBscript code in long form.
by Jeffery Hicks
January 2009
• Get-Unique -- Same But Different
I still prefer the Select-Object cmdlet, but Get-Unique has a way of weeding out the dupes that's, well, different.
by Jeffery Hicks
December 2008
• Uniqueness Counts
PowerShell's Select-Object command has a -unique switch that lest you sort through the riffraff.
by Jeffery Hicks
December 2008
• Command Performance
The Get-Command cmdlet reveals all that you need to know about PowerShell.
by Jeffery Hicks
December 2008
• Reading Assignment
My secret for learning Powershell? Check out these blogs.
by Jeffery Hicks
November 2008
• Just DO It!
Lesson in Logic #3: The secret to getting your script block to execute at least one time is the DO loop.
by Jeffery Hicks
November 2008
• WHILE You Were Away...
Lesson in Logic #2: The WHILE statement keeps things rolling in your PowerShell scripts.
by Jeffery Hicks
November 2008
• Lessons in Logic
Lesson 1: Using an IF
Do You Read Me?
Even sans GUI, Powershell can be made to work interactively. The trick is the Read-Host cmdlet.
by Jeffery Hicks
October 2008
• PowerShell to XML
That nifty trick I showed you last time that exports data to CSV? This time, get more with an export to XML.
by Jeffery Hicks
October 2008
Sunday, January 25, 2009
rename computer
$ComputerInfo = Get-WmiObject -Class Win32_ComputerSystem
$ComputerInfo.rename($NewComputerName)
}
This script shows you how to rename computer by using WMI
How to use it :
& .\rename-computerName.ps1
rename-computername "MyNewComputerNAME"
And to reboot you can use WMI class Win32_OperatingSystem and its method reboot():
$OS = Get-WmiObject -Class Win32_OperatingSystem
$OS.reboot()
OR
shutdown -r -t 10 -f -c "Restart OS"
Users of PowerShell V2 (currently CTP3) can use the Restart-Computer cmdlet too. :)
DETAILED DESCRIPTION
The Restart-Computer cmdlet restarts the operating system on the local and remote computers.
You can use the parameters of Restart-Computer to run the restart operations as a background job, to specify the authentication levels and alternate credentials, to limit the operations that run concurrently, and to force an immediate restart.
This cmdlet does not require Windows PowerShell remoting unless you use the AsJob parameter.
Friday, January 23, 2009
Get-QADComputer Lastlogintime
also switched lastLogonTimeStamp with pwdLastSet
Re: Get-QADComputer Lastlogintime
Posted: Dec 16, 2008 7:29 AM in response to: alex.stefishen@...
Answered
Reply
Sure. :-)
$limit = (get-date).AddDays(-30).ToFileTime()
$filter = "(&(objectcategory=computer)(|(lastLogonTimestamp<=$limit)(!(lastLogonTimestamp=*))))" $inactivecomputers = Get-QADComputer -ldapFilter $filter -ip lastlogontimestamp -SizeLimit 0 $inactivecomputers | ft name,@{l="LastLogonTimeStamp";e={if($_.lastLogonTimestamp -ne $null){[DateTime]::FromFileTime([Int64]::Parse($_.lastLogonTimestamp))}} } -autosize
Tuesday, January 6, 2009
Saturday, January 3, 2009
Get-QADComputer Lastlogintime
$limit = (get-date).AddDays(-30).ToFileTime()
$filter = "(&(objectcategory=computer)(|(lastLogonTimestamp<=$limit)(!(lastLogonTimestamp=*))))" $inactivecomputers = Get-QADComputer -ldapFilter $filter -SizeLimit 0 It's faster than piping to where-object.
$limit = (get-date).AddDays(-30).ToFileTime()
$filter = "(&(objectcategory=computer)(|(lastLogonTimestamp<=$limit)(!(lastLogonTimestamp=*))))"
$inactivecomputers = Get-QADComputer -ldapFilter $filter -ip lastlogontimestamp -SizeLimit 0
$inactivecomputers | ft name,@{l="LastLogonTimeStamp";e={if($_.lastLogonTimestamp -ne $null){[DateTime]::FromFileTime([Int64]::Parse($_.lastLogonTimestamp))}} } -autosize
-aleksandar
http://powershellers.blogspot.com
Wednesday, December 24, 2008
ad groups -duplicate ad group structure in testdom
How to enumerate active directory groups using .Net Directory Services
Scripts to manage Active Directory Groups
get groups that are members of other groups
Set objGroup = GetObject _
("LDAP://CN=Finance Department,OU=Finance,DC=fabrikam,DC=com")
For Each strUser in objGroup.Member
Set objMember = GetObject("LDAP://" & strUser)
If objMember.Class <> "group" Then
objGroup.Remove(objMember.ADsPath)
End If
Next
Set objGroup = GetObject _
("LDAP://cn=Finance Managers, ou=Finance, dc=fabrikam, dc=com")
For Each strUser on objGroup.Member
Set objMember = GetObject("LDAP://" & strUser)
Wscript.Echo objMember.CN & ", " & objMember.Class
Next
Set objGroup = GetObject _
("LDAP://cn=Finance Managers, ou=Finance, dc=Fabrikam, dc=com")
Select Case objGroup.GroupType
Case 2
Wscript.Echo "This is a global distribution group."
Case 4
Wscript.Echo "This is a domain local distribution group."
Case 8
Wscript.Echo "This is a universal distribution group."
Case -2147483646
Wscript.Echo "This is a global security group."
Case -2147483644
Wscript.Echo "This is a domain local security group."
Case -2147483640
Wscript.Echo "This is a universal security group."
End Select
ctp3
?s
Discussions in microsoft.public.windows.powershell
changes
Changes of particular interest
PowerShell 2.0 CTP3 has arrived!
Monday, December 22, 2008
Effective PowerShell from http://keithhill.spaces.live.com/
Effective PowerShell Item 1: The Four Cmdlets That are the Keys to Finding Your Way Around PowerShell, Effective PowerShell,
Effective PowerShell Item 2: Use the Objects Luke. Use the Objects
Effective PowerShell Item 3: Know Your Output Formatters
Effective PowerShell Item 4: Commenting Out Lines in a Script File
Effective PowerShell Item 6: Know What Objects Are Flowing Down .the pipe
Effective PowerShell Item 7: Understanding "Output"
Effective PowerShell Item 8: Output Cardinality - Scalars .
Effective PowerShell Item 9: Regular Expressions
Effective PowerShell Item 10: Understanding PowerShell Parsing
Simple and effective PowerShell scripts for Exchange
The first one is the one-liner he wrote to help him out of the "Incredible Growing Logs" problem, which lists out the users connected to a particular mailbox store and which client they're using (Outlook versions or HTTP for OWA)
A one line script which checks the free disk space on every logical local drive on every computer in a specific OU:
The PowerShell that they use the most at the university is this script, which returns some mailbox details, including SMTP addresses, quota and usage for a user on Exchange 2003 or 2007:
The last Exchange-related PowerShell stuff is entitled "How much space are orphaned mailboxes taking up on my Exchange servers?" and does what it says on the tin.
array compare
$arrFirst = get-content First.txt
$arrSecond = get-content Second.txt
New-Item "C:\Myworkplace\ps\FirstInSecond.txt" -Type file
New-Item "C:\Myworkplace\ps\FirstNotInSecond" -Type file
Foreach ($First in $arrFirst)
{
If ($arrSecond -contains $First)
{Add-content "C:\Myworkplace\ps\FirstInsecond.txt" $First}
Else
{Add-content "C:\Myworkplace\ps\FirstNotInSecond.txt" $First}
===