SteveS832001 has asked for the wisdom of the Perl Monks concerning the following question:
package Services; require Exporter; use Win32::OLE qw( in ); use strict; use warnings; require DBD::mysql; my @ISA = qw(Exporter); =head1 NAME Services - Connects to Servers to get Services and Usernames =head1 SYNOPSIS use Services; Services::UpdateDB; =head1 DESCRIPTION This module provides a list of all services running on a Server/PC and + the User Account That it is running under. The code begins after the "cut" statement. =cut my @EXPORT = qw( UpdateDB ); sub TimeClock { my @months = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec); my @weekDays = qw(Sun Mon Tue Wed Thu Fri Sat Sun); my ($second, $minute, $hour, $dayOfMonth, $month, $yearOffset, $da +yOfWeek, $dayOfYear, $daylightSavings) = localtime(); my $year = 1900 + $yearOffset; my $theTime = "$hour:$minute:$second, $weekDays[$dayOfWeek] $month +s[$month] $dayOfMonth, $year"; return $theTime } sub Services::UpdateDB { my $Machine = shift; my $IP = shift; my $Logfile = shift || "Null"; my $DSN = 'driver={SQL Server};Server=10.10.0.31;database=Services +_Info;uid=Nope;pwd=WhatEver;'; my $ServiceQuery = "Select Service from dbo.Services"; my $Class = "CIM_Service"; my %Service_Info; my $DataHandle = DBI->connect("dbi:ODBC:$DSN") or die "$DBI::errs +tr\n"; my $StatementHandle = $DataHandle->prepare("SELECT * FROM dbo.Serv +ices where ServerName = '$Machine'"); $StatementHandle->execute(); while(my $RecordData = $StatementHandle->fetchrow_hashref) { $Service_Info{ $RecordData->{Service} } = $RecordData->{Server +Name}; } my $WMIServices = Win32::OLE->GetObject("winmgmts:\\\\$Machine\\ro +ot\\CIMV2"); my $Services = $WMIServices->ExecQuery("SELECT * FROM $Class"); foreach my $Service ( in($Services)) { if(defined $Service_Info{ $Service->{DisplayName} }) { $StatementHandle = $DataHandle->prepare("UPDATE dbo.Servic +es SET ServerName = '$Machine', IP_Address = '$IP', Username = '$Service->{StartName}', Service = '$Service->{DisplayName}' where ServerName = '$Machine' AND Service = '$S +ervice->{DisplayName}'"); $StatementHandle->execute(); undef $Service_Info{ $Service->{DisplayName} }; } else { if ($Service->{DisplayName} ne "") { if ($Service->{StartName} ne "") { $Service->{DisplayName} =~ s/'/ /g; $StatementHandle = $DataHandle->prepare("INSERT IN +TO dbo.Services (ServerName, IP_Address, Username, Service) VALUES ('$Machine', '$IP', '$Service->{StartNa +me}', '$Service->{DisplayName}')"); $StatementHandle->execute(); undef $Service_Info{ $Service->{DisplayName} }; } } } } } 1;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Problems with uninitialized value
by NetWallah (Canon) on Jan 05, 2008 at 00:10 UTC | |
|
Re: Problems with uninitialized value
by alexm (Chaplain) on Jan 05, 2008 at 00:10 UTC | |
|
Re: Problems with uninitialized value
by jwkrahn (Abbot) on Jan 04, 2008 at 23:17 UTC |