Share via

Power Shell Help Needed

DHoss 61 Reputation points
2022-05-26T19:30:46.71+00:00

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
  1. Marco Micozzi 1 Reputation point
    2022-05-29T14:46:48.023+00:00

    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

    Was this answer helpful?


  2. 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.ps1
    

    Then 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.

    Was this answer helpful?


  3. 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.CimSession  
    

    It really just sticking the three scripts together and removing the repetitious code.

    Was this answer helpful?


Your answer

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