Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

incorrect data displayed 1

Status
Not open for further replies.

w33mhz

MIS
May 22, 2007
529
US
Code:
$objDomain = New-Object System.DirectoryServices.DirectoryEntry("LDAP://OU=Servers,dc=<doman>,dc=<local>")
$objSearcher = New-Object System.DirectoryServices.DirectorySearcher
$strFilter = "(objectCategory=Computer)"
$objSearcher.SearchRoot = $objDomain
$objSearcher.PageSize = 1000
$objSearcher.Filter = $strFilter
$objSearcher.SearchScope = "Subtree"
$colResults = $objSearcher.FindAll()
foreach ($objResult in $colResults)
{
$servername = $objResult.Properties
$servername = "TMCAPP04"
$schedule = new-object -com("Schedule.Service")
$schedule.connect($servername.name)
$tasks = $schedule.getfolder("\").gettasks(0)
$tasks |select name, lasttaskresult, lastruntime, nextruntime
}

This does not display expected data, it looks like it is showing a CLSID of something with the dates being wrong as well as the lastresult data. Works fine with the following. I am a powershell newbie, but I have cut my teeth with vbscript. I am just trying to learn powershell.
Code:
$servername = "<servername>"
$schedule = new-object -com("Schedule.Service")
$schedule.connect($servername)
$tasks = $schedule.getfolder("\").gettasks(0)
$tasks |select name, lasttaskresult, lastruntime, nextruntime

Windows Haiku:

Serious error.
All shortcuts have disappeared.
Screen. Mind. Both are blank.
 
i dont get the purpose of:

$servername = $objResult.Properties
$servername = "TMCAPP04"
$schedule = new-object -com("Schedule.Service")
$schedule.connect($servername.name)

you start off with assigning $servername what appears to be a collection .Properties, then you assign $servername = a string (being the name of a server i guess?) then you use $servername.name in the $schedule.connect method.

so, by the time you get to $schedule.connect your $servername variable isnt an object with a property of .name?

i hope i am not mistaken, otherwise powershell makes no sense?
 
sorry that was testing I forgot to take out, I had that commented out.
Code:
$objDomain = New-Object System.DirectoryServices.DirectoryEntry("LDAP://OU=Servers,dc=<domain>,dc=<local>")
$objSearcher = New-Object System.DirectoryServices.DirectorySearcher
$strFilter = "(objectCategory=Computer)"
$objSearcher.SearchRoot = $objDomain
$objSearcher.PageSize = 1000
$objSearcher.Filter = $strFilter
$objSearcher.SearchScope = "Subtree"
$colResults = $objSearcher.FindAll()
foreach ($objResult in $colResults)
{
$servername = $objResult.Properties
$schedule = new-object -com("Schedule.Service")
$schedule.connect($servername.name)
$tasks = $schedule.getfolder("\").gettasks(0)
$tasks |select name, lasttaskresult, lastruntime, nextruntime
}

Windows Haiku:

Serious error.
All shortcuts have disappeared.
Screen. Mind. Both are blank.
 
Well looks like i am in error yet again. Sorry for the confusion.
This version I get an error for each object I try
Exception calling "GetFolder" with "1" argument(s): "This operation is supported only when you are connected to the server. (Exception from HRESULT: 0x800704E3)

So that tells me an issue with making the connection. So when I have it just echo the $servername.name to the screen it passes values I am expecting to see (the machinenames).

Windows Haiku:

Serious error.
All shortcuts have disappeared.
Screen. Mind. Both are blank.
 
when you get it to run successfully with

$servername = "<servername>"
$schedule = new-object -com("Schedule.Service")
$schedule.connect($servername)
$tasks = $schedule.getfolder("\").gettasks(0)
$tasks |select name, lasttaskresult, lastruntime, nextruntime

is the "<servername>" the same one as when you get the error with the AD collection?

i am sur eyou can understand that the AD query results will bring you back a list of machiens, some of which will no longer exist, will not be turned on etc....therefore you can expect the $schedule.connect comamdn to these off or old machines to fail, and hence you need to trap for these?
 
Yes the AD query brings back the servername how i would expect to see. I get the error on all the machines. Now I didn't notice this error on my last post, there are 2 errors the "GetFolder" error and then this
Exception Calling "Connect" with "1" argument(s): "Type mismatch. (Exception from HRESULT: 0x80020005 (DISP_E_TYPEMISMATCH)"

So maybe there is some kind of special charater in the output of the servername? Maybe I need to try and reformat the string?

Yes I understand that I need to handle the errors on non accessable machines, but I haven't gotten to that point yet.

Windows Haiku:

Serious error.
All shortcuts have disappeared.
Screen. Mind. Both are blank.
 
write your strings out to the screen. that way you can 'see' what is going on


write-host $servername.name

i believe
 
So anyways I think I have it working. I am confused why but when i chaned
Code:
$schedule.connect($servername.name)
to
Code:
$schedule.connect($($servername.name))
That seemed to work I need to verify the output with what is actually there. Due to my lack of knowledge of powershell (I just jumped in and started looking at examples), does the $ indicate a variable or is it like a pointer of somekind?

Windows Haiku:

Serious error.
All shortcuts have disappeared.
Screen. Mind. Both are blank.
 
well write-host $servername.name gave me the same output as just $servername.name.

Windows Haiku:

Serious error.
All shortcuts have disappeared.
Screen. Mind. Both are blank.
 
one thing it takes forever to error out now, where before it didnt'take nearly as long.

Windows Haiku:

Serious error.
All shortcuts have disappeared.
Screen. Mind. Both are blank.
 
$schedule.connect() is expecting just a standard string to be passed to it. While the display of $servername.name looks like a string, it's actually a "ResultPropertyVauleCollection". $schedule.connect() doesn't know what to do with that, nor does it attempt to evaluate it.

The $() is a subexpression. It forces evaluation of $servername.name, and that evaluation returns a plain string. The other 2 ways around it involve explicitly casting the result as a string:
separate variable
Code:
    $servername = $objResult.Properties
    $server1 = [string] $servername.name
    $schedule = new-object -com("Schedule.Service")
    $schedule.connect($server1)
or in the call itself
Code:
    $servername = $objResult.Properties
    $schedule = new-object -com("Schedule.Service")
    $schedule.connect([string] $servername.name)

I don't know that it matters much which of the 3 methods you use. From a readability standpoint it might make more sense to explicitly cast it to [string].
 
OK that makes sense, that would be why it errors so quickly using just the $servername.name and not error (actually work) using the $($servername.name). Now I have another issue, is there a way to adjust the timeout on the .Connect method, or does should I just start a new thread on that. It thats an extremely long time to time out on a non accessible machine, I put a trap in but it sits there and waits for the error to occur. I would rather it timeout in like 100ms or at most 10 sec.

Windows Haiku:

Serious error.
All shortcuts have disappeared.
Screen. Mind. Both are blank.
 
Right off hand I don't see anything to modify the timeout period. There's nothing in the Connect method itself to change it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top