Is enabling PSRemoting a requirement? If it isn't you could try to just run the code that installs the printer via psexec. That way you don't need to enable and disable PSRemoting again.
Alternatively you could deploy the script via ConfigMgr if available or even via GPO
Power Shell Help Needed
Hello Folks,
I have finally got the piece of code working but I need help to put it together. I am trying to install printer remotely on to users computer. it is working but part by part. I want to put this together and having some difficulties. My first part of code is to enable the PS Remoting onto remote computer....
$SessionArgs = @{
ComputerName = 'computername' ** ( I would prefer to user variable here)
#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
This part works. Then I do is ...create a new PS Session by following.....
$session = New-PSSession -Computername 'computername'
Invoke-Command -ComputerName ComputerName-ScriptBlock {& cmd.exe /c "\\computername\c$\temp\Printer.exe"}
Remove-PSSession $session
This also works and it install the printer package which is a .exe file.
Now after this I use following code to disable the PS Remoting on to the remote computer....
$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 'Disable-PSRemoting -Force'"
}
}
Invoke-CimMethod @MethodArgs
And this also works fine individually.
Now I want to put this together so I do not have to do it one after another. I would also like to use the $computer instead of ComputerName.
My ideal situation would be...when I run this scripts it should open the explorer to chose the printer package file to copy and then it will copy to the remote machine's c:\temp folder.
I hope I am not asking too much but I know you all genius will be able to help me out.
Windows for business | Windows Server | User experience | PowerShell
3 answers
Sort by: Most helpful
-
Marco Micozzi 1 Reputation point
2022-05-29T14:46:48.023+00:00 -
Rich Matheisen 48,116 Reputation points
2022-05-28T02:13:54.547+00:00 Let's try this: put each of your three original scripts into individual PS1 files. Then create a new PS1 file that looks like this:
& <PATH>\Script1.ps1 & <PATH>\Script2.ps1 & <PATH>\Script3.ps1Then run the new PS1 file.
Does that work? I just want to verify that I didn't mess anything up when I merged the three scripts.
-
Rich Matheisen 48,116 Reputation points
2022-05-27T02:09:24.937+00:00 You didn't mention how you wanted to acquire the name(s) of the remote computer(s).
See if this works:
$computername = "remotecomputer" $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 Invoke-Command -ComputerName $computername -ScriptBlock {& cmd.exe /c "c:\temp\Printer.exe"} $MethodArgs.Arguments = @{CommandLine = "powershell Start-Process powershell -ArgumentList 'Disable-PSRemoting -Force'" } Invoke-CimMethod @MethodArgs Remove-CimSession -CimSession $MethodArgs.CimSessionIt really just sticking the three scripts together and removing the repetitious code.