Invoke-Command not running as expected

Ask questions about creating Graphical User Interfaces (GUI) in PowerShell and using WinForms controls.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 5 years and 5 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
mqh77777
Posts: 252
Last visit: Mon Feb 26, 2024 10:07 am
Has voted: 1 time

Invoke-Command not running as expected

Post by mqh77777 »

We are using Powershell Studio and using that we have created a tool. This tool has many buttons. One of those buttons will tell you if certain apps are installed on a remote machine.

Code: Select all

	Invoke-Command -ComputerName $PCNameBox.Text -scriptblock {
		if (Test-Path "c:\program files\snare\SnareCore.exe") { Write-Log "Snare is installed" -path "C:\windows\logs\SystemBuild.log" }
		ELSE { Write-Log "Snare did not install" -path "C:\windows\logs\SystemBuild.log" } }

The code above does not do anything. But if I remove the Invoke-Command line it works as expected (but only against my localhost) How do I get this to run on remote Windows 10 workstations? C:\Windows\Logs needs to write to My local C: drive, not the remote/target machine.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Invoke-Command not running as expected

Post by jvierra »

THe code would make more sense written in a readable way:

Code: Select all

$sb = {
    if (Test-Path "c:\program files\snare\SnareCore.exe"){
        Write-Log "Snare is installed" -path "C:\windows\logs\SystemBuild.log" 
    }else{
        Write-Log "Snare did not install" -path "C:\windows\logs\SystemBuild.log" 
    } 
}

$results = Invoke-Command -ComputerName $PCNameBox.Text -scriptblock $sb
[System.Windows.Forms.MessageBox]::Show($results)

It is most likely that "Write-Log" does not exist on the remote system. You should fist test this at a PS prompt to be sure it works.

To write locally you must return the message to write as you cannot write from a remote script to the local system.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Invoke-Command not running as expected

Post by jvierra »

A good method is:

Code: Select all

$sb = {
    Test-Path "c:\program files\snare\SnareCore.exe"
}

$result = Invoke-Command -ComputerName $PCNameBox.Text -scriptblock $sb
if ($result){
    Write-Log "Snare is installed" -path "C:\windows\logs\SystemBuild.log" 
}else{
    Write-Log "Snare did not install" -path "C:\windows\logs\SystemBuild.log" 
} 
User avatar
mqh77777
Posts: 252
Last visit: Mon Feb 26, 2024 10:07 am
Has voted: 1 time

Re: Invoke-Command not running as expected

Post by mqh77777 »

Thanks I will try out your code. and I should have mentioned that Write-Log is a Function that is inside of our PowerShell stuido script. I got this from Microsoft Script Center.

Code: Select all

function Write-Log
{
    [CmdletBinding()]
    Param
    (
        [Parameter(Mandatory=$true,
                   ValueFromPipelineByPropertyName=$true)]
        [ValidateNotNullOrEmpty()]
        [Alias("LogContent")]
        [string]$Message,

        [Parameter(Mandatory=$false)]
        [Alias('LogPath')]
        [string]$Path='C:\Windows\Logs\GetResults.log',
        
        [Parameter(Mandatory=$false)]
        [ValidateSet("Error","Warn","Info")]
        [string]$Level="Info",
        
        [Parameter(Mandatory=$false)]
        [switch]$NoClobber
    )

    Begin
    {
        # Set VerbosePreference to Continue so that verbose messages are displayed.
        $VerbosePreference = 'Continue'
    }
    Process
    {
        
        # If the file already exists and NoClobber was specified, do not write to the log.
        if ((Test-Path $Path) -AND $NoClobber) {
            Write-Error "Log file $Path already exists, and you specified NoClobber. Either delete the file or specify a different name."
            Return
            }

        # If attempting to write to a log file in a folder/path that doesn't exist create the file including the path.
        elseif (!(Test-Path $Path)) {
            Write-Verbose "Creating $Path."
            $NewLogFile = New-Item $Path -Force -ItemType File
            }

        else {
            # Nothing to see here yet.
            }

        # Format Date for our Log File
        $FormattedDate = Get-Date -Format "yyyy-MM-dd HH:mm:ss"

        # Write message to error, warning, or verbose pipeline and specify $LevelText
        switch ($Level) {
            'Error' {
                Write-Error $Message
                $LevelText = 'ERROR:'
                }
            'Warn' {
                Write-Warning $Message
                $LevelText = 'WARNING:'
                }
            'Info' {
                Write-Verbose $Message
                $LevelText = 'INFO:'
                }
            }
        
        # Write log entry to $Path
        "$FormattedDate $LevelText $Message" | Out-File -FilePath $Path -Append
    }
    End
    {
    }
}
This topic is 5 years and 5 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked