ALombardi01

Forum Replies Created

Viewing 10 posts - 1 through 10 (of 10 total)
  • Author
    Posts
  • in reply to: Question about Best Practice – IfElse or Switch #245181
    Participant
    Topics: 5
    Replies: 10
    Points: 87
    Rank: Member

    Thanks to both of you, @Iain and @Rob

    in reply to: Trying to get random line, store it, then delete it #244337
    Participant
    Topics: 5
    Replies: 10
    Points: 87
    Rank: Member

     Just move the SET of the content out of the pipeline:

    Thanks!

    in reply to: Logging Error/Warning #228268
    Participant
    Topics: 5
    Replies: 10
    Points: 87
    Rank: Member

    I want it to keep running against all the servers it can connect to and run against then tell me which it had issues with, so I could rectify those issues.

    First off, this is the default behavior of this code

    PowerShell
    10 lines

    <textarea class=”ace_text-input” style=”opacity: 0; height: 18px; width: 6.59781px; left: 51px; top: 0px;” spellcheck=”false” wrap=”off”></textarea>

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    $results = Foreach ($Computer in $Computers) {
    Try {
    Invoke-Command ComputerName $computer ErrorAction STOP ScriptBlock {
    # INSERT SOME COMMAND HERE
    }
    } Catch {
    $_.Exception | Out-File ‘C:\Logs\Command-Errors.txt’ Append
    }
    }
    XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

    In other words, this code, as is, will loop through the full list of $computers even if some error out in the middle.

    Have you not tested the code before asking the question!!??

    Second, read the blessed article posted. It specifically addresses this scenario, and I quote:

    PowerShell
    21 lines

    <textarea class=”ace_text-input” style=”opacity: 0; height: 18px; width: 6.59781px; left: 51px; top: 0px;” spellcheck=”false” wrap=”off”></textarea>

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    What if we want to process all files that do exist, yet be able to act individually on those that do not. One way to do that is to read data
    from the Error pipeline:
    $Error.Clear()
    Get-Item Path .\iis1.txt,.\not-there1.txt,.\iis2.txt,.\not-there2.txt,.\iis3.txt
    $MyErrors = @()
    If ($Error) {
    $Error | % {
    $Props = [ordered]@{
    Name = $_.CategoryInfo.TargetName
    Category = $_.CategoryInfo.Category
    Exception = $_.Exception | Out-String
    }
    $MyErrors += New-Object TypeName PSObject Property $Props
    }
    }
    $MyErrors | FT Auto
    In this example, I cleared the Error pipeline, ran the Get-Item cmdlet, then read through the Error records, extracted information I need and
    may want to act on, and saved them to members of $MyErrors array.
    XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

    Sorrrrryyyyy. I was just asking before I began playing around with it as I had some other work items going on so I didn’t read too much into it yet.

    in reply to: Logging Error/Warning #228265
    Participant
    Topics: 5
    Replies: 10
    Points: 87
    Rank: Member

    IMHO, storing anything in a text file (txt) for logging takes a huge amount of functionality away. If you do the following:

    XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

    You take a good return and duplicate the object in your catch. Now you are returning the same object structure for success and fails. This can now be analyzed in Powershell or Excel using filters:

    XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

    I’d swear you are following me now, correcting all the things, but I do appreciate it! 😀

    I’ll play with it and wrap my head around it.

     

     

    in reply to: Logging Error/Warning #228220
    Participant
    Topics: 5
    Replies: 10
    Points: 87
    Rank: Member

    for try/catch to work, you need to turn the error into a terminating error as in

    PowerShell
    8 lines

    <textarea class=”ace_text-input” style=”opacity: 0; height: 18px; width: 6.59781px; left: 44px; top: 0px;” spellcheck=”false” wrap=”off”></textarea>

    1
    2
    3
    4
    5
    6
    7
    8
    Try {
    Invoke-Command ComputerName $computer ErrorAction STOP ScriptBlock {
    # INSERT SOME COMMAND HERE
    }
    } Catch {
    $_.Exception | Out-File ‘C:\Logs\Command-Errors.txt’ Append
    }
    XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

    see https://superwidgets.wordpress.com/2014/12/22/powershell-erroraction/

    But say I’m scanning against 500 servers. I want it to keep running against all the servers it can connect to and run against then tell me which it had issues with, so I could rectify those issues.

    Seeing as I’d rather not stop the command if possible so it can keep running, is there another method to capture the error message into a separate log/file then?

    I guess, would this stop all the other Invoke’s or just the one it had an error on?

    • This reply was modified 6 months, 2 weeks ago by ALombardi01.
    in reply to: Using Invoke-Command, display which machine responded #228217
    Participant
    Topics: 5
    Replies: 10
    Points: 87
    Rank: Member

    I guess my question would be, what is the major benefit of doing it the way I did versus this?

    First, sorry for the incorrect placement of the Computer param, was juggling multiple things. The major benefit is the code works. 🙂 The only reason the code you were running was working was it was local. When you execute the script block, that is all that the remote computer sees, basically:

    PowerShell
    4 lines

    <textarea class=”ace_text-input” style=”opacity: 0; height: 18px; width: 6.59781px; left: 44px; top: 0px;” spellcheck=”false” wrap=”off”></textarea>

    1
    2
    3
    4
    Get-ItemProperty path | Format-Table @{Label=‘Server’;e={$Computer}},,,}
    #or
    Get-ItemProperty path NULL | Format-Table @{Label=‘Server’;e={$Computer}},NULL, NULL, NULL
    XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

    This would work:

    PowerShell
    14 lines

    <textarea class=”ace_text-input” style=”opacity: 0; height: 18px; width: 6.59781px; left: 51px; top: 0px;” spellcheck=”false” wrap=”off”></textarea>

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    $Computers = (Get-ADComputer Filter * SearchBase “DC=Lombardi,DC=local”).Name
    # Script/Command to run against each machine found within the given OU
    $results = Foreach ($Computer in $Computers) {
    Invoke-Command ComputerName $Computer ScriptBlock {
    $Path = ‘HKLM:\System\CurrentControlSet\Control\CrashControl’
    $Key1 = ‘CrashDumpEnabled’
    $Key2 = ‘AlwaysKeepMemoryDump’
    $Key3 = ‘Overwrite’
    Get-ItemProperty path $using:Path ComputerName $computer |
    Select-Object Property $using:Key1, $using:Key2, $using:Key3
    }
    XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

    $Using basically replaces those variables with values when sending to the remote session, otherwise the remote system has no idea what any of those variables are as they are outside of the script block.

    Gotcha. Thanks for clarifying.

    in reply to: Using Invoke-Command, display which machine responded #228124
    Participant
    Topics: 5
    Replies: 10
    Points: 87
    Rank: Member

    Sorry, that was a statement, not a question. You are executing the same code on your local machine, not the remote machine. Try something like this:

    It doesn’t seem to like the use of the $Using variable. I’ll need to check it out more.

     

    I figured it out. I pulled the -ComputerName $Computer portion out of the ScriptBlock.

    Input:

    Output:

     

    I guess my question would be, what is the major benefit of doing it the way I did versus this?

     

     

    • This reply was modified 6 months, 2 weeks ago by ALombardi01.
    • This reply was modified 6 months, 2 weeks ago by ALombardi01.
    • This reply was modified 6 months, 2 weeks ago by ALombardi01. Reason: Trying to fix this quote dammit
    • This reply was modified 6 months, 2 weeks ago by ALombardi01.
    • This reply was modified 6 months, 2 weeks ago by ALombardi01.
    in reply to: Using Invoke-Command, display which machine responded #227920
    Participant
    Topics: 5
    Replies: 10
    Points: 87
    Rank: Member

    Also, just as a side note, you are looping though the computers but not adding a -ComputerName param and it’s invoking the command on the local machine? Next, you may want to look at about_Remote_Variables as you are passing external variables into the scriptblock that is executing remotely. Those setting should be managed by GPO, so you may want to validate those policy settings, OU links and servers are in the correct OU.

     

    Not sure if you are asking me or telling me, sorry. So I am only running the command against the local machine and not against the other computers?

    in reply to: Using Invoke-Command, display which machine responded #227911
    Participant
    Topics: 5
    Replies: 10
    Points: 87
    Rank: Member

    By default invoke-command returns property PSComputerName. You have to add parameter -HideComputerName to have it not return it. Now if it isn’t showing in the default output, you can ask for it.

    XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

    Hope this helps

    I think I may have figured out another option. I had another command I ran to check versions of PowerShell on systems and I threw the $Computer command in there. Originally on this code it didn’t like it. I made it part of it’s own label/result now and it doesn’t complain anymore.

     

    Test Environment Results:

    Input:

     

    Output:

     

    Using it as Format-List (in lieu of table) returns the following, so it shows good either way I want to utilize the info. I’ll likely stick with a table.

    in reply to: Run Function multiple times concurrently #227851
    Participant
    Topics: 5
    Replies: 10
    Points: 87
    Rank: Member

    Is this something you need to happen when you manually run it or for a Scheduled Task kind of deal?

     

    I would think Out-Null after the first command OR using the -Wait command should do it.

Viewing 10 posts - 1 through 10 (of 10 total)