Thursday, October 27, 2011

Finding all file shares on the network

I adapted this script from Michel Steveman's post on printer inventory.

#Purpose: get all file shares and output to Excel

#Create array of servers to retrieve information from by reading masterservers.txt. Could also plug in an AD query here.
$servers= get-content masterservers.txt

#create new Excel WorkBook
$Excel = new-Object -comobject Excel.Application
$Excel.visible = $True
$Excel = $Excel.Workbooks.Add()
$Sheet = $Excel.Worksheets.Item(1)
$Sheet.Cells.Item(1,1) = "Server Name"
$Sheet.Cells.Item(1,2) = "Share Name"
$Sheet.Cells.Item(1,3) = "Share Path"
$intRow = 2
$WorkBook = $Sheet.UsedRange
$WorkBook.Font.Bold = $True

#Get File Share Information and write to Excel
ForEach ($server in $servers)
{
$Shares = Get-WmiObject -Class win32_share -ComputerName $server | where-object {$_.type -eq 0}
foreach ($Share in $Shares)
{
$Sheet.Cells.Item($intRow, 1) = $Server
$Sheet.Cells.Item($intRow, 2) = $Share.Name
$Sheet.Cells.Item($intRow, 3) = $Share.Path
$intRow = $intRow + 1
}
}

$WorkBook.EntireColumn.AutoFit()
$intRow = $intRow + 1
$Sheet.Cells.Item($intRow,1).Font.Bold = $True

Wednesday, October 26, 2011

PowerShell - Output File Share Info to Excel

Need to audit file shares on your network? Here's a quick and easy PowerShell script that will gather all the share names and paths. Note that you need to run this from a computer that has Microsoft Excel installed.

masterservers.txt is a file containing all the servers you want to run the script against. As always, you could also feed an AD query into the $servers array instead of pulling from a file.

#get all file shares and output to Excel

#Set Server Name
$servers= get-content masterservers.txt

#create new Excel WorkBook
$Excel = new-Object -comobject Excel.Application
$Excel.visible = $True
$Excel = $Excel.Workbooks.Add()
$Sheet = $Excel.Worksheets.Item(1)
$Sheet.Cells.Item(1,1) = "Server Name"
$Sheet.Cells.Item(1,2) = "Share Name"
$Sheet.Cells.Item(1,3) = "Share Path"
$intRow = 2
$WorkBook = $Sheet.UsedRange
$WorkBook.Font.Bold = $True

#Get File Share Information
ForEach ($server in $servers)
{
$Shares = Get-WmiObject -Class win32_share -ComputerName $server | where-object {$_.type -eq 0}
foreach ($Share in $Shares)
{
$Sheet.Cells.Item($intRow, 1) = $Server
$Sheet.Cells.Item($intRow, 2) = $Share.Name
$Sheet.Cells.Item($intRow, 3) = $Share.Path
$intRow = $intRow + 1
}
}

$WorkBook.EntireColumn.AutoFit()
$intRow = $intRow + 1
$Sheet.Cells.Item($intRow,1).Font.Bold = $True

Thursday, October 20, 2011

Powershell for Server Inventory

Here are a few snippets I have built to pull inventory information

To gather information where each request will only have one result per machine (doesn't work for things like disk or processor where there can be multiple values for one machine)

#computers.txt should be a list of computers to gather information from. You could also build an AD query instead if you want.
get-Content computers.txt | foreach {
$os=Get-WmiObject -class Win32_OperatingSystem -ComputerName $_
$sys=Get-WmiObject -Class win32_Computersystem -ComputerName $_
$bios=Get-WmiObject -Class win32_Bios -ComputerName $_
$net=Get-WmiObject -Class win32_networkadapterconfiguration -ComputerName $_
$disk=Get-WmiObject -Class win32_logicaldisk
$IP=[System.Net.Dns]::GetHostAddresses($_)| select-object ipaddresstostring

#create a new object so that we can output our data to CSV neatly
#This script returns computer name, OS, memory, vendor, model, domai, serial number, and service tag
new-Object psobject -Property @{
Computername=$_.toUpper()
OperatingSystem=$os.Caption
TotalPhysMemMB=($sys.TotalPhysicalMemory)/1MB -as [int]
Vendor=$sys.Manufacturer
Model=$sys.model
Domain=$sys.domainget
SerialNumber=$os.serialnumber
ServiceTag=$bios.SerialNumber
}| convertto-csv -notypeinfo -outvariable OutData
$outData[1..($outdata.count -1)]|foreach-object {add-content -value $_ -Path OSInfo.csv}
}

This one checks the ping status of each machine in a list of machines. If it is able to ping, it writes that value to a CSV file. If you took out the where-object, you could just have it return the server name and the ping status value instead.

get-content tcmis.txt | ForEach{Get-WmiObject win32_pingstatus -Filter "Address='$_'" | Where-object {$_.statuscode -eq 0} |select-object address, statuscode} | export-csv -notypeinfo exist.csv

Once you have your list of machines that exist, this script can get all the hard drive freespace and size information. It only pulls from non-removable drives (drive type 3)

#Exist.CSV contains a list of computer names. Find all non-removable drive types and export the information to a csv file

Get-content exist.csv | Foreach {Get-WmiObject -Class win32_logicaldisk -ComputerName $_ | where-object {$_.drivetype -eq 3} | select-object systemname, deviceID, FreeSpace, Size}| export-csv diskinfo.csv -notypeinfo

Count Number of Messages sent to Distribution Group

I created this script to tell me how many messages had been sent to each of a list of distribution groups in the last 30 days. It's really useful if you are trying to find groups that aren't used often for cleanup purposes.

#Get all transport servers so we can search across all of them
$hubs=get-TransportServer

#where test.txt contains a list of email addresses
$groups=get-content "C:\MessageTrackingRobin\test.txt"

#For each email address in test.txt, count how many messages were sent to it between the start and end dates, then output that information into count.txt in comma delimited format
foreach ($group in $groups){
$count = ($hubs| get-messagetrackinglog -start "09/19/2011 12:00AM" -End "10/20/2011 11:59PM" -recipients $group)
$output=$group + "," + $count.count
add-content count.txt $output
}

Changing SIP address on multiple users

There are many reasons to change multiple users' SIP domain.  Maybe you acquired a company and are bringing them online with their existing domain name.  Maybe a branch office is planning to do business under a new domain.

Adding a new SIP domain is simple enough in the Topology Builder, and when creating new users it is easy to select the new SIP domain, but when it's existing users converting to the new domain, it’s a lot of clicking in the Lync Control Panel, so I found a nice easy way to do this.

In this example, I have two SIP domain's, contoso.com, and Contoso's new doing business as SIP domain of nwtraders.com.   The entire Boston office is switching over to this, so I am able to select based on Registrar pool, but you can also filter by OU, or any other attributes the users share.

$userscope = Get-CsUser | where { $_.RegistrarPool -eq "boston.contoso.com" }
foreach ($item in $userscope)
    {
        $oldAddress = $item.SipAddress
        $newAddress = $oldAddress -replace "@contoso.com", "@nwtraders.com"
        Set-CsUser -Identity $item.Identity -SipAddress $newAddress
    }