none
Powershell Project from a noob RRS feed

  • Question

  • Hi Everyone

    I'm fairly new to this Powershell Stuff.
    It's actually starting to annoy me... It's like I've just had my 1st writers block.

    #location & Variable
    #===================
    $computers = Get-Content C:\Scripts\Computers.csv

    #Processing of the data
    #======================
    foreach ($computer in $computers) { Get-ADComputer -identity $computer -Properties * | FT Name, LastLogonDate -Autosize #| Export-CSV C:\Scripts\ComputerLastLogonDate.csv }

    #Once computer is checked a lived
    #Processes are validated $service = "wuauserv" $stat = "running" $stype = "Automatic" $ComputerName = Get-Content C:\Scripts\ComputerLastLogonDate.csv #$DomainAdmin = "Domain\Admin Account" foreach ($computer in $computers) { Get-Service -Name $service -ComputerName $computer |

    #if off then run activate and request login details
    #Set-Service -Status $stat –StartupType $stype -credntial $DomainAdmin|

    Select-Object -Property MachineName, Name, Status | Sort-Object -Property MachineName | Format-Table -AutoSize }

    #$comptuters export CSV after new status (need to get the foundation right 1st)


    1 - part of the script is suppose to go through a list of pc's, which is does with out a problem. My only issues is that it outputs it one by one instead of a list, so what happens is only one entry goes to the output file.
    On the screen I get a total output one by one.

    2 - I take that list and then filter it.
    Remove the one that I don't need, and have them check the service status if not activated, then to activate it with elevated admin passwords.

    Somehow I think I'm heading on the wrong path here.
    Doesn't prompt me for an admin passoword/user and it gives me access denied.

    Mother!@##$$%%%

    Anyway, hope the gurus can tell me, a way forward.

    Wednesday, September 27, 2017 6:13 AM

All replies

  • $objarray = @()
    
    
    foreach ($computer in $computers) {
    $example = @()
    $computerservice = Get-Service -Name $service -ComputerName $computer
    
        $Status = $computerservice.Status
        $MachineName = $computerservice.MachineName
        $ServiceName = $computerservice.Name
    
        $example = [pscustomobject]@{ 
                    MachineName = $MachineName
                    Status = $Status
                    Name = $ServiceName
                    }
    
        $objarray += $example
    }
    
    
    $objarray

    Try using a custom PSObject to build an individual output and then adding it into an array that can be leveraged as a coagulation of the indivudal objects.

    Just make sure to set $objarray = @()  before your foreach loop, and then $example = @()  inside of the foreach loop.  This empties the arrays with each time you run the script.  Otherwise, you'll start seeing duplicates.

    Hope this helps.


    Thursday, September 28, 2017 2:49 PM