OpsaC - Operating as PowerShell code
published: September 19, 2021 author: Tinu tags: PowerShell categories: PowerShell-Repositories
It’s possible to work with local, private PowerShellGet Repositories! The File Share Repository is the simplest way to work with local, private Repositories.
NuGet.exe must be available in:
Microsoft.PackageManagement.NuGetProvider.dll must be available in:
PowerShell Modules:
Download the Requirements from this site as PowerShellGetForOffline.zip:
Name | Size | SHA256 |
---|---|---|
PowerShellGetForOffline.zip | 3427528 bytes (3347 KiB) | B430D703B08D0167D013DFD8E2DDF466D69BD37E827E67AEEAE99A178F57F49A |
Create a new share on your NAS or something like that.
Register your new share as a PowerShellGet Repository
$LocalGallery = @{
Name = "LocalGallery"
SourceLocation = "\\YourShare\PSRepository"
ScriptSourceLocation = "\\YourShare\PSRepository"
InstallationPolicy = 'Trusted'
}
Register-PSRepository @LocalGallery
Get-PSRepository | Select-Object Name,Trusted,SourceLocation,ScriptSourceLocation,Registered,PackageManagementProvider | Format-Table -AutoSize
[ Top ]
You can download a docker image of sonatype nexus3 for your internal PowerShell Gallery.
Docker Pull:
docker pull sonatype/nexus3
To run, binding the exposed port 8081 to the host, use:
docker run -d -p 8081:8081 --name nexus sonatype/nexus3
docker exec -it nexus1 /bin/bash
cd /nexus-data
cat admin.password
Open http://localhost:8081 and login as admin. Change the password in the wizard.
Navigate to Repository administration, click Security, Users and create a new user. Add the user to the nx-admin group.
To publish Modules and Scripts, you must have a valide NuGet API Key. Open http://localhost:8081 and login as user. Navigate to Security, Realms and activate NuGet API-Key Realm.
Navigate to the user profile and click NuGet API Key and click the Button Access API Key and copy the API Key.
Create two new Nuget repositories, one for PowerShell Modules and the other for PowerShell Scripts.
Open http://localhost:8081 and login as user. Navigate to Repository administration, click Repositories and create a Nuget (hosted) Repository for PSModules and one for PSScripts.
The new repositories could be accessed over http://localhost:8081/repository/PSModules/
and http://localhost:8081/repository/PSScripts/
.
Before the nexus repository can be used, it must be registered using the Register-PSRepository command. For nexus repositories with no anonymous access you must provide your credentials:
$PSModulesUri = 'http://localhost:8081/repository/PSModules/'
$PSSCriptsUri = 'http://localhost:8081/repository/PSSCripts/'
$Splatting = @{
Name = 'myPSGallery'
SourceLocation = $PSModulesUri
PublishLocation = $PSModulesUri
ScriptSourceLocation = $PSSCriptsUri
ScriptPublishLocation = $PSSCriptsUri
InstallationPolicy = 'Trusted'
PackageManagementProvider = 'NuGet'
Credential = Get-Credential
}
Register-PSRepository @Splatting -Verbose
[ Top ]
For file share-based repositories, SourceLocation and ScriptSourceLocation must match. That’s why I move the module to a Modules subfolder.
Publish-Module -Path 'D:\temp\PsNetTools' -Repository LocalGallery -NuGetApiKey 'AnyStringWillDo'
Move-Item -Path '\\YourShare\PSRepository\PsNetTools.0.7.65.nupkg' `
-Destination '\\YourShare\PSRepository\Modules\PsNetTools.0.7.65.nupkg'
Find-Module -Repository LocalGallery | Select-Object Version,Name,Description
For nexus repositories with no anonymous access you must provide your API Key/credentials:
Publish-Module -Path 'D:\temp\PsNetTools' -Repository myPSGallery -NuGetApiKey 'YourAPIKey' -Verbose
Find-Module -Repository myPSGallery -Credential (Get-Credential) -Verbose
Find-Module -Repository LocalGallery
Install-Module -Repository LocalGallery -Name PsNetTools
Get-InstalledModule
Test-PsNetDig tinuwalther.github.io
Uninstall-Module -Name PsNetTools
<#PSScriptInfo
.VERSION 1.0.2
.GUID cbf898c0-df4e-44ad-9dbc-7a28c68f4797
.AUTHOR it@martin-walther.ch
.COMPANYNAME
.COPYRIGHT
.TAGS
.LICENSEURI
.PROJECTURI
.ICONURI
.EXTERNALMODULEDEPENDENCIES
.REQUIREDSCRIPTS
.EXTERNALSCRIPTDEPENDENCIES
.RELEASENOTES
.PRIVATEDATA
#>
<#
.DESCRIPTION
Create simple iCalendar Event with the properties EventStart, EventEnd, EventSubject, EventDescription, and EventLocation
.PARAMETER EventStart
Start-Timestamp of the event '18.09.2021 18:00'
.PARAMETER EventEnd
End-Timestamp of the event '18.09.2021 19:00'
.PARAMETER EventSubject
Event-Subject of the event
.PARAMETER EventDescription
Event--Description of the event
.PARAMETER EventLocation
Event-Location of the event
.PARAMETER FullFilename
Full-Filename of the iCalendar-File, the extension .ics will be added automatically
.EXAMPLE
$EventProperties = @{
EventStart = '18.09.2021 18:00'
EventEnd = '18.09.2021 18:30'
EventSubject = 'My Event'
EventDescription = 'MyEvent Description'
EventLocation = 'MyEvent Location'
}
$FullFilename = (Join-Path -Path $PSScriptRoot -ChildPath $EventProperties.EventSubject)
New-PSiCalendarEvent @EventProperties -FullFilename $FullFilename
#>
[Cmdletbinding()]
param(
[Parameter(Mandatory=$true)]
[String] $EventStart,
[Parameter(Mandatory=$true)]
[String] $EventEnd,
[Parameter(Mandatory=$true)]
[String] $EventSubject,
[Parameter(Mandatory=$false)]
[String] $EventDescription,
[Parameter(Mandatory=$false)]
[String] $EventLocation,
[Parameter(Mandatory=$false)]
[String] $FullFilename
)
# Custom date formats that we want to use
$LongDateFormat = "yyyyMMddTHHmmssZ"
# Convert input to universal time
$CurrentDateTime = (Get-Date).ToUniversalTime().ToString($LongDateFormat)
$startDateTime = (Get-Date $EventStart).ToUniversalTime().ToString($LongDateFormat)
$endDateTime = (Get-Date $EventEnd).ToUniversalTime().ToString($LongDateFormat)
$guid = New-Guid
# Fill in ICS/iCalendar properties based on RFC2445
$iCalProperties = @"
BEGIN:VCALENDAR
VERSION:2.0
METHOD:PUBLISH
PRODID:-//it@martin-walther.ch//PowerShell ICS Creator//EN
BEGIN:VEVENT
UID:$guid
TRANSP:OPAQUE
CREATED:$CurrentDateTime
DTSTAMP:$CurrentDateTime
LAST-MODIFIED:$CurrentDateTime
DTSTART:$startDateTime
DTEND:$endDateTime
SUMMARY:$EventSubject
DESCRIPTION:$EventDescription
LOCATION:$EventLocation
SEQUENCE:0
END:VEVENT
END:VCALENDAR
"@
# Output ICS File
$iCalProperties | Out-File "$($FullFilename).ics"
Write-Host "iCalendar-Event saved as $($FullFilename).ics."
$Metadata = @{
Version = '1.0.3'
Author = 'it@martin-walther.ch'
Guid = New-Guid
Description = 'Create simple iCalendar Event with the properties EventStart, EventEnd, EventSubject, EventDescription, and EventLocation'
Path = 'D:\Temp\New-PSiCalEvent\New-PSiCalendarEvent.ps1'
}
Update-ScriptFileInfo @Metadata
For file share-based repositories, SourceLocation and ScriptSourceLocation must match. That’s why I move the script to a Scripts subfolder.
Publish-Script -Path D:\Temp\New-PSiCalEvent\New-PSiCalendarEvent.ps1 -Repository LocalGallery
Move-Item -Path '\\YourShare\PSRepository\New-PSiCalendarEvent.1.0.2.nupkg' `
-Destination '\\YourShare\PSRepository\Scripts\New-PSiCalendarEvent.1.0.2.nupkg'
Find-Script -Repository LocalGallery | Select-Object Version,Name,Description
For nexus repositories with no anonymous access you must provide your API Key/credentials:
Publish-Script -Path D:\Temp\New-PSiCalEvent\New-PSiCalendarEvent.ps1 -Repository myPSGallery -NuGetApiKey 'YourAPIKey' -Verbose
Find-Script -Repository myPSGallery -Credential (Get-Credential) -Verbose
Install-Script -Repository LocalGallery -Name New-PSiCalendarEvent
Get-InstalledScript
New-PSiCalendarEvent `
-EventStart '19.09.2021 13:00' `
-EventEnd '19.09.2021 14:00' `
-EventSubject 'New-PSiCalendarEvent' `
-EventDescription 'New-PSiCalendarEvent Description' `
-EventLocation 'Home Office' `-FullFilename 'New-PSiCalendarEvent'
Uninstall-Script -Name New-PSiCalendarEvent
Working with Private PowerShellGet Repositories on Microsoft Docs, Nexus Docker on github.com, Nexus3 on Docker Hub.