Quantcast
Channel: Windows PowerShell forum
Viewing all articles
Browse latest Browse all 21975

IF statement and loop

$
0
0

Hello PowerShell smart people..

I am working on a little project where I want to remotely execute an .exe with switches and parameters when a computer connects to the network. Here is the PowerShell I have so far.

## Declare credentials
$username = "username"
$password = cat C:\mysecurestring.txt | ConvertTo-SecureString
$cred = New-Object -TypeName System.Management.Automation.PSCredential '
        -argumentlist $username, $password '

## Test if computer is online
If (Test-Connection 192.168.1.100)

## If computer is online then connect to it remotely in PowerShell
    {
    Enter-PSSession -ComputerName hostname -Credential $cred

## Execute command for program
    Invoke-Command -ScriptBlock {program.exe -parameter 1 /f /s -parameter 2}
    }

I have successfully been able to manually run the Enter-PSSession and get to a shell on the remote computer. From there I am successfully able to execute "program.exe -parameter 1 /f /s -parameter 2". Now the goal is to automate the process. The Test-Connection should listen on the network for the IP to come online. Once it does then a Enter-PSSession should be established and the program.exe executed. Sometimes the program.exe will quit but I will still be connected to the remote PowerShell session. I would like the .exe to restart if it quits. Once the computer goes offline, I want the PowerShell to go back to listening and waiting for it again.


Thanks in advance for the assistance.


Viewing all articles
Browse latest Browse all 21975

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>