OneGet / oneget

PackageManagement (aka OneGet) is a package manager for Windows
MIT License
2.38k stars 190 forks source link

PackageManagement and non-interactive AKA headless sessions #324

Open larssb opened 6 years ago

larssb commented 6 years ago

I'm struggling a bit to get the PackageManagement module to work in non-interactive/headless mode.

This is the scenario:

  1. Created a local windows user
  2. Added the user to the local administrators group
  3. Setup a PowerShell scheduled job which runs a script where I check for....

    all of the above steps is done programmatically via PowerShell from another admin. user via a PowerShell shell running in elevated mode.

  4. The script, as part of a self-update feature, does..... Get-PackageSource -Name "repository name" -ErrorAction Stop <-- to know if the needed repository is there. If not --> Register-PackageSource -Name "repository name -Location "location" -PublishLocation "location" -ScriptSourceLocation "Scripts location" -ScriptPublishLocation "Script publish location" -Trusted -ProviderName PowerShellGet -ErrorAction Stop -Verbose <-- to make sure that it is there and install-package and so forth can be used. However when the job is invoked I get the following errors into some logging I'm doing via log4net: ....Unable to find package source 'repository name'. Use Get-PackageSource to see all available package sources. and for the Register-Packagesource The property 'Values' cannot be found on this object. Verify that the property exists.

Troubleshooting

Log in as the local administrator user that invokes the jobs

  1. Invoke the job via the Scheduled Tasks manager via another user which is in an interactive session
  2. In this scenario it works. 2a. Get-PackageSource can see the registered repo. and if it wasn't registered Register-Packagesource could register it.

    As soon as I logoff the user that runs the PowerShell scheduled job the errors starts showing in the error log logging to again.

Try Packagemanagement cmdlets in an interactive session as the user under which the PowerShell scheduled jobs runs

  1. No problems.

Force and ForceBootstrap on the Get-PackageSource cmdlet

  1. Tried and no difference.

    Not tried for the Register-PackageSource, as if it does not work for Get-PackageSource why should it work for this one.


This leads me to think that non-interactive/headless mode is not supported. Is that fair of me to conclude? If so, will PackageManagement/OneGet support this scenario? Or if it is already supported, I would highly appreciate to get tips on how to make it work.

Thank you a million and have a great Sunday.

larssb commented 6 years ago

I have a somewhat similar question going on over at the PowerShellGet repo. it is here -- just for reference.

loshurik commented 6 years ago

I have the same issue with PackageManagement DSC resource

tehsuk commented 6 years ago

I ran into a similar issue with scheduled tasks running as SYSTEM & installing powershell modules. You probably need to register the package provider first as the user. Run Get-PackageProvider as the user & make sure PowerShellGet is listed.

loshurik commented 6 years ago

@tehsuk thank you for the idea! I've fixed my issue with DSC by switching to another module https://github.com/PowerShell/PackageManagementProviderResource it has PackageManagementSource resource to register the provider

larssb commented 6 years ago

@tehsuk yeah that is an option. However, I want this as automatized as possible and having to loing as the user counteracts that. I had to refactor my update feature and I'm basically using invoke-webrequest and calling the API of the package management system to do what I need to. However, it would still be great to have this working.

loshurik commented 6 years ago

have to reply here again I have a DSC with resources:

PackageManagementSource NugetRepository
    {
        Ensure      = "Present"
        Name        = "TrDevOpsNuget"
        ProviderName= "Nuget"
        SourceUri   = "http://my.internal.nuget.repo"
        InstallationPolicy ="Trusted"
    } 
NugetPackage Agent
{
    DependsOn = "[PackageManagementSource]NugetRepository"
    Name = MyPackage
    Source = "http://my.internal.nuget.repo"
    Ensure = "Present"
    DestinationPath = "C:\Temp"
}

It works OK for the first time and fails for next attempts. The package gets downloaded, everything works as expected. At the second run I expect to get "the state is OK, no changes needed" but NugetPackage throws this: Source 'C:\Temp\MyPackage.1.2.0.279054\MyPackage.1.2.0.279054.nupkg' is not one of the registered sources in 'NuGet' provider. Source 'C:\Temp\MyPackage.1.2.0.279054\MyPackage.1.2.0.279054.nupkg' is a file path. Package 'MyPackage.' not found in the node Ensure of MyPackage package is Absent Resource 'MyPackage' is not in the desired state. Required Ensure is 'Present' and actual Ensure is 'Absent'

Any idea?

brywang-msft commented 6 years ago

@larssb If you can get the -Debug output to log, that would be nice (set $debugpreference = 'continue' as well). Having trouble getting scheduled tasks to work right now. :( If you could help track down the stack trace for when the error occurs, that would help greatly in debugging. Also logging info about the powershell.exe instance that runs would probably be helpful (including the path to powershell.exe).

I think AppVeyor's build agent runs in a non-interactive session, but I'm not sure.

@loshurik Not sure what the issue might be since I'm assuming you've censored the SourceUri/Source parameters, and I think the actual value of SourceUri/Source is causing the issue. :) I think the best way to debug would be to ensure installing from that repo works manually first (using PackageManagement cmdlets instead of DSC).

Any reason you don't want to use the DSC resources present in PackageManagement? https://github.com/PowerShell/PackageManagementProviderResource has been merged into this repo. The MSFT_PSModule and MSFT_NugetPackage resources have been deprecated because MSFT_PackageManagement does the same thing (see the examples). Most likely you'll have to pick this up with the new owner of PackageManagement, once we finish the transfer.

loshurik commented 6 years ago

@brywang-msft The Source is valid, I use it successfully out of DSC at the same node plus the DSC works OK for the first time

The error triggers after a successful run for NugetPackage resource

with PackageManagement I describe the configuration like

PackageManagementSource NugetRepository
{
    Ensure      = "Present"
    Name       = "TrDevOpsNuget"
    ProviderName= "Nuget"
    SourceUri   = "http://my.internal.nuget.repo"
    InstallationPolicy ="Trusted"
}

PackageManagement Agent
{
    DependsOn = "[PackageManagementSource]NugetRepository"
    Name = MyPackage
    Source = "http://my.internal.nuget.repo"
    Ensure = "Present"
    ProviderName = "Nuget"
    AdditionalParameters = @{ Destination = "C:\Temp" }
}

all the interactive runs like Start-DscConfiguration -UseExisting -Wait -Verbose -Debug -Force are successful when the node applies the configuration itself in the pull mode, it throws [[PackageManagement]Agent] Package 'MyPackage' not found. [[PackageManagement]Agent] Resource MyPackage is not in the desired state. Required Ensure is Present and actual Ensure is Absent

abatishchev commented 3 years ago

So how can I get it working in the non-interactive mode?

Running

Install-PackageProvider -Name NuGet -Force -Confirm:$false -Verbose

Still prompts:

Would you like PackageManagement to automatically download and install 'nuget' now? [Y] Yes [N] No [S] Suspend [?] Help (default is "Y"):