Tuesday, August 30, 2011

Powershell Script to automate WINS install/configuration

Here's a  short powershell script I wrote that will automate the installation of WINS server and configure a two way replication partnership.


Import-Module Servermanager
Add-WindowsFeature WINS-Server
[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic') | Out-Null
$computer = [Microsoft.VisualBasic.Interaction]::InputBox("Enter Host of Replication Partner", "Computer", "$env:computername")
$myip=((ipconfig | findstr [0-9].\.)[0]).Split()[-1]

$partner=([System.Net.Dns]::GetHostaddresses("$computer".split('.')[0]))[0].ipaddresstostring
netsh wins server $myip add partner Server=$partner type=2
netsh wins server $partner add partner Server=$myip type=2

Vbscript to compare services among hosts inside domain

Below is a script I wrote to compare services on a list of hosts versus a "template" list of services and show the differences, very useful if your trying to find differences among roles/services running on a large number of hosts.


How to use:

Run inside domain or do runas and make sure your vpn

C:\Windows\System32\runas.exe /u:<domain>\<account> /netonly cmd

Cscript services.vbs

Cscript makeTemplate /computer:<Computer to make template services>

Template.txt = The list of services that is used to compare against
compareList.txt = Dump of Hosts to look against

services.vbs
----------------------------------------------------------------
Dim args,computername

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("template.txt", ForReading)

Const ForReading = 1

'Read template
Dim arrFileLinesTemplate()
i = 0
Do Until objFile.AtEndOfStream
Redim Preserve arrFileLines(i)
arrFileLines(i) = objFile.ReadLine
i = i + 1
Loop
objFile.Close

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile1 = objFSO.OpenTextFile("CompareList.txt", ForReading)


'Read Compare To List
Dim ListToProcess()
i = 0
Do Until objFile1.AtEndOfStream
Redim Preserve ListToProcess(i)
ListToProcess(i) = objFile1.ReadLine
i = i + 1
Loop
objFile1.Close

For Each strLine in ListToProcess

    if strLine <> "" then
    computername = strLine
    Call enumServices(strLine,1)
    End if

Next

Function enumServices(computername,flag)
On Error Resume Next
Dim objsvc,svccount,count, tempStr, found, strCompName
found = 1
count = 0
strCompName = computername
wscript.echo "==============Checking " & ucase(computername) & "=============="
set objsvc = GetObject("winmgmts:{impersonationLevel=impersonate}\\" & computername & "\root\cimv2").ExecQuery _
            ("SELECT * FROM Win32_Service")
if not (errorChecking (computername)) then
    for each svc in objsvc
        count = count + 1
        Call writetoLog(svc.displayname,svc.state,svc.startmode,svc.pathname,svc.processid,computername)
        if flag = 1 then
            tempStr = svc.displayname
            For Each strLine in arrFileLines
                intCompare = StrComp(strLine, tempStr, vbTextCompare)
               
                if intCompare = 0 then
                    'Wscript.Echo svc.displayname & " xxxx found in template xxxx"
                    found=0
                    exit For
                end if
            Next
            if found = 1 then
                Wscript.Echo svc.displayname & " hosted on " & strCompName & " was not found in template "
               

            end if
            found = 1
        end if
    next       
    wscript.echo "There are " & count & " Services on " & ucase(strCompName)
    wscript.echo "Disconnecting from " & ucase(strCompName)
    wscript.echo ""
end if
set objsvc = nothing
End Function



Function writetoLog(displayname,state,startmode,pathname,processid,computername)
Dim FSO,objFSOwriteline
Set FSO = CreateObject("Scripting.FileSystemObject")
Set objFSOwriteline = FSO.OpenTextFile(logPath & "\services_" & computername & ".txt", 8,True)
    objFSOwriteline.WriteLine(displayname)
    objFSOwriteline.close
Set objFSOwriteline = nothing
Set FSO = nothing
End Function

----------------------------------------------------------------

makeTemplate.vbs
----------------------------------------------------------------
Dim args,computername,adwg,logPath
Set args = Wscript.Arguments.Named
computername = args.Item("computer")
logPath = getLogPath()

if wscript.arguments.count = 0 then
    wscript.echo "Script Usage:"
    wscript.echo "Enum Service on Single PC: cscript makeTemplate.vbs /computer:[ComputerName]"
   
elseif args.exists("computer") then
    if computername = "" then
        computername = getComputer()
    else
        computername = computername
    end if
    Call enumServices(computername,1)

End if


Function enumServices(computername,flag)
On Error Resume Next
Dim objsvc,svccount,count
count = 0
wscript.echo "Connecting to " & ucase(computername) & " building template..... "
set objsvc = GetObject("winmgmts:{impersonationLevel=impersonate}\\" & computername & "\root\cimv2").ExecQuery _
            ("SELECT * FROM Win32_Service")
if not (errorChecking (computername)) then
    for each svc in objsvc
        count = count + 1
        Call writetoLog(svc.displayname,svc.state,svc.startmode,svc.pathname,svc.processid,computername)
        if flag = 1 then
            'wscript.echo svc.displayname & "," & svc.state & "," & svc.startmode
        end if
    next       
    wscript.echo "There are " & count & " Services on " & ucase(computername)
    wscript.echo "Disconnecting from " & ucase(computername)
    wscript.echo ""
end if
set objsvc = nothing
End Function


Function writetoLog(displayname,state,startmode,pathname,processid,computername)
Dim FSO,objFSOwriteline
Set FSO = CreateObject("Scripting.FileSystemObject")
Set objFSOwriteline = FSO.OpenTextFile("Template.txt", 8,True)
    objFSOwriteline.WriteLine(displayname)
    objFSOwriteline.close
Set objFSOwriteline = nothing
Set FSO = nothing
End Function

Function getLogPath()
Dim temp,temp2
temp = split(wscript.scriptfullname,"\")
for i = 0 to ubound(temp) - 1
    temp2 = temp2 & temp(i) & "\"
next
getLogPath = temp2
End Function

----------------------------------------------------------------