Trying to figure multiple IF statements in a FOREACH

This topic contains 0 replies, has 1 voice, and was last updated by Profile photo of Forums Archives Forums Archives 5 years, 4 months ago.

  • Author
    Posts
  • #5018

    by jcrgraham at 2012-08-17 15:17:20

    Hi there!
    New to posting so please let me know how to better my posts.

    Have some code that I'm working on. The principle is simple: Read a list of computers, check to see if they are responding. If they are, check to see if a file is on the remote system or not and write output to files. If they aren't responding at all then log their name to another file.

    I've put a line of hash marks where I"m having problems. It hits the Enter-PSSession but then jumps to the Write-Host "done checking". It opens all the sessions, just doesn't enter them to run the check. Any suggestions would be much appreciated!


    foreach ($Hosts in (get-content .\input\comp.txt))

    {
    $Hosts

    If (test-connection -Count 1 $Hosts)
    {
    Write-Host "I'm available, checking for Cisco Patch: $Hosts" -foregroundcolor DarkGreen

    $Session = New-PSsession –Computername $Hosts
    Enter-PSSession $Session
    ################################################################################################################################
    if (( Test-Path c:\users\agent_done.txt -PathType Leaf) -eq $true)
    {
    Exit-PSSession
    Get-WmiObject -ComputerName $Hosts win32_computersystem | select name, username |Export-Csv -NoTypeInformation .\output\passed.csv -Append
    Remove-PSSession $session.id
    Write-Host "Done checking for update $Hosts" -foregroundcolor Blue
    }
    elseif (( Test-Path c:\users\agent_done.txt -PathType Leaf) -eq $false)
    {
    Exit-PSSession
    Remove-PSSession $session.id
    $Hosts
    Get-WmiObject -ComputerName $comp win32_computersystem | select name, username | Export-CSV .\output\failed.csv -Append
    }

    }

    else {$Hosts|out-file .\output\noton.txt -Append}
    }

    by poshoholic at 2012-08-17 16:26:48

    Welcome to the PowerShell.org forums!

    Your question is not uncommon at all. In fact it came up on the Exchange forum yesterday.

    Enter-PSSession and Exit-PSSession are designed for interactive remoting. For example, you're at your desk and you want to run some PowerShell commands on a server that is not right next to you. If you have PowerShell remoting enabled on that server and you have appropriate access, you can invoke Enter-PSSession with the appropriate parameters to open a session on that server, run the PowerShell commands you want to run on that server interactively, and then invoke Exit-PSSession to end the interactive session. These commands are not what you want to use in a script that needs to run without user interaction.

    Invoke-Command is designed to allow you to run a command or an entire script on a remote system without interaction. You simply identify the name of the computer, the script block that you want to run, and possibly credentials if you need to use alternate credentials and PowerShell will run the script block on the remote system (assuming access, remoting, etc. are all configured correctly). This is the command that you want to use when invoking a single command or a series of commands on a remote system. You can create a new session with New-PSSession, pass that in to Invoke-Command, and then remove the session using Remove-PSSession. Or you can simply invoke Invoke-Command and it will create the session and remove it when it has finished. This is good to know in case you want to make multiple Invoke-Command calls using one session in all of them instead of a single Invoke-Command call which creates and disposes of the session itself.

    With all of that knowledge in hand, you should wrap the logic you want to run on a remote system in a script block (i.e. put it inside curly braces), and then use Invoke-Command to run that script block on the remote system. Oh, and remove your Enter-PSSession and Exit-PSSession calls since they are not appropriate for what you're trying to do.

    by jcrgraham at 2012-08-18 11:27:10

    Thanks Poshoholic,

    I'll give it a try when I get in on Monday. I didn't realize that you could use invoke-command to run the Test-Connection on a defined path on the remote system...that's why I used the PSSession. I think, though, I can see what you are saying about putting it in the script block ( now to bend my noodle around the syntax ) and it will be much less noisy then opening sessions, entering, exiting, closing...

    Cheers!

    J

    by jcrgraham at 2012-08-20 16:11:59

    That's what I needed...a good push in the right direction! Thanks again!!!!

    by poshoholic at 2012-08-20 16:19:32

    Excellent news, I'm glad it worked out for you!

You must be logged in to reply to this topic.