Combining multipart Powershell script together

Hi All
I am new to the forum and also newbie to PowerShell. I have compiled a script that will open up file explorer to choose a file ( printer package which is .exe file) then it will copy to the remote machine. Here is my code for it…

Blockquote
$computername = read-host -Prompt “Enter Computer Name to install the printer”

Function Get-FileName($initialDirectory)
{
[System.Reflection.Assembly]::LoadWithPartialName(“System.windows.forms”) | Out-Null

$OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog
$OpenFileDialog.initialDirectory = $initialDirectory
#$OpenFileDialog.filter = "CSV (*.csv)| *.csv"
$OpenFileDialog.ShowDialog() | Out-Null
$OpenFileDialog.filename

}
$inputfile = Get-FileName “C:\temp”
$inputdata = get-content $inputfile
$path = ‘\’ + $computername + '\C$\Temp'
Copy-Item -Path $inputfile -Destination $path -Force
Write-Host “Copy file Done”

Blockquote
This part works fine
Now I am enabling the PS-Remoting on $computername by following…

Blockquote
$SessionArgs = @{
ComputerName = $computername
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

Blockquote

After PS-Remoting Enabled, Now run the copied .exe file on remote machine

here I am having challege to choose the selected file in $inputfile

if I chose the file manually the code should be

Invoke-Command -ComputerName $computername -ScriptBlock {& cmd.exe /c “c:\Temp\MH18P03_X5855.exe”} - this is not working for some reason, it worked at one point and I lost the track. Now I am getting error message like this…
‘c:\temp\Printer.exe’ is not recognized as an internal or external command,
+ CategoryInfo : NotSpecified: ('c:\temp\Printe…ternal command,:String) , RemoteException
+ FullyQualifiedErrorId : NativeCommandError
+ PSComputerName : j2xd0169184

operable program or batch file.

After it run the .exe file , I want to disable the PS-Remoting on remote computer by this…

Disabling PS-Remoting

$MethodArgs.Arguments = @{CommandLine = “powershell Start-Process powershell -ArgumentList ‘Disable-PSRemoting -Force’” }
Invoke-CimMethod @MethodArgs
Remove-CimSession -CimSession $MethodArgs.CimSession

So I want to make this one script which I am having trouble with. Any help would be life saver.

I want to replace the “MH18P03_X5855.exe” with the selected file in $inputfile

dbanglad,
Welcome to the forum. :wave:t4:

You’ve probably already noticed that the formatting of your post is obviously messed up a lot. Please go back, edit your existing post and fix the formatting of your code, console output and error messages.

When you post code, sample data, console output or error messages please format it as code using the preformatted text button ( </> ). Simply place your cursor on an empty line, click the button and paste your code.

Thanks in advance

How to format code in PowerShell.org <---- Click :point_up_2:t4: :wink:

Thanks but I was trying to edit it, I do not see any option to edit it

1.

image

2.

image

I only see this in my post
image

Hmmm … ok … so you may add another reply to this answer with the proper formatted code, output and error messages.

Hi All
My previous post about this powershell scripts got messed up so I am reposting this again, hope to get some help . It is working fine now but would like to add some error checking and most importantly I want to replace the manula Printerpackage.exe file in Invoke command to automate from the dialogue box. I would apprecitate to get some Help.

Function Get-FileName($initialDirectory) 
{
    [System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") | Out-Null
    
    $OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog
    $OpenFileDialog.initialDirectory = $initialDirectory
    $OpenFileDialog.ShowDialog() | Out-Null
    $OpenFileDialog.filename
    
}
$computername = read-host -Prompt "Enter Computer Name"

$inputfile = Get-FileName "C:\temp"
$inputdata = get-content $inputfile
$path = '\\' + $computername + '\C$\Temp\'


 $SessionArgs = @{
     ComputerName  = $computername
     #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
 
#copy item to remote computer
Copy-Item -Path $inputfile -Destination $path -Force

Write-Host "     File Copy Done"
# Install the printer package on remote machine

$session = New-PSSession -Computername $computername -Credential (Get-Credential)
Invoke-Command -ComputerName $computername -ScriptBlock {& cmd.exe /c "c:\Temp\PrinterPackage.exe"; Disable-PSRemoting -Force}
#  I  would like to automathe here the PrinterPackage.exe file from the $inputfile
Start-Sleep -Seconds 20
Remove-PSSession $session

Write-Host " Printer Installations is Done"

I actually don’t get what you’re asking. If you use a command what’s error prone you could add a try catch block to handle potentially upcomming errors.

What do you mean with:

?

What I meant is that I choose the file form the dialogue box and it copy to the remote machine and run the file on the remote machine by using following command.

Invoke-Command -ComputerName $computername -ScriptBlock {& cmd.exe /c "c:\Temp\PrinterPackage.exe"; Disable-PSRemoting -Force}

Currently I have to manually input the .exe file name in the invoke-command. I want to input the file name automatically from the chosen file that I took from the dialogue box. Hope I was able to explain.

Ah … I see …
You should add

$FileName = Split-Path -Path $InputFile -Leaf

To get only the file name without the path. And you should change your invoke command to this:

Invoke-Command -ComputerName $computername -ScriptBlock {& "c:\Temp\$USING:FileName"; Disable-PSRemoting -Force}

Might be reading the script wrong, but would it not be the $path, as that is where the file will reside in the remote machine? Which is a UNC path, so you might get into issues running the file.
You might need to handle where the files is stored on the destination machine and use a file path?

Again, might be reading that wrong.