Tuesday, March 27, 2012

Get-Gateway

<#
 .SYNOPSIS
 get-gateway
 .DESCRIPTION
 This script pulls the gateway settings for computers listed in a CSV file.
 .EXAMPLE
 get-gateway.ps1 -pcs c:\bin\computers.csv
#>
[cmdletbinding()]

# Require path parameter to CSV file
param([Parameter(Mandatory=$true)]$pcs)
function get-gateway{
     #Create an empty array
     $results = @()
     [array] $computers = Import-Csv $pcs

     foreach ($i in $computers){
          $nics = Get-WmiObject -ComputerName $i.pcname Win32_NetworkAdapterConfiguration |
          Where-Object {$_.IPEnabled -eq "True" -and $_.DefaultIPGateway -ne $null}
          ForEach($nic in $nics){
               $results +=  $i.pcname + "<-->" + $nic.DefaultIPGateway}
               }

#Export results into a text file
Get-Date | Out-File -Append c:\bin\computergateway.txt
$results | Out-File -Append c:\bin\computergateway.txt
}
get-gateway

Monday, March 12, 2012

Move-Computer

# Here is a simple script that moves a list of computers from a CSV file.

# Create an array from the list of computers
$computers = Import-Csv C:\bin\todisable.csv

# Using a foreach loop to process and move the computers to the new location
# Make special note of the -name parameter!
$systems = foreach ($computer in $computers)
     { get-qadcomputer -name $computer.name }
          foreach ($i in $systems)

              { Move-QADObject $i.Guid -NewParentContainer 'ou=all,ou=DISABLED,dc=domain,dc=loc' }

Get-SID

# Here is a quick script for getting a user's SID.
# I borrowed the function and added some usability code
# Enjoy :-)

# User input to set the username variable
Write-Host "This script will identify the SID of a username"
$User = Read-Host "What username do you want converted?"


# The function to get the SID & output that value in yellow
function Name2SID($name, $domain=$env:userdomain) {
 $objUser = New-Object System.Security.Principal.NTAccount($domain,$name)
 $strSID = $objUser.Translate([System.Security.Principal.SecurityIdentifier])
 Write-Host "The SID for $User is: " -NoNewline; Write-Host $strSID.Value -ForegroundColor "Yellow"
}


# Call function with previously entered username parameter
Name2SID $User

Friday, March 9, 2012

Gotta Get-Module

# I found the following modules after watching a webcast from powershell.com:

# OutputHelper
# - Allows you to output via text, excel & the clipboard easily!

Get-Service | Out-TextReport -open

# ToolWindow
# - Allows you to open additional windows that are bound to your current window.  For example, you can # run the output of get-childitem c:\windows to one of these windows.

Get-childItem C:\Windows | Out-ListWindow

Load-Module From Zip Files

# In order to avoid the constant security warning when trying to import a module, make sure you  
# "Unblock" the zip file before unzipping it :)

import-module xzy

Thursday, March 8, 2012

Be Careful with Get-QADComputer

# I wrote a quick script that used Get-QADComputer.  Luckily I ran it to only move and not disable    
# computers.  If you had the following servers on your network:  Exchange, Exchange01 & Exchange02

Get-QADComputer Exchange

# Would return = Exchange, Exchange01 & Exchange02
# See the danger???

Get-QADComputer -name Exchange

# Would return only = Exchange

Tuesday, March 6, 2012

Local Account Management Module

Great blog post here about using the local account management module.  Great addition to Quest's snap-in.

Download the module here

Get-GPOData

#Function - Pulls GPO Settings and reports them via an HTML document
function Show-GPOReport {
  param(
  $GPOName = $null,
  $filename = "$env:temp\report.hta"
  )
  Import-Module GroupPolicy
  if ($GPOName -eq $null) {
    Get-GPO -All | Select-Object -ExpandProperty DisplayName
  } else {
    Get-GPOReport -Name $GPOName -ReportType Html | Out-File $filename
    Invoke-Item $filename
  }
}

#Query user for GPO "Name"
$param1 = read-host "Enter Desired GPO"
#Calls function with parameter.
Show-GPOReport $param1

Monday, March 5, 2012

Get-Logonscripts

# Add Quest Active Directory Snapin
Add-PSSnapin quest.ActiveRoles.admanagement
#Create an empty array
$results = @()
#Query all AD users & assign to an array
$allusers = get-qaduser
#Loop through array, if logon script is present, add it to the arrary
foreach ($i in $allusers)
 {
 if ($i.logonscript)
  {
  $results += $i | select name,logonscript
  }
 }

#Export results into a CSV file
$results | Export-Csv -NoTypeInformation c:\bin\logonscript.csv

First Posting - Powershell Set DNS

#Sets your local DNS settings to public DNS servers

$NICs = Get-WmiObject Win32_NetworkAdapterConfiguration | where{$_.IPEnabled -eq “TRUE”}
Foreach($NIC in $NICs) {
$DNSServers = “4.2.2.2","8.8.8.8"
$NIC.SetDNSServerSearchOrder($DNSServers)
}

#Sets your local DNS settings with no static IP addresses

$NICs = Get-WMIObject Win32_NetworkAdapterConfiguration | where{$_.IPEnabled -eq “TRUE”}
Foreach($NIC in $NICs) {
$NIC.SetDNSServerSearchOrder()
}