Installing a module remotely

100 views
Skip to first unread message

Mike Leone

unread,
Feb 19, 2021, 4:22:53 PM2/19/21
to NTPowershell Mailing List
I'm having struggles with this. I want to run a script that installs a module remotely, from a local repository. I've got a little bit of code that does all that, if I run the script locally on each host, as administrator. But it's failing miserably, when I try to run it remotely as 

Invoke-Command -ComputerName comp1 -FilePath .\Install-PSWindowsUpdate-Module-from-Repository.PS1

---- Internal Repository not present
Copying, then Importing PackageProvider 'NuGet'
Access is denied
    + CategoryInfo          : PermissionDenied: (\\san2\netadmin...epository\nuget:String) [Copy-Item], UnauthorizedAccessException
    + FullyQualifiedErrorId : ItemExistsUnauthorizedAccessError,Microsoft.PowerShell.Commands.CopyItemCommand
    + PSComputerName        : dc1soa001

Cannot find path '\\san2\netadmin\PHA_PS_Internal_Repository\nuget' because it does not exist.
    + CategoryInfo          : ObjectNotFound: (\\san2\netadmin...epository\nuget:String) [Copy-Item], ItemNotFoundException
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.CopyItemCommand
    + PSComputerName        : dc1soa001

No match was found for the specified search criteria and provider name 'NuGet'. Try 'Get-PackageProvider -ListAvailable' to see if the provider exists on the
system.
    + CategoryInfo          : InvalidData: (NuGet:String) [Import-PackageProvider], Exception
    + FullyQualifiedErrorId : NoMatchFoundForCriteria,Microsoft.PowerShell.PackageManagement.Cmdlets.ImportPackageProvider
    + PSComputerName        : dc1soa001

I *think* it's because the session isn't running as administrator on the remote machine (I know the ID I am running the script from is a member of an AD group that is in the local administrators group).

So what's going wrong? I'm assuming i"m missing something fundamental. Can this been done via invoke-command, or would it need to be a "enter-pssession"? And how would I make sure it runs as administrator?

Anyone ever done this? 

Script:

Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy Bypass -Force

# Connect to local repository, so that we can load modules, in case they are not installed locally
$LocalRepoLocation = "\\fileserver\PHA_PS_Internal_Repository"
$LocalRepoName     = "PHA Internal PSRepository"
$NuGetLocation = "\\fileserver\PHA_PS_Internal_Repository\nuget"
$LocalNuGetLocation = "C:\Program Files\PackageManagement\ProviderAssemblies"
$ModuleName = "PSWindowsUpdate"

IF ((Get-PSRepository | Where {$_ -match "Internal"} ) -eq $null ) {
  Write-Host -ForegroundColor Green "Copying, then Importing PackageProvider"
  Copy-Item -Path $NuGetLocation -Recurse -Destination $LocalNuGetLocation -Container
  Import-PackageProvider -Name NuGet -Verbose #  -RequiredVersion 2.855.208
   Register-PSRepository -Name $LocalRepoName -SourceLocation $LocalRepoLocation -InstallationPolicy Trusted
} ELSE {
}

if ((Get-Module | Where { $_ -match "PSWindowsUpdate" }) -eq $null) {
        Install-Module -Name $ModuleName -Repository $LocalRepoName -Scope CurrentUser
} ELSE {
}

Thanks!
--

Mike. Leone, <mailto:tur...@mike-leone.com>

PGP Fingerprint: 0AA8 DC47 CB63 AE3F C739 6BF9 9AB4 1EF6 5AA5 BCDF
Photo Gallery: <http://www.flickr.com/photos/mikeleonephotos>

This space reserved for future witticisms ...

Michael B. Smith

unread,
Feb 19, 2021, 5:03:46 PM2/19/21
to ntpowe...@googlegroups.com

It’s called the “double hop” problem. You can google/bing that and find out more than I want to type on the topic.

 

Typical way around it is to schedule a single-shot remote task to run with highest privileges.

--
You received this message because you are subscribed to the Google Groups "ntpowershell" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ntpowershell...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ntpowershell/CAHBr%2B%2BhFVjzu50fJ_WVPVjN_EhYWfyTVzcaDRk4yQ-Hgm-5vZQ%40mail.gmail.com.

Kurt Buff, GSEC/GCIH/PCIP

unread,
Feb 19, 2021, 9:40:24 PM2/19/21
to ntpowe...@googlegroups.com
The cheating and not-so-very-maintainable way is to just copy the
module directory to the target machine. It's OK for a one-time use,
but I like MBS' suggestion. I'm trying to implement that myself. It's
the roundtuit that I'm missing

copy -recurse "C:\Program
Files\WindowsPowerShell\Modules\<modulename>" -destination
"\\machinename\c$\C:\Program
Files\WindowsPowerShell\Modules\<modulename>" -recurse

Kurt
Reply all
Reply to author
Forward
0 new messages