# Combining multipart Powershell script together

**URL:** <https://forums.powershell.org/t/combining-multipart-powershell-script-together/19443>\
**Category:** PowerShell Help\
**Created:** [May 28, 2022, 2:40am UTC](https://forums.powershell.org/t/combining-multipart-powershell-script-together/19443 "2022-05-28T02:40:19Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![dbanglad](https://avatars.discourse-cdn.com/v4/letter/d/a698b9/32.png) [@dbanglad](https://forums.powershell.org/u/dbanglad)\
**Post date:** [May 28, 2022, 2:40am UTC](https://forums.powershell.org/t/combining-multipart-powershell-script-together/19443/1 "2022-05-28T02:40:19Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [May 29, 2022, 2:32pm UTC](https://forums.powershell.org/t/combining-multipart-powershell-script-together/19443/2 "2022-05-29T14:32:28Z")

</div>

dbanglad,  
Welcome to the forum. 👋🏽

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](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/2X/b/bee50c628238f2c076c0504efb6b15f2fb11f002.gif) \<---- Click 👆🏽 😉

---

<div class="post-metadata">

**Author:** ![dbanglad](https://avatars.discourse-cdn.com/v4/letter/d/a698b9/32.png) [@dbanglad](https://forums.powershell.org/u/dbanglad)\
**Post date:** [May 31, 2022, 4:48pm UTC](https://forums.powershell.org/t/combining-multipart-powershell-script-together/19443/3 "2022-05-31T16:48:22Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [May 31, 2022, 5:16pm UTC](https://forums.powershell.org/t/combining-multipart-powershell-script-together/19443/4 "2022-05-31T17:16:36Z")

</div>

**1.**

![image](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/2X/f/f4c2bf0c603a6f1d30ef060a86a6941c5e4dcc08.png)

**2.**

![image](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/2X/e/e082dfbc67c6ee5f95e6c5957c5f26729ae1ca67.png)

---

<div class="post-metadata">

**Author:** ![dbanglad](https://avatars.discourse-cdn.com/v4/letter/d/a698b9/32.png) [@dbanglad](https://forums.powershell.org/u/dbanglad)\
**Post date:** [May 31, 2022, 6:40pm UTC](https://forums.powershell.org/t/combining-multipart-powershell-script-together/19443/5 "2022-05-31T18:40:36Z")

</div>

I only see this in my post  
 ![image](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/2X/2/2d4ef4bd7393383e90308fbea46a9f176be170ba.png)

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [May 31, 2022, 6:48pm UTC](https://forums.powershell.org/t/combining-multipart-powershell-script-together/19443/6 "2022-05-31T18:48:50Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![dbanglad](https://avatars.discourse-cdn.com/v4/letter/d/a698b9/32.png) [@dbanglad](https://forums.powershell.org/u/dbanglad)\
**Post date:** [May 31, 2022, 7:04pm UTC](https://forums.powershell.org/t/combining-multipart-powershell-script-together/19443/7 "2022-05-31T19:04:58Z")

</div>

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.

```auto
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"

```

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [June 1, 2022, 12:23am UTC](https://forums.powershell.org/t/combining-multipart-powershell-script-together/19443/8 "2022-06-01T00:23:29Z")

</div>

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.

> **[about Try Catch Finally - PowerShell](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_try_catch_finally?view=powershell-7.2)**
>
> Describes how to use the \`try\`, \`catch\`, and \`finally\` blocks to handle terminating errors.

What do you mean with:

> [@dbanglad](#):
>
> I want to replace the manula Printerpackage.exe file in Invoke command to automate from the dialogue box

?

---

<div class="post-metadata">

**Author:** ![dbanglad](https://avatars.discourse-cdn.com/v4/letter/d/a698b9/32.png) [@dbanglad](https://forums.powershell.org/u/dbanglad)\
**Post date:** [June 1, 2022, 4:23pm UTC](https://forums.powershell.org/t/combining-multipart-powershell-script-together/19443/9 "2022-06-01T16:23:38Z")

</div>

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.

```auto
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.

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [June 1, 2022, 5:37pm UTC](https://forums.powershell.org/t/combining-multipart-powershell-script-together/19443/10 "2022-06-01T17:37:13Z")

</div>

Ah … I see …  
You should add

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

```

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

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

```

---

<div class="post-metadata">

**Author:** ![alex-innes](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/alex-innes/32/129_2.png) [@alex-innes](https://forums.powershell.org/u/alex-innes)\
**Post date:** [June 2, 2022, 1:01pm UTC](https://forums.powershell.org/t/combining-multipart-powershell-script-together/19443/11 "2022-06-02T13:01:36Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:17pm UTC](https://forums.powershell.org/t/combining-multipart-powershell-script-together/19443/12 "2024-05-16T20:17:51Z")

</div>

Closed on May 16, 2024, 8:17pm UTC


