File chossen from dialogue box is not recognized

DHoss 61 Reputation points
2022-06-14T14:16:38.593+00:00

Hi All
Here I am again. I have this script that when I run it ask for remote computer name then open dialogue box to select a folder then it copy the folder to remote machine then it open up another dialogue box to select the .exe/.cmd/.bat file and Invoke it to remote computer. I am not sure it I am doing right selecting the file in Invoke command as it is giving me some error saying it is not recognized as an internal or external command. I would really appreciate some help from all of you expert out there. Thanks a lot in advance. Here is my code ....

$Computer = Read-Host -Prompt "Enter Computer Name"  
$folder = Test-Path -path "\\$computer\c$\setup"  
IF (Test-Connection -BufferSize 32 -Count 1 -ComputerName $Computer -Quiet) {  
  
       Try {  
              if ($folder -eq $true) {  
  
  
                                  Try {  
  
                                         # Function Get-Folder($initialDirectory="")  
                                        Function Get-Folder($initialDirectory)  
  
                                        {  
                                            [System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms")|Out-Null  
  
                                            $foldername = New-Object System.Windows.Forms.FolderBrowserDialog  
                                            $foldername.Description = "Select a folder"  
                                            $foldername.rootfolder = "MyComputer"  
                                            $foldername.SelectedPath = $initialDirectory  
                                            $foldername.ShowDialog() | Out-Null  
                                            $foldername.SelectedPath  
                                        }  
  
  
                                         $a = Get-Folder  
  
                                        Copy-Item -Path $a -Destination "\\$Computer\C$\Temp\" -Recurse -Force | Out-Null  
                                        Write-host "Foler is Copied" -ForegroundColor Yellow  
  
                                                    Function Get-FileName($initialDirectory)   
                                                        {  
                                                         [System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") | Out-Null  
      
                                                            $OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog -Property @{  
                                                                 Multiselect = $true  
                                                                     }  
                                                                $OpenFileDialog.Title = 'Select the file you want to copy'  
                                                                 $OpenFileDialog.initialDirectory = $initialDirectory  
                                                                #$OpenFileDialog.filter = "CSV (*.csv)| *.csv"  ### If you want to specify file type  
                                                                $OpenFileDialog.ShowDialog() | Out-Null  
                                                                $OpenFileDialog.filename  
      
                                                            }  
                                                            $ScriptDir = Split-Path $script:MyInvocation.MyCommand.Path  
                                                            $inputfile = Get-FileName $a # -AllowMultiSelect  
                                                            $filename = Split-Path -Path $inputfile -Leaf -Resolve  
  
                                                            $SessionArgs = @{  
                                                            ComputerName  = $computer  
                                                            #Credential    = Get-Credential    
                                                            SessionOption = New-CimSessionOption -Protocol Dcom  
                                                            }  
                                                            $MethodArgs = @{  
                                                                         ClassName     = 'Win32_Process'  
                                                                         MethodName    = 'Create'  
                                                                         CimSession    = New-CimSession @SessionArgs  
                                                                         Arguments     = @{  
                                                                        CommandLine = "powershell Start-Process powershell -ArgumentList 'Enable-PSRemoting -Force'"  
                                                                             }  
                                                                            }  
                                                                        Invoke-CimMethod @MethodArgs  
                                                       # I am having problem at below line  
                                                            Invoke-Command -ComputerName $Computer -ScriptBlock {& cmd.exe /c "$a $($Using:filename)"}  
                                                            #Invoke-Command -ComputerName $Computer -ScriptBlock {& cmd.exe /c $a"\"$filename}  
                                                            Start-Sleep -Seconds 20  
                                                            Invoke-Command -ComputerName $computer -FilePath c:\Temp\Reverse.ps1 # Script resiging on local computer  
                                                            Write-Host " Printer Installations is done "  
                                                            Write-Host ""  
                                                            Start-Sleep -Seconds 3  
  
  
                                     }  
                                      Catch {throw $_.Exception.Message}  
                                      }  
              else {  
                    Write-Host "Access Denied to $computer" -ForegroundColor Yellow }  
  
  
       }  
  
        Catch {throw $_.Exception.Message}  
  
  
}  
  
  
Else {   
        Write-Host " $Computer is OffLine " -ForegroundColor Green }  

The error message is like this....
9356 0 k8xd0201421
'\filename.exe' is not recognized as an internal or external command,
+ CategoryInfo : NotSpecified: ('\ed101ridgetop...ternal command,:String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError
+ PSComputerName : k8xd0201421

operable program or batch file.  
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,602 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Rich Matheisen 47,496 Reputation points
    2022-06-14T14:33:42.67+00:00

    The variable $filename contains only the value "filename.exe", but the working directory (which appears to be the root directory "\") and the contents of the PATH environment variable on the remote machine don't contain the directory in which "filename.exe" can be found.


  2. Ludwig 1 Reputation point
    2022-06-14T18:02:01.807+00:00

    Use "Copy-Item" to get the destination path, like:

    $copy = Copy-Item -Path $a -Destination "\\$Computer\C$\Temp\" -Recurse -Force -PassThru | Out-Null  
    

    Now you can use $copy to Invoke the cmd.

    Invoke-Command -ComputerName $Computer -ScriptBlock {& cmd.exe /c '$copy.Fullname'}  
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.