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.
Locked
User avatar
mqh777
Posts: 237
Joined: Tue Feb 25, 2014 1:01 pm

Invoke-Command not running as expected

Post by mqh777 » Wed Oct 03, 2018 1:50 pm

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.

User avatar
jvierra
Posts: 13718
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Invoke-Command not running as expected

Post by jvierra » Wed Oct 03, 2018 2:05 pm

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.

User avatar
jvierra
Posts: 13718
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Invoke-Command not running as expected

Post by jvierra » Wed Oct 03, 2018 2:09 pm

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
mqh777
Posts: 237
Joined: Tue Feb 25, 2014 1:01 pm

Re: Invoke-Command not running as expected

Post by mqh777 » Wed Oct 03, 2018 2:44 pm

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
    {
    }
}

Locked