Executing Powershell remotely without installing modules locally ?

Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 1 year and 8 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
ITEngineer
Posts: 216
Last visit: Thu Mar 23, 2023 5:45 pm
Has voted: 4 times

Executing Powershell remotely without installing modules locally ?

Post by ITEngineer »

Hi Folks,

I need some help and suggestion on how can I execute this code below from my team desktop while the actual PowerShell modules and the Script.PS1 is located on the \\PAWSVR01-VM\Shared\Script1.PS1 ?

The goal here is to be able to execute the script while the user double-clicking on the script from their desktop will be presented with the login name to remotely executed the server PAWSVR01-VM (Privileged Access Workstation - https://docs.microsoft.com/en-us/securi ... ss-devices).

So all of the Powershell modules are just installed and maintained in the PAWSVR01-VM only rather than on each user's desktop locally.

My script content is like the below:

Code: Select all

Add-Type -AssemblyName 'System.Windows.Forms'
 Add-Type -AssemblyName 'System.Drawing'
 Add-Type -AssemblyName 'Microsoft.VisualBasic'
 [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
 #######################################################################################################################################################################
 $ADURL = 'https://www.microsoft.com/en-us/download/details.aspx?id=45520'
 If (-not (Get-Module -ListAvailable ActiveDirectory))
 {
     Write-Host "No Microsoft Active Directory PowerShell module installed. `n Starting with Windows 10 October 2018 Update, add RSAT tools right from Windows 10. Just go to 'Manage optional features' in Settings and click 'Add a feature' to see the list of available RSAT tools.`n" -WarningAction Continue -ForegroundColor Yellow
     return
 }
 Else
 {
     Try { Import-Module ActiveDirectory -Force -ErrorAction Stop }
     Catch { Write-Warning "Unable to load Microsoft Active Directory PowerShell module because $($Error[0])"; [Diagnostics.Process]::Start($ADURL); return }
 }
 #######################################################################################################################################################################
 $Module = Get-Module -Name Microsoft.Graph -ListAvailable
 if ($Module.count -eq 0)
 {
     Write-Host "Microsoft Graph PowerShell SDK is not available" -ForegroundColor Yellow
     $Confirm = Read-Host "Are you sure you want to install module? [Y] Yes [N] No"
     if ($Confirm -match "[yY]")
     {
         Write-host "Installing Microsoft Graph PowerShell module..."
         Install-Module Microsoft.Graph -Repository PSGallery -Scope CurrentUser -AllowClobber -Force
     }
     else
     {
         Write-Host "Microsoft Graph PowerShell module is required to run this script. Please install module using 'Install-Module Microsoft.Graph' cmdlet."
         Exit
     }
 }
 #######################################################################################################################################################################
 Write-Host "Connecting to Microsoft Graph..." -ForegroundColor Green
    
 $RequiredScopes = @(
     'User.Read.All',
     'Group.Read.All',
     'UserAuthenticationMethod.Read.All'
 )
    
 Connect-MgGraph -Scopes $RequiredScopes
    
 if ((Get-MgContext) -ne "")
 {
     Write-Host Connected to Microsoft Graph PowerShell using (Get-MgContext).Account account -ForegroundColor Yellow
 }
Would that be possible ?
/* IT Engineer */
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Executing Powershell remotely without installing modules locally ?

Post by jvierra »

This is not a PowerShell or script issue. To do this you will need to "publish" the executable to the machine that the user wants to execute it from. Post your question in the RDS forum on Microsoft. There are a number of methods that can accomplish this task.

You can also post to a VM forum as the VM guys can also reference the most current articles on how to publish a process from a VM.
User avatar
ITEngineer
Posts: 216
Last visit: Thu Mar 23, 2023 5:45 pm
Has voted: 4 times

Re: Executing Powershell remotely without installing modules locally ?

Post by ITEngineer »

Hi Mr. Vierra,

Do you mean by using this method http://masteringposh.com/remote-desktop ... s-expanded to publish the script in the RDSH server ?
/* IT Engineer */
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Executing Powershell remotely without installing modules locally ?

Post by jvierra »

That is the best method to make a remote application available to all desktops without having to install anything on the desktop machine.
User avatar
ITEngineer
Posts: 216
Last visit: Thu Mar 23, 2023 5:45 pm
Has voted: 4 times

Re: Executing Powershell remotely without installing modules locally ?

Post by ITEngineer »

Thank you for the suggestion :-)
I will find out some more information about deploying the server.
/* IT Engineer */
This topic is 1 year and 8 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked