![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
|
Welcome to Vista Forums we are your forum to discuss Windows Vista x64 and x86 systems. Whether you need help or just want to post an idea you have on Vista, this is the forum for you.
br> br> |
| |||||||
![]() |
| | Thread Tools | Display Modes |
| | #1 (permalink) |
| Guest | Powershell scripts not working with Vista powershell Hi, I am still in the process of learning Powershell and I am in the middle of my transition from windows XP to Vista.. I was trying to run one of my Powershell scripts in my Vista POSH client and it didn't return any results... I then ran it on my XP powershell client and it worked fine?? i am very confused are the versions of powershell that much different between windows OS? Here is the script... $erroractionpreference = "silentlyContinue" $root= new-object System.DirectoryServices.DirectoryEntry("LDAP://RootDSE"); $domain=[adsi]("LDAP://" + $root.Get("defaultNamingContext")); $search = new-object System.DirectoryServices.DirectorySearcher($domain) $search.filter = '(&(objectCategory=Computer)(operatingSystem=*Server*))' $computers = $search.findall()| foreach-object { $comp = $_ $comp | add-member noteproperty "Name" $comp.properties["name"].item(0) if($comp.name -like "site1*") {write-output $comp.name} elseif ($comp.name -like "site2*") {write-output $comp.name} } $computers.length $result = foreach-object { $service = Get-WmiObject -class Win32_Service -computername $computers | where {$_.name -eq "clussvc"} Write-output $service } $result.length $result | sort | format-table -autosize systemName,Name,State,Status,startmode |
| | #2 (permalink) |
| Guest | RE: Powershell scripts not working with Vista powershell Just to add to the previous post.... if i replace the computer search portion $erroractionpreference = "silentlyContinue" $root= new-object System.DirectoryServices.DirectoryEntry("LDAP://RootDSE"); $domain=[adsi]("LDAP://" + $root.Get("defaultNamingContext")); $search = new-object System.DirectoryServices.DirectorySearcher($domain) $search.filter = '(&(objectCategory=Computer)(operatingSystem=*Server*))' $computers = $search.findall()| foreach-object { $comp = $_ $comp | add-member noteproperty "Name" $comp.properties["name"].item(0) if($comp.name -like "site1*") {write-output $comp.name} elseif ($comp.name -like "site2*") {write-output $comp.name} } With : $computers = "servername1","servername2","servername3","servername4" then the following section works fine and displays the value of $result correctly. computers.length $result = foreach-object { $service = Get-WmiObject -class Win32_Service -computername $computers | where {$_.name -eq "clussvc"} Write-output $service } $result.length $result | sort | format-table -autosize systemName,Name,State,Status,startmode am i doing something wrong?? any help is great appriciated!! Thanks in advance |
| | #3 (permalink) | ||||||||||||
| Guest | Re: Powershell scripts not working with Vista powershell Hi Ben, What happens if you comment out your $erroractionpreference = "silentlyContinue" line in the script and then run it? Does Vista give you any errors that were previously being hidden? - Kirk Munro Poshoholic http://poshoholic.com "Ben" <Ben@xxxxxx> wrote in message news:094A48E5-D2EB-4088-9685-73A8411163F8@xxxxxx
| ||||||||||||
| | #4 (permalink) | ||||||||||||
| Guest | Re: Powershell scripts not working with Vista powershell You may have more wrong, but one thing that stands out is your use of foreach-object... that requires piped input. $result = foreach-object { $service = Get-WmiObject -class Win32_Service -computername $computers | where {$_.name -eq "clussvc"} Write-output $service } There are other things that are more a style issue, but this is the only thing I see wrong. "Ben" <Ben@xxxxxx> wrote in message news:094A48E5-D2EB-4088-9685-73A8411163F8@xxxxxx
| ||||||||||||
| | #5 (permalink) | ||||||||||||
| Guest | Re: Powershell scripts not working with Vista powershell Can you try this? $root = new-object System.DirectoryServices.DirectoryEntry("LDAP://RootDSE") $domain= $root.defaultNamingContext $search = new-object System.DirectoryServices.DirectorySearcher($domain,'(&(objectCategory=Computer)(operatingSystem=*Server*))') $computers = $search.findall() | where-Object{($comp.properties.dnshostname -match "site1") -or ($comp.properties.dnshostname -match "site2")} $computers "Ben" <Ben@xxxxxx> wrote in message news:094A48E5-D2EB-4088-9685-73A8411163F8@xxxxxx
| ||||||||||||
| | #6 (permalink) | ||||||||||||||||||||||||
| Guest | Re: Powershell scripts not working with Vista powershell whoops... Try this instead ![]() $root = new-object System.DirectoryServices.DirectoryEntry("LDAP://RootDSE") $domain= [ADSI]"LDAP://$($root.defaultNamingContext)" $search = new-object System.DirectoryServices.DirectorySearcher($domain,'(&(objectCategory=Computer)(operatingSystem=*Server*))') $computers = $search.findall() | where-Object{($_.properties.dnshostname -match "site1") -or ($_.properties.dnshostname -match "site2")} $computers "Brandon Shell [MVP]" <a_bshell@xxxxxx> wrote in message news:ev7yGwtCIHA.5228@xxxxxx
| ||||||||||||||||||||||||
| | #7 (permalink) | ||||||||||||||||||||||||
| Guest | Re: Powershell scripts not working with Vista powershell Hi Brandon, Actually I was going to post that very same thing because it jumped out at me too, but then I tested it and it works just fine (on my XP laptop at least). So while it isn't very intuitive and the use of ForEach-Object is actually altogether unnecessary for that section of the script, I don't think it is "wrong". It's just...um...creatively obscure. ![]() - Kirk Munro Poshoholic http://poshoholic.com "Brandon Shell [MVP]" <a_bshell@xxxxxx> wrote in message news:eat$3ptCIHA.5160@xxxxxx
| ||||||||||||||||||||||||
| | #8 (permalink) | ||||||||||||||||||||||||||||||||||||
| Guest | Re: Powershell scripts not working with Vista powershell I apologize... I didn't mean you could use Foreach-Object that way.. I meant you have to provide some objects to foreach through. One would need $computers at the end of the foreach-object foreach-object -process {echo $_} -input @(1..10) Either way... I agree with you.. remove the $erroractionpreference "Kirk Munro" <sorry@xxxxxx> wrote in message news:e0M29wuCIHA.5044@xxxxxx
| ||||||||||||||||||||||||||||||||||||
| | #9 (permalink) | ||||||||||||||||||||||||||||||||||||
| Guest | Re: Powershell scripts not working with Vista powershell Btw... I think the reason his orginal Foreach-object worked is because Get-WMIObject takes an array for computername Brandon Shell --------------- Blog: http://www.bsonposh.com/ PSH Scripts Project: www.codeplex.com/psobject B> I apologize... I didn't mean you could use Foreach-Object that way.. B> I meant you have to provide some objects to foreach through. B> B> One would need $computers at the end of the foreach-object B> foreach-object -process {echo $_} -input @(1..10) B> B> Either way... I agree with you.. remove the $erroractionpreference B> B> "Kirk Munro" <sorry@xxxxxx> wrote in message B> news:e0M29wuCIHA.5044@xxxxxx B>
| ||||||||||||||||||||||||||||||||||||
| | #10 (permalink) | ||||||||||||||||||||||||||||||||||||
| Guest | Re: Powershell scripts not working with Vista powershell I'm not so sure. You can execute this one-liner in PowerShell just fine: $wmiServices = ForEach-Object { Get-Service wmi* } And after the execution is done, $wmiServices will contain all services that start with wmi. It surprised me that this would work at all. I wonder if the PowerShell interpreter is recognizing that ForEach-Object doesn't have any input and therefore is simply executing the contents. At any rate, the ForEach-Object cmdlet is not necessary at all in cases like this and can be removed. My only point was that according to PowerShell it doesn't seem to be "wrong". - Kirk Munro Poshoholic http://poshoholic.com "Brandon Shell [MVP]" <a_bshell.mask@xxxxxx> wrote in message news:29d4f646192c8c9d939ea074264@xxxxxx
| ||||||||||||||||||||||||||||||||||||
| |
| |
![]() |
| Thread Tools | |
| Display Modes | |
| |
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| run PowerShell scripts from ASP or asp.net | Carlos | PowerShell | 1 | 04-18-2008 07:12 AM |
| Examples of PowerShell scripts | imarchenko | PowerShell | 11 | 03-14-2008 06:14 PM |
| Can't run powershell scripts | Craig J. Lindstrom | PowerShell | 3 | 10-03-2007 02:03 PM |
| Debugging Powershell scripts | ManfredR | PowerShell | 4 | 03-28-2007 12:22 PM |
| Creating Powershell Scripts | bill | PowerShell | 7 | 12-24-2006 01:29 PM |