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!
--