Thursday, 4 December 2008

Get-IPAddress2.ps1

# Get-IPAddress2.ps1
# Parses an address, returns a System.NET.IPAddress
if OK
# Uses tryparse (and therefore works with PSH v1)
# Thomas Lee
- tfl@psp.co.uk

$strings
= "10.1.1.100", "10.1", "fooey"

foreach( $string in $strings){
"Testing: `"{0}`"" -f $string
$address
= [System.Net.IPAddress]::tryparse($string,[ref] $ip1)
if ($address ){
"`"{0}`" `tis a valid IP address" -f [System.Net.IPAddress]::parse($string)
}
else {
"`"{0}`" `tis NOT a valid IP address" -f $string
}

}
This script produces the following output:

PS C:\foo> .\get-ipaddress2.ps1
Testing:
"10.1.1.100"
"10.1.1.100" is a valid IP address
Testing:
"10.1"
"10.0.0.1" is a valid IP address
Testing:
"fooey"
"fooey" is NOT a valid IP address

Wednesday, 3 December 2008

Get-IPAddress.ps1

#Requires -Version 2.0
#
Get-IPAddress.ps1
#
Parses an address, returns a System.NET.IPAddress if OK
#
Thomas Lee - tfl@psp.co.uk

$strings = "10.1.1.100", "10.1", "fooey"

foreach( $string in $strings){
try {
$address = [System.Net.IPAddress]::parse($string)
"`"{0}`" `tis a valid IP address" -f $address.IPaddressToString
}
catch {
"`"{0}`" `tis NOT a valid IP address" -f $string
}
}

This script produces the following output:

PS C:\foo> . \GetIPAddress.ps1
"10.1.1.100" is a valid IP address
"10.0.0.1" is a valid IP address
"fooey" is NOT a valid IP address




Tuesday, 2 December 2008

Get-PhysicalRam.ps1


# Get-PhysicalRAM.ps1
#
Sample using PowerShell
#
3rd sample from http://msdn.microsoft.com/en-us/library/aa394587
#
Thomas Lee

$mem = Get-WmiObject -Class Win32_ComputerSystem

# Display memory
"This system has {0} MB Free Memory" -f $($mem.TotalPhysicalMemory/1mb)
This script produces the following output:



PS C:\foo> .\Get-PhysicalRAM
This system has 8188.7421875 MB Free Memory


Monday, 1 December 2008

Get-DVD.ps1

# Get-DVD.ps1
#
Sample using PowerShell
#
2nd sample from http://msdn.microsoft.com/en-us/library/aa394587
#
Thomas Lee

$drives = Get-WmiObject -Class Win32_CDROMDrive

# Display CD info

$drives | Format-Table DeviceID, Description, Name

This script produces the following output:
PS C:\foo> .\get-DVD.ps1

DeviceID Description Name
-------- ----------- ----
IDE\CDROMTSSTCORP_DVD
+-RW_TS-H653B_______________D200____\5&1E5D3123&0&0.0.0 CD-ROM Drive TSSTcorp DVD+-RW TS-H653B ATA Device
IDE\CDROMTSSTCORP_DVD
-ROM_TS-H353B_______________D200____\5&3116E1C8&0&1.0.0 CD-ROM Drive TSSTcorp DVD-ROM TS-H353B ATA Device
SCSI\CDROM
&VEN_GF7300L&PROD_AZZ836K&REV_1.01\5&36E5972&1&000000 CD-ROM Drive GF7300L AZZ836K SCSI CdRom Device

Sunday, 30 November 2008

Get-FreeMemory.ps1

# Get-FreeMemory.ps1
#
Sample using PowerShell
#
1st sample from http://msdn.microsoft.com/en-us/library/aa394587
#
Thomas Lee

$mem = Get-WmiObject -Class Win32_OperatingSystem

# Display memory
"System : {0}" -f $mem.csname
"Free Memory: {0}" -f $mem.FreePhysicalMemory



This script produces the following output:



PS C:\foo> .\get-freememory.ps1
System : COOKHAM8
Free Memory: 2776988

Saturday, 29 November 2008

Add-DomainUserToLocalAdministatorsGroup.ps1

#Requires -Version 2.0
#
Add-DomainUserToLocalAdministatorsGroup.ps1
#
Add a domain user to a Local Administrator's group
#
Thomas Lee - tfl@psp.co.uk
#
Based on http://powershell.com/cs/media/p/380.aspx, with some error checking

# Setup up information on user to add,etc
$Domain = "Cookham" # where the user account comes from
$Computer = "Cookham8" # Computer to add the user to
$User = "tfl" # User

# Try to add user to Administrator's group
try {
$group.Add("WinNT://" + $Domain + "/" + $User)
"User {0}\{1} added to Administrator's local group in system: {2}" -f $domain,$user, $computer
}
catch {
"Error adding user to Administrator's group"
}
""
# Now print results
$computer = [ADSI]("WinNT://" + $Computer + ",computer")
$group = $computer.psbase.children.find("Administrators")

"Group: {0} contains {1} members:" -f $group.Name.Value, $members.Length
$members = $group.psbase.invoke("Members") | %{$_.GetType().InvokeMember("Name",'GetProperty',$null,$_,$null)}
$members
This script produces the following output (assuming no errors!):
PS C:\foo> .\Add-DomainUserToLocalAdministatorsGroup.ps1
User Cookham\tfl added to Administrator's local group in system: Cookham8

Group: Administrators contains 3 members:
Administrator
Domain Admins
tfl


Technorati Tags: ,,

Friday, 28 November 2008

Get-Forest.ps1

<#
.SYNOPSIS
    Shows use of GetCurrentForest to return information about the forest  
.DESCRIPTION
    This script uses .NET to return name of forest. 
.NOTES
    File Name  : Get-Forest.ps1
    Author     : Thomas Lee - tfl@psp.co.uk
    Requires   : PowerShell V2
.LINK
    http://pshscripts.blogspot.com/2008/11/get-forestps1.html
.EXAMPLE
    PS c:\foo> .\Get-Forest.ps1
    You are connected to the Cookham.NET forest
#>

##
#  Start of script
##

# Get Forest name
$forest = [System.DirectoryServices.ActiveDirectory.Forest]::GetCurrentForest() 

# Display info
"You are connected to the {0} forest" -f $forest.name

Thursday, 27 November 2008

Get-SQLServer.ps1

<#
.SYNOPSIS
This script uses SQL Server Server SMO objects to display
    server information.
.DESCRIPTION
This script first loads the SMO assembly and then creates a
    server object. The script then prints basic server information
    plus details of databases and
tables.
.NOTES
File Name : Get-SQLServer.ps1
Author : Thomas Lee - tfl@psp.co.uk
Requires : PowerShell Version 3.0, SQL Server 2012
.LINK
This script posted to:
http://pshscripts.blogspot.com/2010/05/get-sqlserverps1.html
.EXAMPLE
C:\Foo> .\Get-SQLServer.ps1
Server Details
--------------
Server Name: SQL2012
Product: Microsoft SQL Server
Edition: Enterprise Edition (64-bit)
Type: Standalone
Version: 11.0.3000
Version String: 11.0.2000.0
Service Account: NT AUTHORITY\NETWORKSERVICE

Databases and Table
-------------------
master (contains 6 tables)
model (contains 0 tables)
msdb (contains 102 tables)
PSMC (contains 1 tables)
ReportServer (contains 34 tables)
ReportServer (contain 13 tables)
tempdb (contains 0 tables)
#>

##
# Start of Script
##

# Load the SMO Assemblies - installed with the module!
# Note this generates a warning since SQL module uses non-standard verb!
Import-Module SQLPS

# Create Server Object
$server = New-Object Microsoft.SqlServer.Management.Smo.Server 'SQL2-12'

# Display key properties
'Server Details'
'--------------'
'Server Name: {0}' -f $Server.Netname
'Product: {0}' -f $Server.Product
'Edition: {0}' -f $Server.Edition
'Type: {0}' -f $Server.ServerType
'Version: {0}' -f $Server.Version
'Version String: {0}' -f $Server.Versionstring
'Service Account: {0}' -f $Server.ServiceAccount
''

# Display Database/Table info
'Databases and Table'
'-------------------'
Foreach ($database in $server.Databases) {
'{0} (contains {1} tables)' -f $Database.Name, $Database.Tables.Count
}

Wednesday, 26 November 2008

Get-LocalGroup.ps1


<#
.SYNOPSIS
Gets local group members from a computer.
.DESCRIPTION
This script uses Win32_Group to get the local groups
on a system then displays those groups.
.NOTES
File Name : Get-LocalGroup.ps1
Author : Thomas Lee - tfl@psp.co.uk
Requires : PowerShell V2
.LINK
PowerShell sample at:
http://pshscripts.blogspot.co.uk/2008/11/get-localgroupsps1.html
.EXAMPLE
C:\foo> .\Get-LocalGroup.PS1

Name Sid SidType Status
---- --- ------- ------
Administrators S-1-5-32-544 4 OK
Backup Operators S-1-5-32-551 4 OK
Certificate Service… S-1-5-32-574 4 OK
Cryptographic Opera… S-1-5-32-569 4 OK
Distributed COM Users S-1-5-32-562 4 OK
Event Log Readers S-1-5-32-573 4 OK
Guests S-1-5-32-546 4 OK
IIS_IUSRS S-1-5-32-568 4 OK
Network Configuration … S-1-5-32-556 4 OK
Performance Log Users S-1-5-32-559 4 OK
Performance Monitor Us… S-1-5-32-558 4 OK
Power Users S-1-5-32-547 4 OK
Print Operators S-1-5-32-550 4 OK
Remote Desktop Users S-1-5-32-555 4 OK
Replicator S-1-5-32-552 4 OK
Users S-1-5-32-5 4 OK
SQLServer2005MSFTE… S-1-5-21-23769… 4 OK
SQLServer2005MSSQLSe… S-1-5-21-23769… 4 OK
SQLServer2005MSSQLUs… S-1-5-21-23769… 4 OK
SQLServer2005MSSQLUs… S-1-5-21-23769… 4 OK
SQLServer2005SQLBro S-1-5-21-23769… 4 OK
#>

##
# Start of script
##

# Get Win32 Group Accounts
$Accts = Get-WMIObject Win32_Group | Where-object {$_.LocalAccount}


# Display them
$accts | Format-Table Name, Sid, SidType, Status –Autosize

# End of script

 

Tuesday, 25 November 2008

Get-Domain.ps1

<#
.SYNOPSIS
    Uses Win32_ComputerSystem to determine the domain this computer is in
.DESCRIPTION
    This script is a re-write of an MSDN sample (sample three
    from  http://msdn.microsoft.com/en-us/library/aa394586) that uses
    Win32_computer name to determine the domain.
.NOTES
    File Name  : Get-Domain.ps1
    Author     : Thomas Lee - tfl@psp.co.uk
    Requires   : PowerShell V2
.LINK
    Script posted to:
        Http://www.pshscripts.blogspot.com
    MSDN Sample at:
        http://msdn.microsoft.com/en-us/library/aa394586
.EXAMPLE
    PSH [C:\foo]: .\Get-Domain.PS1'
    System Name: COOKHAM8
    Domain     : cookham.net
#>

##
# Start of script
##

# Get details of this computer
$computer =  Get-WmiObject -Class Win32_ComputerSystem

# Display details
"System Name: {0}" -f $computer.name
"Domain     : {0}" -f $computer.domain
# End of script