header1   header
header
header Register : : Login header
header
connector   connector
menuleft menuright
submenu   submenu
left
Help With If/Else and Varibles
Last Post 25 Feb 2010 08:41 AM by BradC. 5 Replies.
Printer Friendly
  •  
  •  
  •  
  •  
  •  
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages
BradCUser is Offline
New Member
New Member
Posts:20
Avatar

--
19 Feb 2010 01:01 PM


    What I'm trying tp acomplish is to create 2 different list , computers on (or with a service running) and those that are off.  Then from the list that is on  I want to change the local admin password.

    I have test the password change portion of this script and seem to work fine.  I can not get the sorting to work.  the If serverice is running does not work and the esle do not put the computer name in the file.  If I change the $_.Name to a word , the word does go into the file.


    any help or suggestions would be great.


    $Computers = Read-Host "Enter Machine Name"

    $NewPass = Read-Host "Enter New Password"

    Get-QADComputer $Computers | ForEach-Object {add-content -path List.txt -value $_.Name}

    Get-content List.txt | foreach { get-Service -computer $_ -name netlogon }

    $status=get-content List.txt | foreach { get-Service -computer $_ -name netlogon } | Select-Object status
    if ($status -eq "Running") {add-content -path Online.txt -value good}
    else {add-content -path Offline.txt -value $_.name}

    foreach ($Computer in get-content Online.txt)
    {
    $admin=[adsi]("WinNT://" + $computer + "/administrator, user")
    $admin.psbase.invoke("SetPassword", $NewPass )
    }

     

    Steven MurawskiUser is Offline
    Community Co-Director
    Basic Member
    Basic Member
    Posts:137

    --
    19 Feb 2010 01:47 PM
    $_ is the current object in the pipeline, but your if statement is not part of a pipeline, so it has no value.

    We could trim this down a bit..

    $Computers = Read-Host "Enter Machine Name"

    $NewPass = Read-Host "Enter New Password"

    Get-QADComputer $Computers | get-Service -name netlogon | foreach-object {
    if ($_.status -eq 'Running')
    { $_.name | out-file -path online.txt -append }
    else
    { $_.name | out-file -path offline.txt -append }
    }

    foreach ($Computer in get-content Online.txt)
    {
    $admin=[adsi]("WinNT://" + $computer + "/administrator, user")
    $admin.psbase.invoke("SetPassword", $NewPass )
    }

    Steven Murawski
    Blog ( blog.usepowershell.com )
    Co-Host - Mind of Root ( www.mindofroot.com )
    BradCUser is Offline
    New Member
    New Member
    Posts:20
    Avatar

    --
    22 Feb 2010 10:28 AM
    Thanks for the help Steven! I'm getting a little closer to the solution.

    The pipe from "Get-QADComputer $Computers" was throwing out some errors and the " out-file -path " did aswell.

    I believe the -path was do to the fact I did not state a path, rather I used the root folder.

    This is what I have now

    Get-Content list.txt |foreach {get-Service -ComputerName $_ -name netlogon} | foreach-object {
    if ($_.status -eq 'Running')
    { $_.name | out-file online.txt -append }
    else
    { $_.name | out-file offline.txt -append }
    }


    The loop seems to be working well, but I'm still haveing problem with the IF/ELSE part:

    If the PC is off it will throw out an error instead of the ELSE statment to create the offline.txt. That is no big deal because I can compair the list.txt and online.txt to create a the offline.txt.

    The second issue is with the online.txt. What does get inputed is just word "netlogon" (one line for each computer), but no computer name.

    Any suggestion on getting the computer name instead of netlogon. It really does't matter how the list is made aslong as I have to list of computers that are on.

    Steven MurawskiUser is Offline
    Community Co-Director
    Basic Member
    Basic Member
    Posts:137

    --
    22 Feb 2010 11:55 AM

    I would use a custom object to track the name of the server through the pipelin. Couple of possible options, depending on which version of Powershell -



    Version 1

    Get-Content list.txt             
        ForEach-Object {            
            $result = New-Object Object | Select-Object Name, Status            
            $result.Name = $_            
            $result.Status = (Get-Service -ComputerName $result.Name -Name 'netlogon').Status            
            $result            
        } |             
        foreach-object {             
            if ($_.status -eq 'Running')             
            { $_.Name | out-file online.txt -append }             
            else             
            { $_.Name | out-file offline.txt -append }             
        }


    Version 2 - which offers the nice New-Object syntax

    Get-Content list.txt             
        ForEach-Object {            
            New-Object PSObject -Property @{            
                Name = $_            
                Status = (Get-Service -ComputerName $_ -Name 'netlogon').Status            
                }            
        } |             
        foreach-object {             
            if ($_.status -eq 'Running')             
            { $_.Name | out-file online.txt -append }             
            else             
            { $_.Name | out-file offline.txt -append }             
        }

    Last note.. if you want to control how those errors show for machines that are not reachable, check out the -ErrorAction common parameter

    Steven Murawski
    Blog ( blog.usepowershell.com )
    Co-Host - Mind of Root ( www.mindofroot.com )
    Jason ArcherUser is Offline
    New Member
    New Member
    Posts:13
    Avatar

    --
    23 Feb 2010 11:12 AM
    Here, this should work and is cleaner (no transfering information via files!).

    $Computers = Read-Host "Enter Machine Name"

    $NewPass = Read-Host "Enter New Password"

    [String[]]$online = @()
    [String[]]$offline = @()

    Get-QADComputer $Computers | Get-Service -Name "netlogon" -ComputerName {$_.Name} | ForEach-Object {
    if ($_.Status -eq 'Running')
    { $online += $_.MachineName }
    else
    { $offline += $_.MachineName }
    }

    foreach ($Computer in $online)
    {
    $admin=[adsi]("WinNT://$computer/administrator, user")
    $admin.psbase.invoke("SetPassword", $NewPass )
    }
    BradCUser is Offline
    New Member
    New Member
    Posts:20
    Avatar

    --
    25 Feb 2010 08:41 AM
    Thanks for all your help.... You guys and this community rock!

    this is what I ended up with:

    add-PSSnapin quest.activeroles.admanagement

    $Computers = Read-Host "Enter Machine Name"

    $NewPass = Read-Host "Enter New Password"

    [String[]]$online = @()
    [String[]]$offline = @()

    $list=Get-QADComputer $Computers

    Get-QADComputer $Computers | Get-Service -Name "netlogon" -ComputerName {$_.Name} -ErrorAction SilentlyContinue| ForEach-Object {

    if ($_.Status -eq 'Running')
    { $online += $_.MachineName }
    else
    { $offline += $_.MachineName }
    }

    foreach ($Computer in $online)
    {
    $admin=[adsi]("WinNT://$computer/administrator, user")
    $admin.psbase.invoke("SetPassword", $NewPass )
    }
    Compare-Object $list $online |Select-Object inputobject | Format-Table -HideTableHeaders > Still_To_Do.txt

    "The Follow Computer's Password is Now Changed:"
    $online


    Because I needed a list of computers that did not get their password changed and the error from them being off did not pass the machine name to the ELSE statment I decided to use a Compair-Object to make my list. This way at a later time I can try just the PC's that were previously off.

    Again Thanks
    You are not authorized to post a reply.


    Active Forums 4.3
    right
    footer   footer
    footer Sponsored by Quest Software • SAPIEN Technologies • Compellent • Microsoft Windows Server 2008 R2 footer
    footer   footer