Executing Powershell script remotely without installing modules?

Hi Everyone,

I need some help and example to allow a Specific PowerShell script to be executed remotely on the server from my local PC.

The goal is so that I can centralize the execution and the PowerShell modules installation in one single server.

This is the script that I’m currently working with:

$RemoteServer = "Remote-SVR1" + "." + (Get-WmiObject Win32_ComputerSystem).Domain
$session = New-PSSession -ComputerName $RemoteServer

Invoke-Command -Session $session -ScriptBlock {
		Import-Module -Name 'Microsoft.Graph' -Force -ErrorAction Stop
		Import-Module -Name 'ActiveDirectory' -Force -ErrorAction Stop
		
		Read-Host -Prompt $Prompt -AsSecureString
		Read-Host "Please enter input ..."
		
		#Do some proessing and then export to .CSV
		$paramExportCsv = @{
			Path			  = 'Path'
			Delimiter		  = $Delimiter
			InputObject	      = $InputObject
			LiteralPath	      = 'LiteralPath'
			Force			  = $true
			NoClobber		  = $true
			Encoding		  = 'Unicode'
			Append		      = $true
			NoTypeInformation = $true
		}
		
		Export-Csv @paramExportCsv
		
		# Send Email when all is completed...
		$paramSendMailMessage = @{
			To						   = 'To'
			Subject				       = 'Subject'
			Body					   = 'Body'
			SmtpServer				   = 'SmtpServer'
			From					   = 'From'
			Attachments			       = 'Attachments'
			Bcc					       = 'Bcc'
			BodyAsHtml				   = $true
			Encoding				   = $Encoding
			Cc						   = 'Cc'
			DeliveryNotificationOption = 'None'
			Priority				   = 'Normal'
			Credential				   = $Credential
			UseSsl					   = $true
			Port					   = $Port
		}
		
		Send-MailMessage @paramSendMailMessage
		
}

Remove-PSSession $session

Any help and suggestion would be greatly appreciated.

Albert,
Welcome back to the forum. :wave:t4:

That’s most of the times not as easy or helpful as you might think. :wink:

You cannot use a module on a remote computer you installed on your local machine out of the box. You could use implicit remoting but that’s actually made for the other way around. Using a module on your local computer you have installed on a remote computer. And if you want to use this on a remote computer you’d have to deal with the so called double hop issue.

A much better option would be to have a server with all needed modules and run the necessary scripts there.

And BTW: in the code you shared you’re not using the modules you import at the beginning of your script block. :wink:

Here you have some more information about implicit remoting and about how to circumvent the double hop issue:

https://4sysops.com/archives/using-powershell-implicit-remoting

https://4sysops.com/archives/solve-the-powershell-multi-hop-problem-without-using-credssp

1 Like

Hi @Olaf,
Yes, it’s been a while since I posted something back in this forum.

Thank you for your suggestion, it looks like it is not possible without deploying an additional server or Remote Desktop Session Host (RDSH) like in this article: Remote Desktop Services (RDS) – PowerShell App Launch and File Associations Expanded - PowerShell

Please correct me if I’m wrong, by publishing the script on the RDSH server, the modules can stay there and then be updated manually as required.

I’d say it does not have to be a Remote Desktop Session Host and it does not even have to be a server but yes - that’s the idea. You have one machine with all needed modules and you run all queries from there.

1 Like

thanks for the confirmation and the explanation @Olaf :slightly_smiling_face: